]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kdp/kdp_udp.c
xnu-1699.22.81.tar.gz
[apple/xnu.git] / osfmk / kdp / kdp_udp.c
1 /*
2 * Copyright (c) 2000-2008 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_kdb.h>
39 #include <mach/boolean.h>
40 #include <mach/mach_types.h>
41 #include <mach/exception_types.h>
42 #include <kern/cpu_data.h>
43 #include <kern/debug.h>
44 #include <kern/clock.h>
45
46 #include <kdp/kdp_core.h>
47 #include <kdp/kdp_internal.h>
48 #include <kdp/kdp_en_debugger.h>
49 #include <kdp/kdp_callout.h>
50 #include <kdp/kdp_udp.h>
51 #if CONFIG_SERIAL_KDP
52 #include <kdp/kdp_serial.h>
53 #endif
54
55 #include <vm/vm_map.h>
56 #include <vm/vm_protos.h>
57 #include <vm/vm_kern.h> /* kernel_map */
58
59 #include <mach/memory_object_types.h>
60 #include <machine/pal_routines.h>
61
62 #include <sys/msgbuf.h>
63
64 /* we just want the link status flags, so undef KERNEL_PRIVATE for this
65 * header file. */
66 #undef KERNEL_PRIVATE
67 #include <net/if_media.h>
68 #define KERNEL_PRIVATE
69
70 #include <string.h>
71
72 #include <IOKit/IOPlatformExpert.h>
73 #include <libkern/version.h>
74
75 #define DO_ALIGN 1 /* align all packet data accesses */
76 #define KDP_SERIAL_IPADDR 0xABADBABE /* IP address used for serial KDP */
77 #define LINK_UP_STATUS (IFM_AVALID | IFM_ACTIVE)
78
79 extern int kdp_getc(void);
80 extern int reattach_wait;
81
82 /* only used by IONetworkingFamily */
83 typedef uint32_t (*kdp_link_t)(void);
84 typedef boolean_t (*kdp_mode_t)(boolean_t);
85 void kdp_register_link(kdp_link_t link, kdp_mode_t mode);
86 void kdp_unregister_link(kdp_link_t link, kdp_mode_t mode);
87
88 static u_short ip_id; /* ip packet ctr, for ids */
89
90 /* @(#)udp_usrreq.c 2.2 88/05/23 4.0NFSSRC SMI; from UCB 7.1 6/5/86 */
91
92 /*
93 * UDP protocol implementation.
94 * Per RFC 768, August, 1980.
95 */
96 #define UDP_TTL 60 /* deflt time to live for UDP packets */
97 int udp_ttl = UDP_TTL;
98 static unsigned char exception_seq;
99
100 static struct {
101 unsigned char data[KDP_MAXPACKET];
102 unsigned int off, len;
103 boolean_t input;
104 } pkt, saved_reply;
105
106 struct kdp_manual_pkt manual_pkt;
107
108 struct {
109 struct {
110 struct in_addr in;
111 struct ether_addr ea;
112 } loc;
113 struct {
114 struct in_addr in;
115 struct ether_addr ea;
116 } rmt;
117 } adr;
118
119 static const char
120 *exception_message[] = {
121 "Unknown",
122 "Memory access", /* EXC_BAD_ACCESS */
123 "Failed instruction", /* EXC_BAD_INSTRUCTION */
124 "Arithmetic", /* EXC_ARITHMETIC */
125 "Emulation", /* EXC_EMULATION */
126 "Software", /* EXC_SOFTWARE */
127 "Breakpoint" /* EXC_BREAKPOINT */
128 };
129
130 volatile int kdp_flag = 0;
131
132 static kdp_send_t kdp_en_send_pkt;
133 static kdp_receive_t kdp_en_recv_pkt;
134 static kdp_link_t kdp_en_linkstatus;
135 static kdp_mode_t kdp_en_setmode;
136
137 #if CONFIG_SERIAL_KDP
138 static void kdp_serial_send(void *rpkt, unsigned int rpkt_len);
139 #define KDP_SERIAL_ENABLED() (kdp_en_send_pkt == kdp_serial_send)
140 #else
141 #define KDP_SERIAL_ENABLED() (0)
142 #endif
143
144 static uint32_t kdp_current_ip_address = 0;
145 static struct ether_addr kdp_current_mac_address = {{0, 0, 0, 0, 0, 0}};
146 static void *kdp_current_ifp;
147
148 static void kdp_handler( void *);
149
150 static uint32_t panic_server_ip = 0;
151 static uint32_t parsed_router_ip = 0;
152 static uint32_t router_ip = 0;
153 static uint32_t target_ip = 0;
154
155 static boolean_t save_ip_in_nvram = FALSE;
156
157 static volatile boolean_t panicd_specified = FALSE;
158 static boolean_t router_specified = FALSE;
159 static boolean_t corename_specified = FALSE;
160 static unsigned int panicd_port = CORE_REMOTE_PORT;
161
162 static struct ether_addr etherbroadcastaddr = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
163
164 static struct ether_addr router_mac = {{0, 0, 0 , 0, 0, 0}};
165 static struct ether_addr destination_mac = {{0, 0, 0 , 0, 0, 0}};
166 static struct ether_addr temp_mac = {{0, 0, 0 , 0, 0, 0}};
167 static struct ether_addr current_resolved_MAC = {{0, 0, 0 , 0, 0, 0}};
168
169 static boolean_t flag_panic_dump_in_progress = FALSE;
170 static boolean_t flag_router_mac_initialized = FALSE;
171 static boolean_t flag_dont_abort_panic_dump = FALSE;
172
173 static boolean_t flag_arp_resolved = FALSE;
174
175 static unsigned int panic_timeout = 100000;
176 static unsigned int last_panic_port = CORE_REMOTE_PORT;
177
178 #define KDP_THROTTLE_VALUE (10ULL * NSEC_PER_SEC)
179
180 uint32_t kdp_crashdump_pkt_size = 512;
181 #define KDP_LARGE_CRASHDUMP_PKT_SIZE (1440 - 6 - sizeof(struct udpiphdr))
182 static char panicd_ip_str[20];
183 static char router_ip_str[20];
184 static char corename_str[50];
185
186 static unsigned int panic_block = 0;
187 volatile unsigned int kdp_trigger_core_dump = 0;
188 __private_extern__ volatile unsigned int flag_kdp_trigger_reboot = 0;
189
190 extern unsigned int not_in_kdp;
191
192 extern unsigned int disableConsoleOutput;
193
194 extern void kdp_call(void);
195 extern boolean_t kdp_call_kdb(void);
196 extern int kern_dump(void);
197
198 extern int inet_aton(const char *cp, struct in_addr *pin);
199 extern int inet_ntoa2(struct in_addr * pin, char * cp, const int len);
200
201 void * kdp_get_interface(void);
202 void kdp_set_gateway_mac(void *gatewaymac);
203 void kdp_set_ip_and_mac_addresses(struct in_addr *ipaddr, struct ether_addr *);
204 void kdp_set_interface(void *interface, const struct ether_addr *macaddr);
205
206 void kdp_disable_arp(void);
207 static void kdp_arp_reply(struct ether_arp *);
208 static void kdp_process_arp_reply(struct ether_arp *);
209 static boolean_t kdp_arp_resolve(uint32_t, struct ether_addr *);
210
211 static volatile unsigned kdp_reentry_deadline;
212
213 static uint32_t kdp_crashdump_feature_mask = KDP_FEATURE_LARGE_CRASHDUMPS | KDP_FEATURE_LARGE_PKT_SIZE;
214 uint32_t kdp_feature_large_crashdumps, kdp_feature_large_pkt_size;
215
216 char kdp_kernelversion_string[256];
217
218 static boolean_t gKDPDebug = FALSE;
219 #define KDP_DEBUG(...) if (gKDPDebug) printf(__VA_ARGS__);
220
221 int kdp_snapshot = 0;
222 static int stack_snapshot_ret = 0;
223 static unsigned stack_snapshot_bytes_traced = 0;
224
225 static void *stack_snapshot_buf;
226 static uint32_t stack_snapshot_bufsize;
227 static int stack_snapshot_pid;
228 static uint32_t stack_snapshot_flags;
229 static uint32_t stack_snapshot_dispatch_offset;
230
231 static unsigned int old_debugger;
232
233 #define SBLOCKSZ (2048)
234 uint64_t kdp_dump_start_time = 0;
235 uint64_t kdp_min_superblock_dump_time = ~1ULL;
236 uint64_t kdp_max_superblock_dump_time = 0;
237 uint64_t kdp_superblock_dump_time = 0;
238 uint64_t kdp_superblock_dump_start_time = 0;
239
240 void
241 kdp_snapshot_preflight(int pid, void * tracebuf, uint32_t tracebuf_size,
242 uint32_t flags, uint32_t dispatch_offset);
243
244 void
245 kdp_snapshot_postflight(void);
246
247 extern int
248 kdp_stackshot(int pid, void *tracebuf, uint32_t tracebuf_size,
249 uint32_t flags, uint32_t dispatch_offset, uint32_t *pbytesTraced);
250
251 int
252 kdp_stack_snapshot_geterror(void);
253
254 int
255 kdp_stack_snapshot_bytes_traced(void);
256
257 static thread_call_t
258 kdp_timer_call;
259
260 static void
261 kdp_ml_enter_debugger_wrapper(__unused void *param0, __unused void *param1) {
262 kdp_ml_enter_debugger();
263 }
264
265 static void
266 kdp_timer_callout_init(void) {
267 kdp_timer_call = thread_call_allocate(kdp_ml_enter_debugger_wrapper, NULL);
268 }
269
270
271 /* only send/receive data if the link is up */
272 inline static void wait_for_link(void)
273 {
274 static int first = 0;
275
276 if (!kdp_en_linkstatus)
277 return;
278
279 while (((*kdp_en_linkstatus)() & LINK_UP_STATUS) != LINK_UP_STATUS) {
280 if (first)
281 continue;
282
283 first = 1;
284 printf("Waiting for link to become available.\n");
285 kprintf("Waiting for link to become available.\n");
286 }
287 }
288
289
290 inline static void kdp_send_data(void *packet, unsigned int len)
291 {
292 wait_for_link();
293 (*kdp_en_send_pkt)(packet, len);
294 }
295
296
297 inline static void kdp_receive_data(void *packet, unsigned int *len,
298 unsigned int timeout)
299 {
300 wait_for_link();
301 (*kdp_en_recv_pkt)(packet, len, timeout);
302 }
303
304
305
306 void kdp_register_link(kdp_link_t link, kdp_mode_t mode)
307 {
308 kdp_en_linkstatus = link;
309 kdp_en_setmode = mode;
310 }
311
312 void kdp_unregister_link(__unused kdp_link_t link, __unused kdp_mode_t mode)
313 {
314 kdp_en_linkstatus = NULL;
315 kdp_en_setmode = NULL;
316 }
317
318 void
319 kdp_register_send_receive(
320 kdp_send_t send,
321 kdp_receive_t receive)
322 {
323 unsigned int debug = 0;
324
325 debug_log_init();
326
327 kdp_timer_callout_init();
328
329 PE_parse_boot_argn("debug", &debug, sizeof (debug));
330 kdp_crashdump_feature_mask = htonl(kdp_crashdump_feature_mask);
331
332
333 if (!debug)
334 return;
335
336 kdp_en_send_pkt = send;
337 kdp_en_recv_pkt = receive;
338
339 if (debug & DB_KDP_BP_DIS)
340 kdp_flag |= KDP_BP_DIS;
341 if (debug & DB_KDP_GETC_ENA)
342 kdp_flag |= KDP_GETC_ENA;
343 if (debug & DB_ARP)
344 kdp_flag |= KDP_ARP;
345
346 if (debug & DB_KERN_DUMP_ON_PANIC)
347 kdp_flag |= KDP_PANIC_DUMP_ENABLED;
348 if (debug & DB_KERN_DUMP_ON_NMI)
349 kdp_flag |= PANIC_CORE_ON_NMI;
350
351 if (debug & DB_DBG_POST_CORE)
352 kdp_flag |= DBG_POST_CORE;
353
354 if (debug & DB_PANICLOG_DUMP)
355 kdp_flag |= PANIC_LOG_DUMP;
356
357 if (PE_parse_boot_argn("_panicd_ip", panicd_ip_str, sizeof (panicd_ip_str)))
358 panicd_specified = TRUE;
359
360 if ((debug & DB_REBOOT_POST_CORE) && (panicd_specified == TRUE))
361 kdp_flag |= REBOOT_POST_CORE;
362
363 if (PE_parse_boot_argn("_router_ip", router_ip_str, sizeof (router_ip_str)))
364 router_specified = TRUE;
365
366 if (!PE_parse_boot_argn("panicd_port", &panicd_port, sizeof (panicd_port)))
367 panicd_port = CORE_REMOTE_PORT;
368
369 if (PE_parse_boot_argn("_panicd_corename", &corename_str, sizeof (corename_str)))
370 corename_specified = TRUE;
371
372 kdp_flag |= KDP_READY;
373 if (current_debugger == NO_CUR_DB)
374 current_debugger = KDP_CUR_DB;
375 if ((kdp_current_ip_address != 0) && halt_in_debugger) {
376 kdp_call();
377 halt_in_debugger=0;
378 }
379 }
380
381 void
382 kdp_unregister_send_receive(
383 __unused kdp_send_t send,
384 __unused kdp_receive_t receive)
385 {
386 if (current_debugger == KDP_CUR_DB)
387 current_debugger = NO_CUR_DB;
388 kdp_flag &= ~KDP_READY;
389 kdp_en_send_pkt = NULL;
390 kdp_en_recv_pkt = NULL;
391 }
392
393 /* Cache stack snapshot parameters in preparation for a trace */
394 void
395 kdp_snapshot_preflight(int pid, void * tracebuf, uint32_t tracebuf_size, uint32_t flags, uint32_t dispatch_offset)
396 {
397 stack_snapshot_pid = pid;
398 stack_snapshot_buf = tracebuf;
399 stack_snapshot_bufsize = tracebuf_size;
400 stack_snapshot_flags = flags;
401 stack_snapshot_dispatch_offset = dispatch_offset;
402 kdp_snapshot++;
403 /* Mark this debugger as active, since the polled mode driver that
404 * ordinarily does this may not be enabled (yet), or since KDB may be
405 * the primary debugger.
406 */
407 old_debugger = current_debugger;
408 if (old_debugger != KDP_CUR_DB) {
409 current_debugger = KDP_CUR_DB;
410 }
411 }
412
413 void
414 kdp_snapshot_postflight(void)
415 {
416 kdp_snapshot--;
417 if ((kdp_en_send_pkt == NULL) || (old_debugger == KDB_CUR_DB))
418 current_debugger = old_debugger;
419 }
420
421 int
422 kdp_stack_snapshot_geterror(void)
423 {
424 return stack_snapshot_ret;
425 }
426
427 int
428 kdp_stack_snapshot_bytes_traced(void)
429 {
430 return stack_snapshot_bytes_traced;
431 }
432
433 static void
434 kdp_schedule_debugger_reentry(unsigned interval) {
435 uint64_t deadline;;
436
437 clock_interval_to_deadline(interval, 1000 * 1000, &deadline);
438 thread_call_enter_delayed(kdp_timer_call, deadline);
439 }
440
441 static void
442 enaddr_copy(
443 void *src,
444 void *dst
445 )
446 {
447 bcopy((char *)src, (char *)dst, sizeof (struct ether_addr));
448 }
449
450 static unsigned short
451 ip_sum(
452 unsigned char *c,
453 unsigned int hlen
454 )
455 {
456 unsigned int high, low, sum;
457
458 high = low = 0;
459 while (hlen-- > 0) {
460 low += c[1] + c[3];
461 high += c[0] + c[2];
462
463 c += sizeof (int);
464 }
465
466 sum = (high << 8) + low;
467 sum = (sum >> 16) + (sum & 65535);
468
469 return (sum > 65535 ? sum - 65535 : sum);
470 }
471
472 static void
473 kdp_reply(
474 unsigned short reply_port,
475 const boolean_t sideband
476 )
477 {
478 struct udpiphdr aligned_ui, *ui = &aligned_ui;
479 struct ip aligned_ip, *ip = &aligned_ip;
480 struct in_addr tmp_ipaddr;
481 struct ether_addr tmp_enaddr;
482 struct ether_header *eh = NULL;
483
484 if (!pkt.input)
485 kdp_panic("kdp_reply");
486
487 pkt.off -= (unsigned int)sizeof (struct udpiphdr);
488
489 #if DO_ALIGN
490 bcopy((char *)&pkt.data[pkt.off], (char *)ui, sizeof(*ui));
491 #else
492 ui = (struct udpiphdr *)&pkt.data[pkt.off];
493 #endif
494 ui->ui_next = ui->ui_prev = 0;
495 ui->ui_x1 = 0;
496 ui->ui_pr = IPPROTO_UDP;
497 ui->ui_len = htons((u_short)pkt.len + sizeof (struct udphdr));
498 tmp_ipaddr = ui->ui_src;
499 ui->ui_src = ui->ui_dst;
500 ui->ui_dst = tmp_ipaddr;
501 ui->ui_sport = htons(KDP_REMOTE_PORT);
502 ui->ui_dport = reply_port;
503 ui->ui_ulen = ui->ui_len;
504 ui->ui_sum = 0;
505 #if DO_ALIGN
506 bcopy((char *)ui, (char *)&pkt.data[pkt.off], sizeof(*ui));
507 bcopy((char *)&pkt.data[pkt.off], (char *)ip, sizeof(*ip));
508 #else
509 ip = (struct ip *)&pkt.data[pkt.off];
510 #endif
511 ip->ip_len = htons(sizeof (struct udpiphdr) + pkt.len);
512 ip->ip_v = IPVERSION;
513 ip->ip_id = htons(ip_id++);
514 ip->ip_hl = sizeof (struct ip) >> 2;
515 ip->ip_ttl = udp_ttl;
516 ip->ip_sum = 0;
517 ip->ip_sum = htons(~ip_sum((unsigned char *)ip, ip->ip_hl));
518 #if DO_ALIGN
519 bcopy((char *)ip, (char *)&pkt.data[pkt.off], sizeof(*ip));
520 #endif
521
522 pkt.len += (unsigned int)sizeof (struct udpiphdr);
523
524 pkt.off -= (unsigned int)sizeof (struct ether_header);
525
526 eh = (struct ether_header *)&pkt.data[pkt.off];
527 enaddr_copy(eh->ether_shost, &tmp_enaddr);
528 enaddr_copy(eh->ether_dhost, eh->ether_shost);
529 enaddr_copy(&tmp_enaddr, eh->ether_dhost);
530 eh->ether_type = htons(ETHERTYPE_IP);
531
532 pkt.len += (unsigned int)sizeof (struct ether_header);
533
534 // save reply for possible retransmission
535 assert(pkt.len <= KDP_MAXPACKET);
536 if (!sideband)
537 bcopy((char *)&pkt, (char *)&saved_reply, sizeof(saved_reply));
538
539 kdp_send_data(&pkt.data[pkt.off], pkt.len);
540
541 // increment expected sequence number
542 if (!sideband)
543 exception_seq++;
544 }
545
546 static void
547 kdp_send(
548 unsigned short remote_port
549 )
550 {
551 struct udpiphdr aligned_ui, *ui = &aligned_ui;
552 struct ip aligned_ip, *ip = &aligned_ip;
553 struct ether_header *eh;
554
555 if (pkt.input)
556 kdp_panic("kdp_send");
557
558 pkt.off -= (unsigned int)sizeof (struct udpiphdr);
559
560 #if DO_ALIGN
561 bcopy((char *)&pkt.data[pkt.off], (char *)ui, sizeof(*ui));
562 #else
563 ui = (struct udpiphdr *)&pkt.data[pkt.off];
564 #endif
565 ui->ui_next = ui->ui_prev = 0;
566 ui->ui_x1 = 0;
567 ui->ui_pr = IPPROTO_UDP;
568 ui->ui_len = htons((u_short)pkt.len + sizeof (struct udphdr));
569 ui->ui_src = adr.loc.in;
570 ui->ui_dst = adr.rmt.in;
571 ui->ui_sport = htons(KDP_REMOTE_PORT);
572 ui->ui_dport = remote_port;
573 ui->ui_ulen = ui->ui_len;
574 ui->ui_sum = 0;
575 #if DO_ALIGN
576 bcopy((char *)ui, (char *)&pkt.data[pkt.off], sizeof(*ui));
577 bcopy((char *)&pkt.data[pkt.off], (char *)ip, sizeof(*ip));
578 #else
579 ip = (struct ip *)&pkt.data[pkt.off];
580 #endif
581 ip->ip_len = htons(sizeof (struct udpiphdr) + pkt.len);
582 ip->ip_v = IPVERSION;
583 ip->ip_id = htons(ip_id++);
584 ip->ip_hl = sizeof (struct ip) >> 2;
585 ip->ip_ttl = udp_ttl;
586 ip->ip_sum = 0;
587 ip->ip_sum = htons(~ip_sum((unsigned char *)ip, ip->ip_hl));
588 #if DO_ALIGN
589 bcopy((char *)ip, (char *)&pkt.data[pkt.off], sizeof(*ip));
590 #endif
591
592 pkt.len += (unsigned int)sizeof (struct udpiphdr);
593
594 pkt.off -= (unsigned int)sizeof (struct ether_header);
595
596 eh = (struct ether_header *)&pkt.data[pkt.off];
597 enaddr_copy(&adr.loc.ea, eh->ether_shost);
598 enaddr_copy(&adr.rmt.ea, eh->ether_dhost);
599 eh->ether_type = htons(ETHERTYPE_IP);
600
601 pkt.len += (unsigned int)sizeof (struct ether_header);
602 kdp_send_data(&pkt.data[pkt.off], pkt.len);
603 }
604
605
606 inline static void debugger_if_necessary(void)
607 {
608 if ((current_debugger == KDP_CUR_DB) && halt_in_debugger) {
609 kdp_call();
610 halt_in_debugger=0;
611 }
612 }
613
614
615 /* We don't interpret this pointer, we just give it to the bsd stack
616 so it can decide when to set the MAC and IP info. We'll
617 early initialize the MAC/IP info if we can so that we can use
618 KDP early in boot. These values may subsequently get over-written
619 when the interface gets initialized for real.
620 */
621 void
622 kdp_set_interface(void *ifp, const struct ether_addr *macaddr)
623 {
624 char kdpstr[80];
625 struct in_addr addr = { 0 };
626 unsigned int len;
627
628 kdp_current_ifp = ifp;
629
630 if (PE_parse_boot_argn("kdp_ip_addr", kdpstr, sizeof(kdpstr))) {
631 /* look for a static ip address */
632 if (inet_aton(kdpstr, &addr) == FALSE)
633 goto done;
634
635 goto config_network;
636 }
637
638 /* use saved ip address */
639 save_ip_in_nvram = TRUE;
640
641 len = sizeof(kdpstr);
642 if (PEReadNVRAMProperty("_kdp_ipstr", kdpstr, &len) == FALSE)
643 goto done;
644
645 kdpstr[len < sizeof(kdpstr) ? len : sizeof(kdpstr) - 1] = '\0';
646 if (inet_aton(kdpstr, &addr) == FALSE)
647 goto done;
648
649 config_network:
650 kdp_current_ip_address = addr.s_addr;
651 if (macaddr)
652 kdp_current_mac_address = *macaddr;
653
654 /* we can't drop into the debugger at this point because the
655 link will likely not be up. when getDebuggerLinkStatus() support gets
656 added to the appropriate network drivers, adding the
657 following will enable this capability:
658 debugger_if_necessary();
659 */
660 done:
661 return;
662 }
663
664 void *
665 kdp_get_interface(void)
666 {
667 return kdp_current_ifp;
668 }
669
670 void
671 kdp_set_ip_and_mac_addresses(
672 struct in_addr *ipaddr,
673 struct ether_addr *macaddr)
674 {
675 static uint64_t last_time = (uint64_t) -1;
676 static uint64_t throttle_val = 0;
677 uint64_t cur_time;
678 char addr[16];
679
680 if (kdp_current_ip_address == ipaddr->s_addr)
681 goto done;
682
683 /* don't replace if serial debugging is configured */
684 if (!KDP_SERIAL_ENABLED() ||
685 (kdp_current_ip_address != KDP_SERIAL_IPADDR)) {
686 kdp_current_mac_address = *macaddr;
687 kdp_current_ip_address = ipaddr->s_addr;
688 }
689
690 if (save_ip_in_nvram == FALSE)
691 goto done;
692
693 if (inet_ntoa2(ipaddr, addr, sizeof(addr)) == FALSE)
694 goto done;
695
696 /* throttle writes if needed */
697 if (!throttle_val)
698 nanoseconds_to_absolutetime(KDP_THROTTLE_VALUE, &throttle_val);
699
700 cur_time = mach_absolute_time();
701 if (last_time == (uint64_t) -1 ||
702 ((cur_time - last_time) > throttle_val)) {
703 PEWriteNVRAMProperty("_kdp_ipstr", addr,
704 (const unsigned int) strlen(addr));
705 }
706 last_time = cur_time;
707
708 done:
709 debugger_if_necessary();
710 }
711
712 void
713 kdp_set_gateway_mac(void *gatewaymac)
714 {
715 router_mac = *(struct ether_addr *)gatewaymac;
716 flag_router_mac_initialized = TRUE;
717 }
718
719 struct ether_addr
720 kdp_get_mac_addr(void)
721 {
722 return kdp_current_mac_address;
723 }
724
725 unsigned int
726 kdp_get_ip_address(void)
727 {
728 return (unsigned int)kdp_current_ip_address;
729 }
730
731 void
732 kdp_disable_arp(void)
733 {
734 kdp_flag &= ~(DB_ARP);
735 }
736
737 static void
738 kdp_arp_dispatch(void)
739 {
740 struct ether_arp aligned_ea, *ea = &aligned_ea;
741 unsigned arp_header_offset;
742
743 arp_header_offset = (unsigned)sizeof(struct ether_header) + pkt.off;
744 memcpy((void *)ea, (void *)&pkt.data[arp_header_offset], sizeof(*ea));
745
746 switch(ntohs(ea->arp_op)) {
747 case ARPOP_REQUEST:
748 kdp_arp_reply(ea);
749 break;
750 case ARPOP_REPLY:
751 kdp_process_arp_reply(ea);
752 break;
753 default:
754 return;
755 }
756 }
757
758 static void
759 kdp_process_arp_reply(struct ether_arp *ea)
760 {
761 /* Are we interested in ARP replies? */
762 if (flag_arp_resolved == TRUE)
763 return;
764
765 /* Did we receive a reply from the right source? */
766 if (((struct in_addr *)(ea->arp_spa))->s_addr != target_ip)
767 return;
768
769 flag_arp_resolved = TRUE;
770 current_resolved_MAC = *(struct ether_addr *) (ea->arp_sha);
771
772 return;
773 }
774
775 /* ARP responses are enabled when the DB_ARP bit of the debug boot arg
776 * is set.
777 */
778
779 static void
780 kdp_arp_reply(struct ether_arp *ea)
781 {
782 struct ether_header *eh;
783
784 struct in_addr isaddr, itaddr, myaddr;
785 struct ether_addr my_enaddr;
786
787 eh = (struct ether_header *)&pkt.data[pkt.off];
788 pkt.off += (unsigned int)sizeof(struct ether_header);
789
790 if(ntohs(ea->arp_op) != ARPOP_REQUEST)
791 return;
792
793 myaddr.s_addr = kdp_get_ip_address();
794 my_enaddr = kdp_get_mac_addr();
795
796 if ((ntohl(myaddr.s_addr) == 0) ||
797 ((my_enaddr.ether_addr_octet[0] & 0xff) == 0
798 && (my_enaddr.ether_addr_octet[1] & 0xff) == 0
799 && (my_enaddr.ether_addr_octet[2] & 0xff) == 0
800 && (my_enaddr.ether_addr_octet[3] & 0xff) == 0
801 && (my_enaddr.ether_addr_octet[4] & 0xff) == 0
802 && (my_enaddr.ether_addr_octet[5] & 0xff) == 0
803 ))
804 return;
805
806 (void)memcpy((void *)&isaddr, (void *)ea->arp_spa, sizeof (isaddr));
807 (void)memcpy((void *)&itaddr, (void *)ea->arp_tpa, sizeof (itaddr));
808
809 if (itaddr.s_addr == myaddr.s_addr) {
810 (void)memcpy((void *)ea->arp_tha, (void *)ea->arp_sha, sizeof(ea->arp_sha));
811 (void)memcpy((void *)ea->arp_sha, (void *)&my_enaddr, sizeof(ea->arp_sha));
812
813 (void)memcpy((void *)ea->arp_tpa, (void *) ea->arp_spa, sizeof(ea->arp_spa));
814 (void)memcpy((void *)ea->arp_spa, (void *) &itaddr, sizeof(ea->arp_spa));
815
816 ea->arp_op = htons(ARPOP_REPLY);
817 ea->arp_pro = htons(ETHERTYPE_IP);
818 (void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost));
819 (void)memcpy(eh->ether_shost, &my_enaddr, sizeof(eh->ether_shost));
820 eh->ether_type = htons(ETHERTYPE_ARP);
821 (void)memcpy(&pkt.data[pkt.off], ea, sizeof(*ea));
822 pkt.off -= (unsigned int)sizeof (struct ether_header);
823 /* pkt.len is still the length we want, ether_header+ether_arp */
824 kdp_send_data(&pkt.data[pkt.off], pkt.len);
825 }
826 }
827
828 static void
829 kdp_poll(void)
830 {
831 struct ether_header *eh = NULL;
832 struct udpiphdr aligned_ui, *ui = &aligned_ui;
833 struct ip aligned_ip, *ip = &aligned_ip;
834 static int msg_printed;
835
836 if (pkt.input)
837 kdp_panic("kdp_poll");
838
839 if (!kdp_en_recv_pkt || !kdp_en_send_pkt) {
840 if( msg_printed == 0) {
841 msg_printed = 1;
842 printf("kdp_poll: no debugger device\n");
843 }
844 return;
845 }
846
847 pkt.off = pkt.len = 0;
848 kdp_receive_data(pkt.data, &pkt.len, 3/* ms */);
849
850 if (pkt.len == 0)
851 return;
852
853 if (pkt.len >= sizeof(struct ether_header))
854 {
855 eh = (struct ether_header *)&pkt.data[pkt.off];
856
857 if (kdp_flag & KDP_ARP)
858 {
859 if (ntohs(eh->ether_type) == ETHERTYPE_ARP)
860 {
861 kdp_arp_dispatch();
862 return;
863 }
864 }
865 }
866
867 if (pkt.len < (sizeof (struct ether_header) + sizeof (struct udpiphdr)))
868 return;
869
870 pkt.off += (unsigned int)sizeof (struct ether_header);
871 if (ntohs(eh->ether_type) != ETHERTYPE_IP) {
872 return;
873 }
874
875 #if DO_ALIGN
876 bcopy((char *)&pkt.data[pkt.off], (char *)ui, sizeof(*ui));
877 bcopy((char *)&pkt.data[pkt.off], (char *)ip, sizeof(*ip));
878 #else
879 ui = (struct udpiphdr *)&pkt.data[pkt.off];
880 ip = (struct ip *)&pkt.data[pkt.off];
881 #endif
882
883 pkt.off += (unsigned int)sizeof (struct udpiphdr);
884 if (ui->ui_pr != IPPROTO_UDP) {
885 return;
886 }
887
888 if (ip->ip_hl > (sizeof (struct ip) >> 2)) {
889 return;
890 }
891
892 if (ntohs(ui->ui_dport) != KDP_REMOTE_PORT) {
893 if (panicd_port == (ntohs(ui->ui_dport)) &&
894 flag_panic_dump_in_progress) {
895 last_panic_port = ui->ui_sport;
896 }
897 else
898 return;
899 }
900 /* If we receive a kernel debugging packet whilst a
901 * core dump is in progress, abort the transfer and
902 * enter the debugger if not told otherwise.
903 */
904 else
905 if (flag_panic_dump_in_progress)
906 {
907 if (!flag_dont_abort_panic_dump) {
908 abort_panic_transfer();
909 }
910 return;
911 }
912
913 if (!kdp.is_conn && !flag_panic_dump_in_progress) {
914 enaddr_copy(eh->ether_dhost, &adr.loc.ea);
915 adr.loc.in = ui->ui_dst;
916
917 enaddr_copy(eh->ether_shost, &adr.rmt.ea);
918 adr.rmt.in = ui->ui_src;
919 }
920
921 /*
922 * Calculate kdp packet length.
923 */
924 pkt.len = ntohs((u_short)ui->ui_ulen) - (unsigned int)sizeof (struct udphdr);
925 pkt.input = TRUE;
926 }
927
928 /* Create and transmit an ARP resolution request for the target IP address.
929 * This is modeled on ether_inet_arp()/RFC 826.
930 */
931
932 static void
933 transmit_ARP_request(uint32_t ip_addr)
934 {
935 struct ether_header *eh = (struct ether_header *) &pkt.data[0];
936 struct ether_arp *ea = (struct ether_arp *) &pkt.data[sizeof(struct ether_header)];
937
938 KDP_DEBUG("Transmitting ARP request\n");
939 /* Populate the ether_header */
940 eh->ether_type = htons(ETHERTYPE_ARP);
941 enaddr_copy(&kdp_current_mac_address, eh->ether_shost);
942 enaddr_copy(&etherbroadcastaddr, eh->ether_dhost);
943
944 /* Populate the ARP header */
945 ea->arp_pro = htons(ETHERTYPE_IP);
946 ea->arp_hln = sizeof(ea->arp_sha);
947 ea->arp_pln = sizeof(ea->arp_spa);
948 ea->arp_hrd = htons(ARPHRD_ETHER);
949 ea->arp_op = htons(ARPOP_REQUEST);
950
951 /* Target fields */
952 enaddr_copy(&etherbroadcastaddr, ea->arp_tha);
953 memcpy(ea->arp_tpa, (void *) &ip_addr, sizeof(ip_addr));
954
955 /* Source fields */
956 enaddr_copy(&kdp_current_mac_address, ea->arp_sha);
957 memcpy(ea->arp_spa, (void *) &kdp_current_ip_address, sizeof(kdp_current_ip_address));
958
959 pkt.off = 0;
960 pkt.len = sizeof(struct ether_header) + sizeof(struct ether_arp);
961 /* Transmit */
962 kdp_send_data(&pkt.data[pkt.off], pkt.len);
963 }
964
965 static boolean_t
966 kdp_arp_resolve(uint32_t arp_target_ip, struct ether_addr *resolved_MAC)
967 {
968 int poll_count = 256; /* ~770 ms modulo broadcast/delayed traffic? */
969 char tretries = 0;
970
971 #define NUM_ARP_TX_RETRIES 5
972
973 target_ip = arp_target_ip;
974 flag_arp_resolved = FALSE;
975
976 TRANSMIT_RETRY:
977 pkt.off = pkt.len = 0;
978
979 tretries++;
980
981 if (tretries >= NUM_ARP_TX_RETRIES) {
982 return FALSE;
983 }
984
985 KDP_DEBUG("ARP TX attempt #%d \n", tretries);
986
987 transmit_ARP_request(arp_target_ip);
988
989 while (!pkt.input && !flag_arp_resolved && flag_panic_dump_in_progress && --poll_count) {
990 kdp_poll();
991 }
992
993 if (flag_arp_resolved) {
994 *resolved_MAC = current_resolved_MAC;
995 return TRUE;
996 }
997
998 if (!flag_panic_dump_in_progress || pkt.input) /* we received a debugging packet, bail*/
999 {
1000 printf("Received a debugger packet,transferring control to debugger\n");
1001 /* Indicate that we should wait in the debugger when we return */
1002 kdp_flag |= DBG_POST_CORE;
1003 pkt.input = FALSE;
1004 return FALSE;
1005 }
1006 else /* We timed out */
1007 if (0 == poll_count) {
1008 poll_count = 256;
1009 goto TRANSMIT_RETRY;
1010 }
1011 return FALSE;
1012 }
1013
1014 static void
1015 kdp_handler(
1016 void *saved_state
1017 )
1018 {
1019 unsigned short reply_port;
1020 kdp_hdr_t aligned_hdr, *hdr = &aligned_hdr;
1021
1022 kdp.saved_state = saved_state; // see comment in kdp_raise_exception
1023
1024 do {
1025 while (!pkt.input)
1026 kdp_poll();
1027
1028 #if DO_ALIGN
1029 bcopy((char *)&pkt.data[pkt.off], (char *)hdr, sizeof(*hdr));
1030 #else
1031 hdr = (kdp_hdr_t *)&pkt.data[pkt.off];
1032 #endif
1033
1034 // ignore replies -- we're not expecting them anyway.
1035 if (hdr->is_reply) {
1036 goto again;
1037 }
1038
1039 if (hdr->request == KDP_REATTACH)
1040 exception_seq = hdr->seq;
1041
1042 // check for retransmitted request
1043 if (hdr->seq == (exception_seq - 1)) {
1044 /* retransmit last reply */
1045 kdp_send_data(&saved_reply.data[saved_reply.off],
1046 saved_reply.len);
1047 goto again;
1048 } else if ((hdr->seq != exception_seq) &&
1049 (hdr->request != KDP_CONNECT)) {
1050 printf("kdp: bad sequence %d (want %d)\n",
1051 hdr->seq, exception_seq);
1052 goto again;
1053 }
1054
1055 /* This is a manual side-channel to the main KDP protocol.
1056 * A client like GDB/kgmacros can manually construct
1057 * a request, set the input flag, issue a dummy KDP request,
1058 * and then manually collect the result
1059 */
1060 if (manual_pkt.input) {
1061 kdp_hdr_t *manual_hdr = (kdp_hdr_t *)&manual_pkt.data;
1062 unsigned short manual_port_unused = 0;
1063 if (!manual_hdr->is_reply) {
1064 /* process */
1065 kdp_packet((unsigned char *)&manual_pkt.data,
1066 (int *)&manual_pkt.len,
1067 &manual_port_unused);
1068 }
1069 manual_pkt.input = 0;
1070 }
1071
1072 if (kdp_packet((unsigned char*)&pkt.data[pkt.off],
1073 (int *)&pkt.len,
1074 (unsigned short *)&reply_port)) {
1075 boolean_t sideband = FALSE;
1076
1077 /* if it's an already connected error message,
1078 * send a sideband reply for that. for successful connects,
1079 * make sure the sequence number is correct. */
1080 if (hdr->request == KDP_CONNECT) {
1081 kdp_connect_reply_t *rp =
1082 (kdp_connect_reply_t *) &pkt.data[pkt.off];
1083 kdp_error_t err = rp->error;
1084
1085 if (err == KDPERR_NO_ERROR) {
1086 exception_seq = hdr->seq;
1087 } else if (err == KDPERR_ALREADY_CONNECTED) {
1088 sideband = TRUE;
1089 }
1090 }
1091
1092 kdp_reply(reply_port, sideband);
1093 }
1094
1095 again:
1096 pkt.input = FALSE;
1097 } while (kdp.is_halted);
1098 }
1099
1100 static void
1101 kdp_connection_wait(void)
1102 {
1103 unsigned short reply_port;
1104 struct ether_addr kdp_mac_addr = kdp_get_mac_addr();
1105 unsigned int ip_addr = ntohl(kdp_get_ip_address());
1106
1107 /*
1108 * Do both a printf() and a kprintf() of the MAC and IP so that
1109 * they will print out on headless machines but not be added to
1110 * the panic.log
1111 */
1112
1113 if (KDP_SERIAL_ENABLED()) {
1114 printf("Using serial KDP.\n");
1115 kprintf("Using serial KDP.\n");
1116 } else {
1117 printf( "ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1118 kdp_mac_addr.ether_addr_octet[0] & 0xff,
1119 kdp_mac_addr.ether_addr_octet[1] & 0xff,
1120 kdp_mac_addr.ether_addr_octet[2] & 0xff,
1121 kdp_mac_addr.ether_addr_octet[3] & 0xff,
1122 kdp_mac_addr.ether_addr_octet[4] & 0xff,
1123 kdp_mac_addr.ether_addr_octet[5] & 0xff);
1124
1125 kprintf( "ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1126 kdp_mac_addr.ether_addr_octet[0] & 0xff,
1127 kdp_mac_addr.ether_addr_octet[1] & 0xff,
1128 kdp_mac_addr.ether_addr_octet[2] & 0xff,
1129 kdp_mac_addr.ether_addr_octet[3] & 0xff,
1130 kdp_mac_addr.ether_addr_octet[4] & 0xff,
1131 kdp_mac_addr.ether_addr_octet[5] & 0xff);
1132
1133 printf( "ip address: %d.%d.%d.%d\n",
1134 (ip_addr & 0xff000000) >> 24,
1135 (ip_addr & 0xff0000) >> 16,
1136 (ip_addr & 0xff00) >> 8,
1137 (ip_addr & 0xff));
1138
1139 kprintf( "ip address: %d.%d.%d.%d\n",
1140 (ip_addr & 0xff000000) >> 24,
1141 (ip_addr & 0xff0000) >> 16,
1142 (ip_addr & 0xff00) >> 8,
1143 (ip_addr & 0xff));
1144 }
1145
1146 printf("\nWaiting for remote debugger connection.\n");
1147
1148
1149 if (reattach_wait == 0) {
1150 if((kdp_flag & KDP_GETC_ENA) && (0 != kdp_getc()))
1151 {
1152 printf("Options..... Type\n");
1153 printf("------------ ----\n");
1154 printf("continue.... 'c'\n");
1155 printf("reboot...... 'r'\n");
1156 #if MACH_KDB
1157 printf("enter kdb... 'k'\n");
1158 #endif
1159 }
1160 } else
1161 reattach_wait = 0;
1162
1163 exception_seq = 0;
1164
1165 do {
1166 kdp_hdr_t aligned_hdr, *hdr = &aligned_hdr;
1167
1168 while (!pkt.input) {
1169 if (kdp_flag & KDP_GETC_ENA) {
1170 switch(kdp_getc()) {
1171 case 'c':
1172 printf("Continuing...\n");
1173 return;
1174 case 'r':
1175 printf("Rebooting...\n");
1176 kdp_machine_reboot();
1177 break;
1178 #if MACH_KDB
1179 case 'k':
1180 printf("calling kdb...\n");
1181 if (kdp_call_kdb())
1182 return;
1183 else
1184 printf("not implemented...\n");
1185 #endif
1186 default:
1187 break;
1188 }
1189 }
1190 kdp_poll();
1191 }
1192
1193 #if DO_ALIGN
1194 bcopy((char *)&pkt.data[pkt.off], (char *)hdr, sizeof(*hdr));
1195 #else
1196 hdr = (kdp_hdr_t *)&pkt.data[pkt.off];
1197 #endif
1198 if (hdr->request == KDP_HOSTREBOOT) {
1199 kdp_machine_reboot();
1200 /* should not return! */
1201 }
1202 if (((hdr->request == KDP_CONNECT) || (hdr->request == KDP_REATTACH)) &&
1203 !hdr->is_reply && (hdr->seq == exception_seq)) {
1204 if (kdp_packet((unsigned char *)&pkt.data[pkt.off],
1205 (int *)&pkt.len,
1206 (unsigned short *)&reply_port))
1207 kdp_reply(reply_port, FALSE);
1208 if (hdr->request == KDP_REATTACH) {
1209 reattach_wait = 0;
1210 hdr->request=KDP_DISCONNECT;
1211 exception_seq = 0;
1212 }
1213 }
1214
1215 pkt.input = FALSE;
1216 } while (!kdp.is_conn);
1217
1218 if (current_debugger == KDP_CUR_DB)
1219 active_debugger=1;
1220 printf("Connected to remote debugger.\n");
1221 }
1222
1223 static void
1224 kdp_send_exception(
1225 unsigned int exception,
1226 unsigned int code,
1227 unsigned int subcode
1228 )
1229 {
1230 unsigned short remote_port;
1231 unsigned int timeout_count = 100;
1232 unsigned int poll_timeout;
1233
1234 do {
1235 pkt.off = sizeof (struct ether_header) + sizeof (struct udpiphdr);
1236 kdp_exception((unsigned char *)&pkt.data[pkt.off],
1237 (int *)&pkt.len,
1238 (unsigned short *)&remote_port,
1239 (unsigned int)exception,
1240 (unsigned int)code,
1241 (unsigned int)subcode);
1242
1243 kdp_send(remote_port);
1244
1245 poll_timeout = 50;
1246 while(!pkt.input && poll_timeout)
1247 {
1248 kdp_poll();
1249 poll_timeout--;
1250 }
1251
1252 if (pkt.input) {
1253 if (!kdp_exception_ack(&pkt.data[pkt.off], pkt.len)) {
1254 pkt.input = FALSE;
1255 }
1256 }
1257
1258 pkt.input = FALSE;
1259
1260 if (kdp.exception_ack_needed)
1261 kdp_us_spin(250000);
1262
1263 } while (kdp.exception_ack_needed && timeout_count--);
1264
1265 if (kdp.exception_ack_needed) {
1266 // give up & disconnect
1267 printf("kdp: exception ack timeout\n");
1268 if (current_debugger == KDP_CUR_DB)
1269 active_debugger=0;
1270 kdp_reset();
1271 }
1272 }
1273
1274 void
1275 kdp_raise_exception(
1276 unsigned int exception,
1277 unsigned int code,
1278 unsigned int subcode,
1279 void *saved_state
1280 )
1281 {
1282 int index;
1283
1284 /* Was a system trace requested ? */
1285 if (kdp_snapshot && (!panic_active()) && (panic_caller == 0)) {
1286 stack_snapshot_ret = kdp_stackshot(stack_snapshot_pid,
1287 stack_snapshot_buf, stack_snapshot_bufsize,
1288 stack_snapshot_flags, stack_snapshot_dispatch_offset,
1289 &stack_snapshot_bytes_traced);
1290 return;
1291 }
1292
1293 disable_preemption();
1294
1295 if (saved_state == 0)
1296 printf("kdp_raise_exception with NULL state\n");
1297
1298 index = exception;
1299 if (exception != EXC_BREAKPOINT) {
1300 if (exception > EXC_BREAKPOINT || exception < EXC_BAD_ACCESS) {
1301 index = 0;
1302 }
1303 printf("%s exception (%x,%x,%x)\n",
1304 exception_message[index],
1305 exception, code, subcode);
1306 }
1307
1308 kdp_sync_cache();
1309
1310 /* XXX WMG it seems that sometimes it doesn't work to let kdp_handler
1311 * do this. I think the client and the host can get out of sync.
1312 */
1313 kdp.saved_state = saved_state;
1314 kdp.kdp_cpu = cpu_number();
1315 kdp.kdp_thread = current_thread();
1316
1317 if (kdp_en_setmode)
1318 (*kdp_en_setmode)(TRUE); /* enabling link mode */
1319
1320 if (pkt.input)
1321 kdp_panic("kdp_raise_exception");
1322
1323 if (((kdp_flag & KDP_PANIC_DUMP_ENABLED) || (kdp_flag & PANIC_LOG_DUMP))
1324 && (panicstr != (char *) 0)) {
1325 kdp_panic_dump();
1326 if (kdp_flag & REBOOT_POST_CORE)
1327 kdp_machine_reboot();
1328 }
1329 else
1330 if ((kdp_flag & PANIC_CORE_ON_NMI) && (panicstr == (char *) 0) &&
1331 !kdp.is_conn) {
1332
1333 disable_debug_output = disableConsoleOutput = FALSE;
1334 kdp_panic_dump();
1335
1336 if (!(kdp_flag & DBG_POST_CORE))
1337 goto exit_raise_exception;
1338 }
1339
1340 again:
1341 if (!kdp.is_conn)
1342 kdp_connection_wait();
1343 else {
1344 kdp_send_exception(exception, code, subcode);
1345 if (kdp.exception_ack_needed) {
1346 kdp.exception_ack_needed = FALSE;
1347 kdp_remove_all_breakpoints();
1348 printf("Remote debugger disconnected.\n");
1349 }
1350 }
1351
1352 if (kdp.is_conn) {
1353 kdp.is_halted = TRUE; /* XXX */
1354 kdp_handler(saved_state);
1355 if (!kdp.is_conn)
1356 {
1357 kdp_remove_all_breakpoints();
1358 printf("Remote debugger disconnected.\n");
1359 }
1360 }
1361 /* Allow triggering a panic core dump when connected to the machine
1362 * Continuing after setting kdp_trigger_core_dump should do the
1363 * trick.
1364 */
1365
1366 if (1 == kdp_trigger_core_dump) {
1367 kdp_flag |= KDP_PANIC_DUMP_ENABLED;
1368 kdp_panic_dump();
1369 if (kdp_flag & REBOOT_POST_CORE)
1370 kdp_machine_reboot();
1371 kdp_trigger_core_dump = 0;
1372 }
1373
1374 /* Trigger a reboot if the user has set this flag through the
1375 * debugger.Ideally, this would be done through the HOSTREBOOT packet
1376 * in the protocol,but that will need gdb support,and when it's
1377 * available, it should work automatically.
1378 */
1379 if (1 == flag_kdp_trigger_reboot) {
1380 kdp_machine_reboot();
1381 /* If we're still around, reset the flag */
1382 flag_kdp_trigger_reboot = 0;
1383 }
1384
1385 if (kdp_reentry_deadline) {
1386 kdp_schedule_debugger_reentry(kdp_reentry_deadline);
1387 printf("Debugger re-entry scheduled in %d milliseconds\n", kdp_reentry_deadline);
1388 kdp_reentry_deadline = 0;
1389 }
1390
1391 kdp_sync_cache();
1392
1393 if (reattach_wait == 1)
1394 goto again;
1395
1396 exit_raise_exception:
1397 if (kdp_en_setmode)
1398 (*kdp_en_setmode)(FALSE); /* link cleanup */
1399 enable_preemption();
1400 }
1401
1402 void
1403 kdp_reset(void)
1404 {
1405 kdp.reply_port = kdp.exception_port = 0;
1406 kdp.is_halted = kdp.is_conn = FALSE;
1407 kdp.exception_seq = kdp.conn_seq = 0;
1408 kdp.session_key = 0;
1409 }
1410
1411 struct corehdr *
1412 create_panic_header(unsigned int request, const char *corename,
1413 unsigned length, unsigned int block)
1414 {
1415 struct udpiphdr aligned_ui, *ui = &aligned_ui;
1416 struct ip aligned_ip, *ip = &aligned_ip;
1417 struct ether_header *eh;
1418 struct corehdr *coreh;
1419 const char *mode = "octet";
1420 char modelen = strlen(mode);
1421
1422 size_t fmask_size = sizeof(KDP_FEATURE_MASK_STRING) + sizeof(kdp_crashdump_feature_mask);
1423
1424 pkt.off = sizeof (struct ether_header);
1425 pkt.len = (unsigned int)(length + ((request == KDP_WRQ) ? modelen + fmask_size : 0) +
1426 (corename ? strlen(corename): 0) + sizeof(struct corehdr));
1427
1428 #if DO_ALIGN
1429 bcopy((char *)&pkt.data[pkt.off], (char *)ui, sizeof(*ui));
1430 #else
1431 ui = (struct udpiphdr *)&pkt.data[pkt.off];
1432 #endif
1433 ui->ui_next = ui->ui_prev = 0;
1434 ui->ui_x1 = 0;
1435 ui->ui_pr = IPPROTO_UDP;
1436 ui->ui_len = htons((u_short)pkt.len + sizeof (struct udphdr));
1437 ui->ui_src.s_addr = (uint32_t)kdp_current_ip_address;
1438 /* Already in network byte order via inet_aton() */
1439 ui->ui_dst.s_addr = panic_server_ip;
1440 ui->ui_sport = htons(panicd_port);
1441 ui->ui_dport = ((request == KDP_WRQ) ? htons(panicd_port) : last_panic_port);
1442 ui->ui_ulen = ui->ui_len;
1443 ui->ui_sum = 0;
1444 #if DO_ALIGN
1445 bcopy((char *)ui, (char *)&pkt.data[pkt.off], sizeof(*ui));
1446 bcopy((char *)&pkt.data[pkt.off], (char *)ip, sizeof(*ip));
1447 #else
1448 ip = (struct ip *)&pkt.data[pkt.off];
1449 #endif
1450 ip->ip_len = htons(sizeof (struct udpiphdr) + pkt.len);
1451 ip->ip_v = IPVERSION;
1452 ip->ip_id = htons(ip_id++);
1453 ip->ip_hl = sizeof (struct ip) >> 2;
1454 ip->ip_ttl = udp_ttl;
1455 ip->ip_sum = 0;
1456 ip->ip_sum = htons(~ip_sum((unsigned char *)ip, ip->ip_hl));
1457 #if DO_ALIGN
1458 bcopy((char *)ip, (char *)&pkt.data[pkt.off], sizeof(*ip));
1459 #endif
1460
1461 pkt.len += (unsigned int)sizeof (struct udpiphdr);
1462
1463 pkt.off += (unsigned int)sizeof (struct udpiphdr);
1464
1465 coreh = (struct corehdr *) &pkt.data[pkt.off];
1466 coreh->th_opcode = htons((u_short)request);
1467
1468 if (request == KDP_WRQ)
1469 {
1470 char *cp;
1471
1472 cp = coreh->th_u.tu_rpl;
1473 cp += strlcpy (cp, corename, KDP_MAXPACKET);
1474 *cp++ = '\0';
1475 cp += strlcpy (cp, mode, KDP_MAXPACKET - strlen(corename));
1476 *cp++ = '\0';
1477 cp += strlcpy(cp, KDP_FEATURE_MASK_STRING, sizeof(KDP_FEATURE_MASK_STRING));
1478 *cp++ = '\0'; /* Redundant */
1479 bcopy(&kdp_crashdump_feature_mask, cp, sizeof(kdp_crashdump_feature_mask));
1480 kdp_crashdump_pkt_size = KDP_LARGE_CRASHDUMP_PKT_SIZE;
1481 PE_parse_boot_argn("kdp_crashdump_pkt_size", &kdp_crashdump_pkt_size, sizeof(kdp_crashdump_pkt_size));
1482 cp += sizeof(kdp_crashdump_feature_mask);
1483 *(uint32_t *)cp = htonl(kdp_crashdump_pkt_size);
1484 }
1485 else
1486 {
1487 coreh->th_block = htonl((unsigned int) block);
1488 }
1489
1490 pkt.off -= (unsigned int)sizeof (struct udpiphdr);
1491 pkt.off -= (unsigned int)sizeof (struct ether_header);
1492
1493 eh = (struct ether_header *)&pkt.data[pkt.off];
1494 enaddr_copy(&kdp_current_mac_address, eh->ether_shost);
1495 enaddr_copy(&destination_mac, eh->ether_dhost);
1496 eh->ether_type = htons(ETHERTYPE_IP);
1497
1498 pkt.len += (unsigned int)sizeof (struct ether_header);
1499 return coreh;
1500 }
1501
1502 static int kdp_send_crashdump_seek(char *corename, uint64_t seek_off)
1503 {
1504 int panic_error;
1505
1506 if (kdp_feature_large_crashdumps) {
1507 panic_error = kdp_send_crashdump_pkt(KDP_SEEK, corename,
1508 sizeof(seek_off),
1509 &seek_off);
1510 } else {
1511 uint32_t off = (uint32_t) seek_off;
1512 panic_error = kdp_send_crashdump_pkt(KDP_SEEK, corename,
1513 sizeof(off), &off);
1514 }
1515
1516 if (panic_error < 0) {
1517 printf ("kdp_send_crashdump_pkt failed with error %d\n",
1518 panic_error);
1519 return panic_error;
1520 }
1521
1522 return 0;
1523 }
1524
1525 int kdp_send_crashdump_data(unsigned int request, char *corename,
1526 int64_t length, caddr_t txstart)
1527 {
1528 int panic_error = 0;
1529
1530 while (length > 0) {
1531 uint64_t chunk = MIN(kdp_crashdump_pkt_size, length);
1532
1533 panic_error = kdp_send_crashdump_pkt(request, corename, chunk,
1534 txstart);
1535 if (panic_error < 0) {
1536 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error);
1537 return panic_error;
1538 }
1539
1540 txstart += chunk;
1541 length -= chunk;
1542 }
1543 return 0;
1544 }
1545
1546 uint32_t kdp_crashdump_short_pkt;
1547
1548 int
1549 kdp_send_crashdump_pkt(unsigned int request, char *corename,
1550 uint64_t length, void *panic_data)
1551 {
1552 int poll_count;
1553 struct corehdr *th = NULL;
1554 char rretries, tretries;
1555
1556 if (kdp_dump_start_time == 0) {
1557 kdp_dump_start_time = mach_absolute_time();
1558 kdp_superblock_dump_start_time = kdp_dump_start_time;
1559 }
1560
1561 tretries = rretries = 0;
1562 poll_count = KDP_CRASHDUMP_POLL_COUNT;
1563 pkt.off = pkt.len = 0;
1564 if (request == KDP_WRQ) /* longer timeout for initial request */
1565 poll_count += 1000;
1566
1567 TRANSMIT_RETRY:
1568 tretries++;
1569
1570 if (tretries >=15) {
1571 /* The crashdump server is unreachable for some reason. This could be a network
1572 * issue or, if we've been especially unfortunate, we've hit Radar 2760413,
1573 * which is a long standing problem with the IOKit polled mode network driver
1574 * shim which can prevent transmits/receives completely.
1575 */
1576 printf ("Cannot contact panic server, timing out.\n");
1577 return (-3);
1578 }
1579
1580 if (tretries > 2)
1581 printf("TX retry #%d ", tretries );
1582
1583 th = create_panic_header(request, corename, (unsigned)length, panic_block);
1584
1585 if (request == KDP_DATA) {
1586 /* as all packets are kdp_crashdump_pkt_size in length, the last packet
1587 * may end up with trailing bits. make sure that those
1588 * bits aren't confusing. */
1589 if (length < kdp_crashdump_pkt_size) {
1590 kdp_crashdump_short_pkt++;
1591 memset(th->th_data + length, 'Y',
1592 kdp_crashdump_pkt_size - (uint32_t) length);
1593 }
1594
1595 if (!kdp_machine_vm_read((mach_vm_address_t)(uintptr_t)panic_data, (caddr_t) th->th_data, length)) {
1596 uintptr_t next_page = round_page((uintptr_t)panic_data);
1597 memset((caddr_t) th->th_data, 'X', (size_t)length);
1598 if ((next_page - ((uintptr_t) panic_data)) < length) {
1599 uint64_t resid = length - (next_page - (intptr_t) panic_data);
1600 if (!kdp_machine_vm_read((mach_vm_address_t)(uintptr_t)next_page, (caddr_t) th->th_data + (length - resid), resid)) {
1601 memset((caddr_t) th->th_data + (length - resid), 'X', (size_t)resid);
1602 }
1603 }
1604 }
1605 }
1606 else if (request == KDP_SEEK) {
1607 if (kdp_feature_large_crashdumps)
1608 *(uint64_t *) th->th_data = OSSwapHostToBigInt64((*(uint64_t *) panic_data));
1609 else
1610 *(unsigned int *) th->th_data = htonl(*(unsigned int *) panic_data);
1611 }
1612
1613 kdp_send_data(&pkt.data[pkt.off], pkt.len);
1614
1615 /* Listen for the ACK */
1616 RECEIVE_RETRY:
1617 while (!pkt.input && flag_panic_dump_in_progress && poll_count) {
1618 kdp_poll();
1619 poll_count--;
1620 }
1621
1622 if (pkt.input) {
1623
1624 pkt.input = FALSE;
1625
1626 th = (struct corehdr *) &pkt.data[pkt.off];
1627 if (request == KDP_WRQ) {
1628 uint16_t opcode64 = ntohs(th->th_opcode);
1629 uint16_t features64 = (opcode64 & 0xFF00)>>8;
1630 if ((opcode64 & 0xFF) == KDP_ACK) {
1631 kdp_feature_large_crashdumps = features64 & KDP_FEATURE_LARGE_CRASHDUMPS;
1632 if (features64 & KDP_FEATURE_LARGE_PKT_SIZE) {
1633 kdp_feature_large_pkt_size = 1;
1634 }
1635 else {
1636 kdp_feature_large_pkt_size = 0;
1637 kdp_crashdump_pkt_size = 512;
1638 }
1639 printf("Protocol features: 0x%x\n", (uint32_t) features64);
1640 th->th_opcode = htons(KDP_ACK);
1641 }
1642 }
1643 if (ntohs(th->th_opcode) == KDP_ACK && ntohl(th->th_block) == panic_block) {
1644 }
1645 else
1646 if (ntohs(th->th_opcode) == KDP_ERROR) {
1647 printf("Panic server returned error %d, retrying\n", ntohl(th->th_code));
1648 poll_count = 1000;
1649 goto TRANSMIT_RETRY;
1650 }
1651 else
1652 if (ntohl(th->th_block) == (panic_block - 1)) {
1653 printf("RX retry ");
1654 if (++rretries > 1)
1655 goto TRANSMIT_RETRY;
1656 else
1657 goto RECEIVE_RETRY;
1658 }
1659 }
1660 else
1661 if (!flag_panic_dump_in_progress) /* we received a debugging packet, bail*/
1662 {
1663 printf("Received a debugger packet,transferring control to debugger\n");
1664 /* Configure that if not set ..*/
1665 kdp_flag |= DBG_POST_CORE;
1666 return (-2);
1667 }
1668 else /* We timed out */
1669 if (0 == poll_count) {
1670 poll_count = 1000;
1671 kdp_us_spin ((tretries%4) * panic_timeout); /* capped linear backoff */
1672 goto TRANSMIT_RETRY;
1673 }
1674
1675 if (!(++panic_block % SBLOCKSZ)) {
1676 uint64_t ctime;
1677 kdb_printf_unbuffered(".");
1678 ctime = mach_absolute_time();
1679 kdp_superblock_dump_time = ctime - kdp_superblock_dump_start_time;
1680 kdp_superblock_dump_start_time = ctime;
1681 if (kdp_superblock_dump_time > kdp_max_superblock_dump_time)
1682 kdp_max_superblock_dump_time = kdp_superblock_dump_time;
1683 if (kdp_superblock_dump_time < kdp_min_superblock_dump_time)
1684 kdp_min_superblock_dump_time = kdp_superblock_dump_time;
1685 }
1686
1687 if (request == KDP_EOF) {
1688 printf("\nTotal number of packets transmitted: %d\n", panic_block);
1689 printf("Avg. superblock transfer abstime 0x%llx\n", ((mach_absolute_time() - kdp_dump_start_time) / panic_block) * SBLOCKSZ);
1690 printf("Minimum superblock transfer abstime: 0x%llx\n", kdp_min_superblock_dump_time);
1691 printf("Maximum superblock transfer abstime: 0x%llx\n", kdp_max_superblock_dump_time);
1692 }
1693 return 1;
1694 }
1695
1696 static int
1697 isdigit (char c)
1698 {
1699 return ((c > 47) && (c < 58));
1700 }
1701 /* From user mode Libc - this ought to be in a library */
1702 static char *
1703 strnstr(char *s, const char *find, size_t slen)
1704 {
1705 char c, sc;
1706 size_t len;
1707
1708 if ((c = *find++) != '\0') {
1709 len = strlen(find);
1710 do {
1711 do {
1712 if ((sc = *s++) == '\0' || slen-- < 1)
1713 return (NULL);
1714 } while (sc != c);
1715 if (len > slen)
1716 return (NULL);
1717 } while (strncmp(s, find, len) != 0);
1718 s--;
1719 }
1720 return (s);
1721 }
1722
1723 /* Horrid hack to extract xnu version if possible - a much cleaner approach
1724 * would be to have the integrator run a script which would copy the
1725 * xnu version into a string or an int somewhere at project submission
1726 * time - makes assumptions about sizeof(version), but will not fail if
1727 * it changes, but may be incorrect.
1728 */
1729 /* 2006: Incorporated a change from Darwin user P. Lovell to extract
1730 * the minor kernel version numbers from the version string.
1731 */
1732 static int
1733 kdp_get_xnu_version(char *versionbuf)
1734 {
1735 char *versionpos;
1736 char vstr[20];
1737 int retval = -1;
1738 char *vptr;
1739
1740 strlcpy(vstr, "custom", 10);
1741 if (kdp_machine_vm_read((mach_vm_address_t)(uintptr_t)version, versionbuf, 128)) {
1742 versionbuf[127] = '\0';
1743 versionpos = strnstr(versionbuf, "xnu-", 115);
1744 if (versionpos) {
1745 strncpy(vstr, versionpos, sizeof(vstr));
1746 vstr[sizeof(vstr)-1] = '\0';
1747 vptr = vstr + 4; /* Begin after "xnu-" */
1748 while (*vptr && (isdigit(*vptr) || *vptr == '.'))
1749 vptr++;
1750 *vptr = '\0';
1751 /* Remove trailing period, if any */
1752 if (*(--vptr) == '.')
1753 *vptr = '\0';
1754 retval = 0;
1755 }
1756 }
1757 strlcpy(versionbuf, vstr, KDP_MAXPACKET);
1758 return retval;
1759 }
1760
1761 void
1762 kdp_set_dump_info(const uint32_t flags, const char *filename,
1763 const char *destipstr, const char *routeripstr,
1764 const uint32_t port)
1765 {
1766 uint32_t cmd;
1767
1768 if (destipstr && (destipstr[0] != '\0')) {
1769 strlcpy(panicd_ip_str, destipstr, sizeof(panicd_ip_str));
1770 panicd_specified = 1;
1771 }
1772
1773 if (routeripstr && (routeripstr[0] != '\0')) {
1774 strlcpy(router_ip_str, routeripstr, sizeof(router_ip_str));
1775 router_specified = 1;
1776 }
1777
1778 if (filename && (filename[0] != '\0')) {
1779 strlcpy(corename_str, filename, sizeof(corename_str));
1780 corename_specified = TRUE;
1781 } else {
1782 corename_specified = FALSE;
1783 }
1784
1785 if (port)
1786 panicd_port = port;
1787
1788 /* on a disconnect, should we stay in KDP or not? */
1789 noresume_on_disconnect = (flags & KDP_DUMPINFO_NORESUME) ? 1 : 0;
1790
1791 if ((flags & KDP_DUMPINFO_DUMP) == 0)
1792 return;
1793
1794 /* the rest of the commands can modify kdp_flags */
1795 cmd = flags & KDP_DUMPINFO_MASK;
1796 if (cmd == KDP_DUMPINFO_DISABLE) {
1797 kdp_flag &= ~KDP_PANIC_DUMP_ENABLED;
1798 panicd_specified = 0;
1799 kdp_trigger_core_dump = 0;
1800 return;
1801 }
1802
1803 kdp_flag &= ~REBOOT_POST_CORE;
1804 if (flags & KDP_DUMPINFO_REBOOT)
1805 kdp_flag |= REBOOT_POST_CORE;
1806
1807 kdp_flag &= ~PANIC_LOG_DUMP;
1808 if (cmd == KDP_DUMPINFO_PANICLOG)
1809 kdp_flag |= PANIC_LOG_DUMP;
1810
1811 kdp_flag &= ~SYSTEM_LOG_DUMP;
1812 if (cmd == KDP_DUMPINFO_SYSTEMLOG)
1813 kdp_flag |= SYSTEM_LOG_DUMP;
1814
1815 /* trigger a dump */
1816 kdp_flag |= DBG_POST_CORE;
1817
1818 flag_dont_abort_panic_dump = (flags & KDP_DUMPINFO_NOINTR) ?
1819 TRUE : FALSE;
1820
1821 reattach_wait = 1;
1822 logPanicDataToScreen = 1;
1823 disableConsoleOutput = 0;
1824 disable_debug_output = 0;
1825 kdp_trigger_core_dump = 1;
1826 }
1827
1828 void
1829 kdp_get_dump_info(uint32_t *flags, char *filename, char *destipstr,
1830 char *routeripstr, uint32_t *port)
1831 {
1832 if (destipstr) {
1833 if (panicd_specified)
1834 strlcpy(destipstr, panicd_ip_str,
1835 sizeof(panicd_ip_str));
1836 else
1837 destipstr[0] = '\0';
1838 }
1839
1840 if (routeripstr) {
1841 if (router_specified)
1842 strlcpy(routeripstr, router_ip_str,
1843 sizeof(router_ip_str));
1844 else
1845 routeripstr[0] = '\0';
1846 }
1847
1848 if (filename) {
1849 if (corename_specified)
1850 strlcpy(filename, corename_str,
1851 sizeof(corename_str));
1852 else
1853 filename[0] = '\0';
1854
1855 }
1856
1857 if (port)
1858 *port = panicd_port;
1859
1860 if (flags) {
1861 *flags = 0;
1862 if (!panicd_specified)
1863 *flags |= KDP_DUMPINFO_DISABLE;
1864 else if (kdp_flag & PANIC_LOG_DUMP)
1865 *flags |= KDP_DUMPINFO_PANICLOG;
1866 else
1867 *flags |= KDP_DUMPINFO_CORE;
1868
1869 if (noresume_on_disconnect)
1870 *flags |= KDP_DUMPINFO_NORESUME;
1871 }
1872 }
1873
1874
1875 /* Primary dispatch routine for the system dump */
1876 void
1877 kdp_panic_dump(void)
1878 {
1879 char coreprefix[10];
1880 int panic_error;
1881
1882 uint64_t abstime;
1883 uint32_t current_ip = ntohl((uint32_t)kdp_current_ip_address);
1884
1885 if (flag_panic_dump_in_progress) {
1886 kdb_printf("System dump aborted.\n");
1887 goto panic_dump_exit;
1888 }
1889
1890 printf("Entering system dump routine\n");
1891
1892 if (!kdp_en_recv_pkt || !kdp_en_send_pkt) {
1893 kdb_printf("Error: No transport device registered for kernel crashdump\n");
1894 return;
1895 }
1896
1897 if (!panicd_specified) {
1898 kdb_printf("A dump server was not specified in the boot-args, terminating kernel core dump.\n");
1899 goto panic_dump_exit;
1900 }
1901
1902 flag_panic_dump_in_progress = TRUE;
1903 not_in_kdp = 0;
1904
1905 if (pkt.input)
1906 kdp_panic("kdp_panic_dump: unexpected pending input packet");
1907
1908 kdp_get_xnu_version((char *) &pkt.data[0]);
1909
1910 if (!corename_specified) {
1911 /* Panic log bit takes precedence over core dump bit */
1912 if ((panicstr != (char *) 0) && (kdp_flag & PANIC_LOG_DUMP))
1913 strlcpy(coreprefix, "paniclog", sizeof(coreprefix));
1914 else if (kdp_flag & SYSTEM_LOG_DUMP)
1915 strlcpy(coreprefix, "systemlog", sizeof(coreprefix));
1916 else
1917 strlcpy(coreprefix, "core", sizeof(coreprefix));
1918
1919 abstime = mach_absolute_time();
1920 pkt.data[20] = '\0';
1921 snprintf (corename_str, sizeof(corename_str), "%s-%s-%d.%d.%d.%d-%x",
1922 coreprefix, &pkt.data[0],
1923 (current_ip & 0xff000000) >> 24,
1924 (current_ip & 0xff0000) >> 16,
1925 (current_ip & 0xff00) >> 8,
1926 (current_ip & 0xff),
1927 (unsigned int) (abstime & 0xffffffff));
1928 }
1929
1930 if (0 == inet_aton(panicd_ip_str, (struct in_addr *) &panic_server_ip)) {
1931 kdb_printf("inet_aton() failed interpreting %s as a panic server IP\n", panicd_ip_str);
1932 }
1933 else
1934 kdb_printf("Attempting connection to panic server configured at IP %s, port %d\n", panicd_ip_str, panicd_port);
1935
1936 destination_mac = router_mac;
1937
1938 if (kdp_arp_resolve(panic_server_ip, &temp_mac)) {
1939 kdb_printf("Resolved %s's (or proxy's) link level address\n", panicd_ip_str);
1940 destination_mac = temp_mac;
1941 }
1942 else {
1943 if (!flag_panic_dump_in_progress) goto panic_dump_exit;
1944 if (router_specified) {
1945 if (0 == inet_aton(router_ip_str, (struct in_addr *) &parsed_router_ip))
1946 kdb_printf("inet_aton() failed interpreting %s as an IP\n", router_ip_str);
1947 else {
1948 router_ip = parsed_router_ip;
1949 if (kdp_arp_resolve(router_ip, &temp_mac)) {
1950 destination_mac = temp_mac;
1951 kdb_printf("Routing through specified router IP %s (%d)\n", router_ip_str, router_ip);
1952 }
1953 }
1954 }
1955 }
1956
1957 if (!flag_panic_dump_in_progress) goto panic_dump_exit;
1958
1959 kdb_printf("Transmitting packets to link level address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1960 destination_mac.ether_addr_octet[0] & 0xff,
1961 destination_mac.ether_addr_octet[1] & 0xff,
1962 destination_mac.ether_addr_octet[2] & 0xff,
1963 destination_mac.ether_addr_octet[3] & 0xff,
1964 destination_mac.ether_addr_octet[4] & 0xff,
1965 destination_mac.ether_addr_octet[5] & 0xff);
1966
1967 kdb_printf("Kernel map size is %llu\n", (unsigned long long) get_vmmap_size(kernel_map));
1968 kdb_printf("Sending write request for %s\n", corename_str);
1969
1970 if ((panic_error = kdp_send_crashdump_pkt(KDP_WRQ, corename_str, 0 , NULL)) < 0) {
1971 kdb_printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error);
1972 goto panic_dump_exit;
1973 }
1974
1975 /* Just the panic log requested */
1976 if ((panicstr != (char *) 0) && (kdp_flag & PANIC_LOG_DUMP)) {
1977 kdb_printf_unbuffered("Transmitting panic log, please wait: ");
1978 kdp_send_crashdump_data(KDP_DATA, corename_str,
1979 debug_buf_ptr - debug_buf,
1980 debug_buf);
1981 kdp_send_crashdump_pkt (KDP_EOF, NULL, 0, ((void *) 0));
1982 printf("Please file a bug report on this panic, if possible.\n");
1983 goto panic_dump_exit;
1984 }
1985
1986 /* maybe we wanted the systemlog */
1987 if (kdp_flag & SYSTEM_LOG_DUMP) {
1988 long start_off = msgbufp->msg_bufx;
1989 long len;
1990
1991 kdb_printf_unbuffered("Transmitting system log, please wait: ");
1992 if (start_off >= msgbufp->msg_bufr) {
1993 len = msgbufp->msg_size - start_off;
1994 kdp_send_crashdump_data(KDP_DATA, corename_str, len,
1995 msgbufp->msg_bufc + start_off);
1996 /* seek to remove trailing bytes */
1997 kdp_send_crashdump_seek(corename_str, len);
1998 start_off = 0;
1999 }
2000
2001 if (start_off != msgbufp->msg_bufr) {
2002 len = msgbufp->msg_bufr - start_off;
2003 kdp_send_crashdump_data(KDP_DATA, corename_str, len,
2004 msgbufp->msg_bufc + start_off);
2005 }
2006
2007 kdp_send_crashdump_pkt (KDP_EOF, NULL, 0, ((void *) 0));
2008 goto panic_dump_exit;
2009 }
2010
2011 /* We want a core dump if we're here */
2012 kern_dump();
2013
2014 panic_dump_exit:
2015 abort_panic_transfer();
2016 pkt.input = FALSE;
2017 pkt.len = 0;
2018 kdp_reset();
2019 return;
2020 }
2021
2022 void
2023 abort_panic_transfer(void)
2024 {
2025 flag_panic_dump_in_progress = FALSE;
2026 flag_dont_abort_panic_dump = FALSE;
2027 not_in_kdp = 1;
2028 panic_block = 0;
2029 }
2030
2031 #if CONFIG_SERIAL_KDP
2032
2033 static boolean_t needs_serial_init = TRUE;
2034
2035 static void
2036 kdp_serial_send(void *rpkt, unsigned int rpkt_len)
2037 {
2038 // printf("tx\n");
2039 kdp_serialize_packet((unsigned char *)rpkt, rpkt_len, pal_serial_putc);
2040 }
2041
2042 static void
2043 kdp_serial_receive(void *rpkt, unsigned int *rpkt_len, unsigned int timeout)
2044 {
2045 int readkar;
2046 uint64_t now, deadline;
2047
2048 clock_interval_to_deadline(timeout, 1000 * 1000 /* milliseconds */, &deadline);
2049
2050 // printf("rx\n");
2051 for(clock_get_uptime(&now); now < deadline; clock_get_uptime(&now))
2052 {
2053 readkar = pal_serial_getc();
2054 if(readkar >= 0)
2055 {
2056 unsigned char *packet;
2057 // printf("got char %02x\n", readkar);
2058 if((packet = kdp_unserialize_packet(readkar,rpkt_len)))
2059 {
2060 memcpy(rpkt, packet, *rpkt_len);
2061 return;
2062 }
2063 }
2064 }
2065 *rpkt_len = 0;
2066 }
2067
2068 static boolean_t
2069 kdp_serial_setmode(boolean_t active)
2070 {
2071 if (active == FALSE) /* leaving KDP */
2072 return TRUE;
2073
2074 if (!needs_serial_init)
2075 return TRUE;
2076
2077 pal_serial_init();
2078 needs_serial_init = FALSE;
2079 return TRUE;
2080 }
2081
2082
2083 static void kdp_serial_callout(__unused void *arg, kdp_event_t event)
2084 {
2085 /* When we stop KDP, set the bit to re-initialize the console serial port
2086 * the next time we send/receive a KDP packet. We don't do it on
2087 * KDP_EVENT_ENTER directly because it also gets called when we trap to KDP
2088 * for non-external debugging, i.e., stackshot or core dumps.
2089 *
2090 * Set needs_serial_init on exit (and initialization, see above) and not
2091 * enter because enter is sent multiple times and causes excess reinitialization.
2092 */
2093
2094 switch (event)
2095 {
2096 case KDP_EVENT_PANICLOG:
2097 case KDP_EVENT_ENTER:
2098 break;
2099 case KDP_EVENT_EXIT:
2100 needs_serial_init = TRUE;
2101 break;
2102 }
2103 }
2104
2105 #endif /* CONFIG_SERIAL_KDP */
2106
2107 void
2108 kdp_init(void)
2109 {
2110 strlcpy(kdp_kernelversion_string, version, sizeof(kdp_kernelversion_string));
2111
2112 /* Relies on platform layer calling panic_init() before kdp_init() */
2113 if (kernel_uuid[0] != '\0') {
2114 /*
2115 * Update kdp_kernelversion_string with our UUID
2116 * generated at link time.
2117 */
2118
2119 strlcat(kdp_kernelversion_string, "; UUID=", sizeof(kdp_kernelversion_string));
2120 strlcat(kdp_kernelversion_string, kernel_uuid, sizeof(kdp_kernelversion_string));
2121 }
2122
2123 if (debug_boot_arg & DB_REBOOT_POST_CORE)
2124 kdp_flag |= REBOOT_POST_CORE;
2125 #if CONFIG_SERIAL_KDP
2126 char kdpname[80];
2127 struct in_addr ipaddr;
2128 struct ether_addr macaddr;
2129
2130
2131 #if CONFIG_EMBEDDED
2132 //serial will be the debugger, unless match name is explicitly provided, and it's not "serial"
2133 if(PE_parse_boot_argn("kdp_match_name", kdpname, sizeof(kdpname)) && strncmp(kdpname, "serial", sizeof(kdpname)) != 0)
2134 return;
2135 #else
2136 // serial must be explicitly requested
2137 if(!PE_parse_boot_argn("kdp_match_name", kdpname, sizeof(kdpname)) || strncmp(kdpname, "serial", sizeof(kdpname)) != 0)
2138 return;
2139 #endif
2140
2141 kprintf("Initializing serial KDP\n");
2142
2143 kdp_register_callout(kdp_serial_callout, NULL);
2144 kdp_register_link(NULL, kdp_serial_setmode);
2145 kdp_register_send_receive(kdp_serial_send, kdp_serial_receive);
2146
2147 /* fake up an ip and mac for early serial debugging */
2148 macaddr.ether_addr_octet[0] = 's';
2149 macaddr.ether_addr_octet[1] = 'e';
2150 macaddr.ether_addr_octet[2] = 'r';
2151 macaddr.ether_addr_octet[3] = 'i';
2152 macaddr.ether_addr_octet[4] = 'a';
2153 macaddr.ether_addr_octet[5] = 'l';
2154 ipaddr.s_addr = KDP_SERIAL_IPADDR;
2155 kdp_set_ip_and_mac_addresses(&ipaddr, &macaddr);
2156
2157 #endif /* CONFIG_SERIAL_KDP */
2158 }