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