]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kdp/kdp_udp.c
a37f808735b9010561897421cc60acb320991621
[apple/xnu.git] / osfmk / kdp / kdp_udp.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1982, 1986, 1993
27 * The Regents of the University of California. All rights reserved.
28 */
29
30 /*
31 * Kernel Debugging Protocol UDP implementation.
32 */
33
34 #include <mach_kdb.h>
35 #include <mach/boolean.h>
36 #include <mach/mach_types.h>
37 #include <mach/exception_types.h>
38 #include <kern/cpu_data.h>
39 #include <kern/debug.h>
40
41 #include <kdp/kdp_internal.h>
42 #include <kdp/kdp_en_debugger.h>
43 #include <kdp/kdp_udp.h>
44
45 #include <kdp/kdp_core.h>
46
47 #include <vm/vm_map.h>
48 #include <mach/memory_object_types.h>
49
50 #include <string.h>
51
52 #define DO_ALIGN 1 /* align all packet data accesses */
53
54 extern int kdp_getc(void);
55 extern int reattach_wait;
56
57 static u_short ip_id; /* ip packet ctr, for ids */
58
59 /* @(#)udp_usrreq.c 2.2 88/05/23 4.0NFSSRC SMI; from UCB 7.1 6/5/86 */
60
61 /*
62 * UDP protocol implementation.
63 * Per RFC 768, August, 1980.
64 */
65 #define UDP_TTL 60 /* deflt time to live for UDP packets */
66 int udp_ttl = UDP_TTL;
67 static unsigned char exception_seq;
68
69 static struct {
70 unsigned char data[KDP_MAXPACKET];
71 unsigned int off, len;
72 boolean_t input;
73 } pkt, saved_reply;
74
75 struct {
76 struct {
77 struct in_addr in;
78 struct ether_addr ea;
79 } loc;
80 struct {
81 struct in_addr in;
82 struct ether_addr ea;
83 } rmt;
84 } adr;
85
86 static char
87 *exception_message[] = {
88 "Unknown",
89 "Memory access", /* EXC_BAD_ACCESS */
90 "Failed instruction", /* EXC_BAD_INSTRUCTION */
91 "Arithmetic", /* EXC_ARITHMETIC */
92 "Emulation", /* EXC_EMULATION */
93 "Software", /* EXC_SOFTWARE */
94 "Breakpoint" /* EXC_BREAKPOINT */
95 };
96
97 int kdp_flag = 0;
98
99 static kdp_send_t kdp_en_send_pkt = 0;
100 static kdp_receive_t kdp_en_recv_pkt = 0;
101
102
103 static u_long kdp_current_ip_address = 0;
104 static struct ether_addr kdp_current_mac_address = {{0, 0, 0, 0, 0, 0}};
105
106 static void kdp_handler( void *);
107
108 static unsigned int panic_server_ip = 0;
109 static unsigned int parsed_router_ip = 0;
110 static unsigned int router_ip = 0;
111 static unsigned int panicd_specified = 0;
112 static unsigned int router_specified = 0;
113
114 static struct ether_addr router_mac = {{0, 0, 0 , 0, 0, 0}};
115
116 static u_char flag_panic_dump_in_progress = 0;
117 static u_char flag_router_mac_initialized = 0;
118
119 static unsigned int panic_timeout = 100000;
120 static unsigned int last_panic_port = CORE_REMOTE_PORT;
121
122 unsigned int SEGSIZE = 512;
123
124 static unsigned int PANIC_PKTSIZE = 518;
125 static char panicd_ip_str[20];
126 static char router_ip_str[20];
127
128 static unsigned int panic_block = 0;
129 static volatile unsigned int kdp_trigger_core_dump = 0;
130
131 extern unsigned int not_in_kdp;
132
133 void
134 kdp_register_send_receive(
135 kdp_send_t send,
136 kdp_receive_t receive)
137 {
138 unsigned int debug;
139
140 kdp_en_send_pkt = send;
141 kdp_en_recv_pkt = receive;
142
143 debug_log_init();
144
145 PE_parse_boot_arg("debug", &debug);
146
147 if (debug & DB_KDP_BP_DIS)
148 kdp_flag |= KDP_BP_DIS;
149 if (debug & DB_KDP_GETC_ENA)
150 kdp_flag |= KDP_GETC_ENA;
151 if (debug & DB_ARP)
152 kdp_flag |= KDP_ARP;
153
154 if (debug & DB_KERN_DUMP_ON_PANIC)
155 kdp_flag |= KDP_PANIC_DUMP_ENABLED;
156 if (debug & DB_KERN_DUMP_ON_NMI)
157 kdp_flag |= PANIC_CORE_ON_NMI;
158
159 if (debug & DB_DBG_POST_CORE)
160 kdp_flag |= DBG_POST_CORE;
161
162 if (debug & DB_PANICLOG_DUMP)
163 kdp_flag |= PANIC_LOG_DUMP;
164
165 if (PE_parse_boot_arg ("_panicd_ip", panicd_ip_str))
166 panicd_specified = 1;
167 /* For the future, currently non-functional */
168 if (PE_parse_boot_arg ("_router_ip", router_ip_str))
169 router_specified = 1;
170
171 kdp_flag |= KDP_READY;
172 if (current_debugger == NO_CUR_DB)
173 current_debugger = KDP_CUR_DB;
174 if (halt_in_debugger) {
175 kdp_call();
176 halt_in_debugger=0;
177 }
178 }
179
180 void
181 kdp_unregister_send_receive(
182 kdp_send_t send,
183 kdp_receive_t receive)
184 {
185 if (current_debugger == KDP_CUR_DB)
186 current_debugger = NO_CUR_DB;
187 kdp_flag &= ~KDP_READY;
188 kdp_en_send_pkt = NULL;
189 kdp_en_recv_pkt = NULL;
190 }
191
192 static void
193 enaddr_copy(
194 void *src,
195 void *dst
196 )
197 {
198 bcopy((char *)src, (char *)dst, sizeof (struct ether_addr));
199 }
200
201 static unsigned short
202 ip_sum(
203 unsigned char *c,
204 unsigned int hlen
205 )
206 {
207 unsigned int high, low, sum;
208
209 high = low = 0;
210 while (hlen-- > 0) {
211 low += c[1] + c[3];
212 high += c[0] + c[2];
213
214 c += sizeof (int);
215 }
216
217 sum = (high << 8) + low;
218 sum = (sum >> 16) + (sum & 65535);
219
220 return (sum > 65535 ? sum - 65535 : sum);
221 }
222
223 static void
224 kdp_reply(
225 unsigned short reply_port
226 )
227 {
228 struct udpiphdr aligned_ui, *ui = &aligned_ui;
229 struct ip aligned_ip, *ip = &aligned_ip;
230 struct in_addr tmp_ipaddr;
231 struct ether_addr tmp_enaddr;
232 struct ether_header *eh;
233
234 if (!pkt.input)
235 kdp_panic("kdp_reply");
236
237 pkt.off -= sizeof (struct udpiphdr);
238
239 #if DO_ALIGN
240 bcopy((char *)&pkt.data[pkt.off], (char *)ui, sizeof(*ui));
241 #else
242 ui = (struct udpiphdr *)&pkt.data[pkt.off];
243 #endif
244 ui->ui_next = ui->ui_prev = 0;
245 ui->ui_x1 = 0;
246 ui->ui_pr = IPPROTO_UDP;
247 ui->ui_len = htons((u_short)pkt.len + sizeof (struct udphdr));
248 tmp_ipaddr = ui->ui_src;
249 ui->ui_src = ui->ui_dst;
250 ui->ui_dst = tmp_ipaddr;
251 ui->ui_sport = htons(KDP_REMOTE_PORT);
252 ui->ui_dport = reply_port;
253 ui->ui_ulen = ui->ui_len;
254 ui->ui_sum = 0;
255 #if DO_ALIGN
256 bcopy((char *)ui, (char *)&pkt.data[pkt.off], sizeof(*ui));
257 bcopy((char *)&pkt.data[pkt.off], (char *)ip, sizeof(*ip));
258 #else
259 ip = (struct ip *)&pkt.data[pkt.off];
260 #endif
261 ip->ip_len = htons(sizeof (struct udpiphdr) + pkt.len);
262 ip->ip_v = IPVERSION;
263 ip->ip_id = htons(ip_id++);
264 ip->ip_hl = sizeof (struct ip) >> 2;
265 ip->ip_ttl = udp_ttl;
266 ip->ip_sum = 0;
267 ip->ip_sum = htons(~ip_sum((unsigned char *)ip, ip->ip_hl));
268 #if DO_ALIGN
269 bcopy((char *)ip, (char *)&pkt.data[pkt.off], sizeof(*ip));
270 #endif
271
272 pkt.len += sizeof (struct udpiphdr);
273
274 pkt.off -= sizeof (struct ether_header);
275
276 eh = (struct ether_header *)&pkt.data[pkt.off];
277 enaddr_copy(eh->ether_shost, &tmp_enaddr);
278 enaddr_copy(eh->ether_dhost, eh->ether_shost);
279 enaddr_copy(&tmp_enaddr, eh->ether_dhost);
280 eh->ether_type = htons(ETHERTYPE_IP);
281
282 pkt.len += sizeof (struct ether_header);
283
284 // save reply for possible retransmission
285 bcopy((char *)&pkt, (char *)&saved_reply, sizeof(pkt));
286
287 (*kdp_en_send_pkt)(&pkt.data[pkt.off], pkt.len);
288
289 // increment expected sequence number
290 exception_seq++;
291 }
292
293 static void
294 kdp_send(
295 unsigned short remote_port
296 )
297 {
298 struct udpiphdr aligned_ui, *ui = &aligned_ui;
299 struct ip aligned_ip, *ip = &aligned_ip;
300 struct ether_header *eh;
301
302 if (pkt.input)
303 kdp_panic("kdp_send");
304
305 pkt.off -= sizeof (struct udpiphdr);
306
307 #if DO_ALIGN
308 bcopy((char *)&pkt.data[pkt.off], (char *)ui, sizeof(*ui));
309 #else
310 ui = (struct udpiphdr *)&pkt.data[pkt.off];
311 #endif
312 ui->ui_next = ui->ui_prev = 0;
313 ui->ui_x1 = 0;
314 ui->ui_pr = IPPROTO_UDP;
315 ui->ui_len = htons((u_short)pkt.len + sizeof (struct udphdr));
316 ui->ui_src = adr.loc.in;
317 ui->ui_dst = adr.rmt.in;
318 ui->ui_sport = htons(KDP_REMOTE_PORT);
319 ui->ui_dport = remote_port;
320 ui->ui_ulen = ui->ui_len;
321 ui->ui_sum = 0;
322 #if DO_ALIGN
323 bcopy((char *)ui, (char *)&pkt.data[pkt.off], sizeof(*ui));
324 bcopy((char *)&pkt.data[pkt.off], (char *)ip, sizeof(*ip));
325 #else
326 ip = (struct ip *)&pkt.data[pkt.off];
327 #endif
328 ip->ip_len = htons(sizeof (struct udpiphdr) + pkt.len);
329 ip->ip_v = IPVERSION;
330 ip->ip_id = htons(ip_id++);
331 ip->ip_hl = sizeof (struct ip) >> 2;
332 ip->ip_ttl = udp_ttl;
333 ip->ip_sum = 0;
334 ip->ip_sum = htons(~ip_sum((unsigned char *)ip, ip->ip_hl));
335 #if DO_ALIGN
336 bcopy((char *)ip, (char *)&pkt.data[pkt.off], sizeof(*ip));
337 #endif
338
339 pkt.len += sizeof (struct udpiphdr);
340
341 pkt.off -= sizeof (struct ether_header);
342
343 eh = (struct ether_header *)&pkt.data[pkt.off];
344 enaddr_copy(&adr.loc.ea, eh->ether_shost);
345 enaddr_copy(&adr.rmt.ea, eh->ether_dhost);
346 eh->ether_type = htons(ETHERTYPE_IP);
347
348 pkt.len += sizeof (struct ether_header);
349 (*kdp_en_send_pkt)(&pkt.data[pkt.off], pkt.len);
350 }
351
352
353 void
354 kdp_set_ip_and_mac_addresses(
355 struct in_addr *ipaddr,
356 struct ether_addr *macaddr)
357 {
358 unsigned int debug = 0;
359
360 kdp_current_ip_address = ipaddr->s_addr;
361 kdp_current_mac_address = *macaddr;
362 }
363
364 void
365 kdp_set_gateway_mac(void *gatewaymac)
366 {
367 router_mac = *(struct ether_addr *)gatewaymac;
368 }
369
370 struct ether_addr
371 kdp_get_mac_addr(void)
372 {
373 return kdp_current_mac_address;
374 }
375
376 unsigned int
377 kdp_get_ip_address(void)
378 {
379 return kdp_current_ip_address;
380 }
381
382 /* ARP responses are enabled when the DB_ARP bit of the debug boot arg
383 is set. A workaround if you don't want to reboot is to set
384 kdpDEBUGFlag &= DB_ARP when connected (but that certainly isn't a published
385 interface!)
386 */
387 static void
388 kdp_arp_reply(void)
389 {
390 struct ether_header *eh;
391 struct ether_arp aligned_ea, *ea = &aligned_ea;
392
393 struct in_addr isaddr, itaddr, myaddr;
394 struct ether_addr my_enaddr;
395
396 eh = (struct ether_header *)&pkt.data[pkt.off];
397 pkt.off += sizeof(struct ether_header);
398
399 memcpy((void *)ea, (void *)&pkt.data[pkt.off],sizeof(*ea));
400
401 if(ntohs(ea->arp_op) != ARPOP_REQUEST)
402 return;
403
404 myaddr.s_addr = kdp_get_ip_address();
405 my_enaddr = kdp_get_mac_addr();
406
407 if (!(myaddr.s_addr) || !(my_enaddr.ether_addr_octet[1]))
408 return;
409
410 (void)memcpy((void *)&isaddr, (void *)ea->arp_spa, sizeof (isaddr));
411 (void)memcpy((void *)&itaddr, (void *)ea->arp_tpa, sizeof (itaddr));
412
413 if (itaddr.s_addr == myaddr.s_addr) {
414 (void)memcpy((void *)ea->arp_tha, (void *)ea->arp_sha, sizeof(ea->arp_sha));
415 (void)memcpy((void *)ea->arp_sha, (void *)&my_enaddr, sizeof(ea->arp_sha));
416
417 (void)memcpy((void *)ea->arp_tpa, (void *) ea->arp_spa, sizeof(ea->arp_spa));
418 (void)memcpy((void *)ea->arp_spa, (void *) &itaddr, sizeof(ea->arp_spa));
419
420 ea->arp_op = htons(ARPOP_REPLY);
421 ea->arp_pro = htons(ETHERTYPE_IP);
422 (void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost));
423 (void)memcpy(eh->ether_shost, &my_enaddr, sizeof(eh->ether_shost));
424 eh->ether_type = htons(ETHERTYPE_ARP);
425 (void)memcpy(&pkt.data[pkt.off], ea, sizeof(*ea));
426 pkt.off -= sizeof (struct ether_header);
427 /* pkt.len is still the length we want, ether_header+ether_arp */
428 (*kdp_en_send_pkt)(&pkt.data[pkt.off], pkt.len);
429 }
430 }
431
432 static void
433 kdp_poll(void)
434 {
435 struct ether_header *eh;
436 struct udpiphdr aligned_ui, *ui = &aligned_ui;
437 struct ip aligned_ip, *ip = &aligned_ip;
438 static int msg_printed;
439
440
441 if (pkt.input)
442 kdp_panic("kdp_poll");
443
444 if (!kdp_en_recv_pkt || !kdp_en_send_pkt) {
445 if( msg_printed == 0) {
446 msg_printed = 1;
447 printf("kdp_poll: no debugger device\n");
448 }
449 return;
450 }
451
452 pkt.off = pkt.len = 0;
453 (*kdp_en_recv_pkt)(pkt.data, &pkt.len, 3/* ms */);
454
455 if (pkt.len == 0)
456 return;
457
458 if (pkt.len >= sizeof(struct ether_header))
459 {
460 eh = (struct ether_header *)&pkt.data[pkt.off];
461
462 if (kdp_flag & KDP_ARP)
463 {
464 if (ntohs(eh->ether_type) == ETHERTYPE_ARP)
465 {
466 kdp_arp_reply();
467 return;
468 }
469 }
470 }
471
472 if (pkt.len < (sizeof (struct ether_header) + sizeof (struct udpiphdr)))
473 return;
474
475 pkt.off += sizeof (struct ether_header);
476 if (ntohs(eh->ether_type) != ETHERTYPE_IP) {
477 return;
478 }
479
480 #if DO_ALIGN
481 bcopy((char *)&pkt.data[pkt.off], (char *)ui, sizeof(*ui));
482 bcopy((char *)&pkt.data[pkt.off], (char *)ip, sizeof(*ip));
483 #else
484 ui = (struct udpiphdr *)&pkt.data[pkt.off];
485 ip = (struct ip *)&pkt.data[pkt.off];
486 #endif
487
488 pkt.off += sizeof (struct udpiphdr);
489 if (ui->ui_pr != IPPROTO_UDP) {
490 return;
491 }
492
493 if (ip->ip_hl > (sizeof (struct ip) >> 2)) {
494 return;
495 }
496
497 if (ntohs(ui->ui_dport) != KDP_REMOTE_PORT) {
498 if (CORE_REMOTE_PORT == (ntohs(ui->ui_dport)) &&
499 flag_panic_dump_in_progress) {
500 last_panic_port = ui->ui_sport;
501 }
502 else
503 return;
504 }
505 /* If we receive a kernel debugging packet whilst a
506 * core dump is in progress, abort the transfer and
507 * enter the debugger.
508 */
509 else
510 if (flag_panic_dump_in_progress)
511 {
512 abort_panic_transfer();
513 return;
514 }
515
516 if (!kdp.is_conn && !flag_panic_dump_in_progress) {
517 enaddr_copy(eh->ether_dhost, &adr.loc.ea);
518 adr.loc.in = ui->ui_dst;
519
520 enaddr_copy(eh->ether_shost, &adr.rmt.ea);
521 adr.rmt.in = ui->ui_src;
522 }
523
524 /*
525 * Calculate kdp packet length.
526 */
527 pkt.len = ntohs((u_short)ui->ui_ulen) - sizeof (struct udphdr);
528 pkt.input = TRUE;
529 }
530
531 static void
532 kdp_handler(
533 void *saved_state
534 )
535 {
536 unsigned short reply_port;
537 kdp_hdr_t aligned_hdr, *hdr = &aligned_hdr;
538
539
540 kdp.saved_state = saved_state; // see comment in kdp_raise_exception
541
542 do {
543 while (!pkt.input)
544 kdp_poll();
545
546 #if DO_ALIGN
547 bcopy((char *)&pkt.data[pkt.off], (char *)hdr, sizeof(*hdr));
548 #else
549 hdr = (kdp_hdr_t *)&pkt.data[pkt.off];
550 #endif
551
552 // ignore replies -- we're not expecting them anyway.
553 if (hdr->is_reply) {
554 goto again;
555 }
556
557 if (hdr->request == KDP_REATTACH)
558 exception_seq = hdr->seq;
559
560 // check for retransmitted request
561 if (hdr->seq == (exception_seq - 1)) {
562 /* retransmit last reply */
563 (*kdp_en_send_pkt)(&saved_reply.data[saved_reply.off],
564 saved_reply.len);
565 goto again;
566 } else if (hdr->seq != exception_seq) {
567 printf("kdp: bad sequence %d (want %d)\n",
568 hdr->seq, exception_seq);
569 goto again;
570 }
571
572 if (kdp_packet((unsigned char*)&pkt.data[pkt.off],
573 (int *)&pkt.len,
574 (unsigned short *)&reply_port)) {
575 kdp_reply(reply_port);
576 }
577
578 again:
579 pkt.input = FALSE;
580 } while (kdp.is_halted);
581 }
582
583 static void
584 kdp_connection_wait(void)
585 {
586 unsigned short reply_port;
587 boolean_t kdp_call_kdb();
588 struct ether_addr kdp_mac_addr = kdp_get_mac_addr();
589 unsigned int ip_addr = ntohl(kdp_get_ip_address());
590
591 printf( "ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
592 kdp_mac_addr.ether_addr_octet[0] & 0xff,
593 kdp_mac_addr.ether_addr_octet[1] & 0xff,
594 kdp_mac_addr.ether_addr_octet[2] & 0xff,
595 kdp_mac_addr.ether_addr_octet[3] & 0xff,
596 kdp_mac_addr.ether_addr_octet[4] & 0xff,
597 kdp_mac_addr.ether_addr_octet[5] & 0xff);
598
599 printf( "ip address: %d.%d.%d.%d\n",
600 (ip_addr & 0xff000000) >> 24,
601 (ip_addr & 0xff0000) >> 16,
602 (ip_addr & 0xff00) >> 8,
603 (ip_addr & 0xff));
604
605 printf("\nWaiting for remote debugger connection.\n");
606
607 if (reattach_wait == 0) {
608 if((kdp_flag & KDP_GETC_ENA) && (0 != kdp_getc()))
609 {
610 printf("Options..... Type\n");
611 printf("------------ ----\n");
612 printf("continue.... 'c'\n");
613 printf("reboot...... 'r'\n");
614 #if MACH_KDB
615 printf("enter kdb... 'k'\n");
616 #endif
617 }
618 } else
619 reattach_wait = 0;
620
621 exception_seq = 0;
622
623 do {
624 kdp_hdr_t aligned_hdr, *hdr = &aligned_hdr;
625
626 while (!pkt.input) {
627 if (kdp_flag & KDP_GETC_ENA) {
628 switch(kdp_getc()) {
629 case 'c':
630 printf("Continuing...\n");
631 return;
632 case 'r':
633 printf("Rebooting...\n");
634 kdp_reboot();
635 break;
636 #if MACH_KDB
637 case 'k':
638 printf("calling kdb...\n");
639 if (kdp_call_kdb())
640 return;
641 else
642 printf("not implemented...\n");
643 #endif
644 default:
645 break;
646 }
647 }
648 kdp_poll();
649 }
650
651 #if DO_ALIGN
652 bcopy((char *)&pkt.data[pkt.off], (char *)hdr, sizeof(*hdr));
653 #else
654 hdr = (kdp_hdr_t *)&pkt.data[pkt.off];
655 #endif
656 if (hdr->request == KDP_HOSTREBOOT) {
657 kdp_reboot();
658 /* should not return! */
659 }
660 if (((hdr->request == KDP_CONNECT) || (hdr->request == KDP_REATTACH)) &&
661 !hdr->is_reply && (hdr->seq == exception_seq)) {
662 if (kdp_packet((unsigned char *)&pkt.data[pkt.off],
663 (int *)&pkt.len,
664 (unsigned short *)&reply_port))
665 kdp_reply(reply_port);
666 if (hdr->request == KDP_REATTACH) {
667 reattach_wait = 0;
668 hdr->request=KDP_DISCONNECT;
669 exception_seq = 0;
670 }
671 }
672
673 pkt.input = FALSE;
674 } while (!kdp.is_conn);
675
676 if (current_debugger == KDP_CUR_DB)
677 active_debugger=1;
678 printf("Connected to remote debugger.\n");
679 }
680
681 static void
682 kdp_send_exception(
683 unsigned int exception,
684 unsigned int code,
685 unsigned int subcode
686 )
687 {
688 unsigned short remote_port;
689 unsigned int timeout_count = 100;
690 unsigned int poll_timeout;
691
692 do {
693 pkt.off = sizeof (struct ether_header) + sizeof (struct udpiphdr);
694 kdp_exception((unsigned char *)&pkt.data[pkt.off],
695 (int *)&pkt.len,
696 (unsigned short *)&remote_port,
697 (unsigned int)exception,
698 (unsigned int)code,
699 (unsigned int)subcode);
700
701 kdp_send(remote_port);
702
703 poll_timeout = 50;
704 while(!pkt.input && poll_timeout)
705 {
706 kdp_poll();
707 poll_timeout--;
708 }
709
710 if (pkt.input) {
711 if (!kdp_exception_ack(&pkt.data[pkt.off], pkt.len)) {
712 pkt.input = FALSE;
713 }
714 }
715
716 pkt.input = FALSE;
717
718 if (kdp.exception_ack_needed)
719 kdp_us_spin(250000);
720
721 } while (kdp.exception_ack_needed && timeout_count--);
722
723 if (kdp.exception_ack_needed) {
724 // give up & disconnect
725 printf("kdp: exception ack timeout\n");
726 if (current_debugger == KDP_CUR_DB)
727 active_debugger=0;
728 kdp_reset();
729 }
730 }
731
732 void
733 kdp_raise_exception(
734 unsigned int exception,
735 unsigned int code,
736 unsigned int subcode,
737 void *saved_state
738 )
739 {
740 int index;
741
742 extern unsigned int disableDebugOuput;
743 extern unsigned int disableConsoleOutput;
744
745 disable_preemption();
746
747 if (saved_state == 0)
748 printf("kdp_raise_exception with NULL state\n");
749
750 index = exception;
751 if (exception != EXC_BREAKPOINT) {
752 if (exception > EXC_BREAKPOINT || exception < EXC_BAD_ACCESS) {
753 index = 0;
754 }
755 printf("%s exception (%x,%x,%x)\n",
756 exception_message[index],
757 exception, code, subcode);
758 }
759
760 kdp_sync_cache();
761
762 /* XXX WMG it seems that sometimes it doesn't work to let kdp_handler
763 * do this. I think the client and the host can get out of sync.
764 */
765 kdp.saved_state = saved_state;
766
767 if (pkt.input)
768 kdp_panic("kdp_raise_exception");
769
770 if (((kdp_flag & KDP_PANIC_DUMP_ENABLED) || (kdp_flag & PANIC_LOG_DUMP))
771 && (panicstr != (char *) 0)) {
772
773 kdp_panic_dump();
774
775 }
776 else
777 if ((kdp_flag & PANIC_CORE_ON_NMI) && (panicstr == (char *) 0) &&
778 !kdp.is_conn) {
779
780 disableDebugOuput = disableConsoleOutput = FALSE;
781 kdp_panic_dump();
782
783 if (!(kdp_flag & DBG_POST_CORE))
784 goto exit_raise_exception;
785 }
786
787 again:
788 if (!kdp.is_conn)
789 kdp_connection_wait();
790 else {
791 kdp_send_exception(exception, code, subcode);
792 if (kdp.exception_ack_needed) {
793 kdp.exception_ack_needed = FALSE;
794 kdp_remove_all_breakpoints();
795 printf("Remote debugger disconnected.\n");
796 }
797 }
798
799 if (kdp.is_conn) {
800 kdp.is_halted = TRUE; /* XXX */
801 kdp_handler(saved_state);
802 if (!kdp.is_conn)
803 {
804 kdp_remove_all_breakpoints();
805 printf("Remote debugger disconnected.\n");
806 }
807 }
808 /* Allow triggering a panic core dump when connected to the machine
809 * Continuing after setting kdp_trigger_core_dump should do the
810 * trick.
811 */
812 if (1 == kdp_trigger_core_dump) {
813 kdp_flag &= ~PANIC_LOG_DUMP;
814 kdp_flag |= KDP_PANIC_DUMP_ENABLED;
815 kdp_panic_dump();
816 }
817
818 kdp_sync_cache();
819
820 if (reattach_wait == 1)
821 goto again;
822 exit_raise_exception:
823 enable_preemption();
824 }
825
826 void
827 kdp_reset(void)
828 {
829 kdp.reply_port = kdp.exception_port = 0;
830 kdp.is_halted = kdp.is_conn = FALSE;
831 kdp.exception_seq = kdp.conn_seq = 0;
832 }
833
834 struct corehdr *
835 create_panic_header(unsigned int request, const char *corename,
836 unsigned length, unsigned int block)
837 {
838 struct udpiphdr aligned_ui, *ui = &aligned_ui;
839 struct ip aligned_ip, *ip = &aligned_ip;
840 struct ether_header *eh;
841 struct corehdr *coreh;
842 const char *mode = "octet";
843 char modelen = strlen(mode);
844
845 pkt.off = sizeof (struct ether_header);
846 pkt.len = length + ((request == KDP_WRQ) ? modelen : 0) +
847 (corename ? strlen(corename): 0) + sizeof(struct corehdr);
848
849 #if DO_ALIGN
850 bcopy((char *)&pkt.data[pkt.off], (char *)ui, sizeof(*ui));
851 #else
852 ui = (struct udpiphdr *)&pkt.data[pkt.off];
853 #endif
854 ui->ui_next = ui->ui_prev = 0;
855 ui->ui_x1 = 0;
856 ui->ui_pr = IPPROTO_UDP;
857 ui->ui_len = htons((u_short)pkt.len + sizeof (struct udphdr));
858 ui->ui_src.s_addr = htonl(kdp_current_ip_address);
859 ui->ui_dst.s_addr = panic_server_ip;
860 ui->ui_sport = htons(CORE_REMOTE_PORT);
861 ui->ui_dport = ((request == KDP_WRQ) ? htons(CORE_REMOTE_PORT) : last_panic_port);
862 ui->ui_ulen = ui->ui_len;
863 ui->ui_sum = 0;
864 #if DO_ALIGN
865 bcopy((char *)ui, (char *)&pkt.data[pkt.off], sizeof(*ui));
866 bcopy((char *)&pkt.data[pkt.off], (char *)ip, sizeof(*ip));
867 #else
868 ip = (struct ip *)&pkt.data[pkt.off];
869 #endif
870 ip->ip_len = htons(sizeof (struct udpiphdr) + pkt.len);
871 ip->ip_v = IPVERSION;
872 ip->ip_id = htons(ip_id++);
873 ip->ip_hl = sizeof (struct ip) >> 2;
874 ip->ip_ttl = udp_ttl;
875 ip->ip_sum = 0;
876 ip->ip_sum = htons(~ip_sum((unsigned char *)ip, ip->ip_hl));
877 #if DO_ALIGN
878 bcopy((char *)ip, (char *)&pkt.data[pkt.off], sizeof(*ip));
879 #endif
880
881 pkt.len += sizeof (struct udpiphdr);
882
883 pkt.off += sizeof (struct udpiphdr);
884
885 coreh = (struct corehdr *) &pkt.data[pkt.off];
886 coreh->th_opcode = htons((u_short)request);
887
888 if (request == KDP_WRQ)
889 {
890 register char *cp;
891
892 cp = coreh->th_u.tu_rpl;
893 strcpy (cp, corename);
894 cp += strlen(corename);
895 *cp++ = '\0';
896 strcpy (cp, mode);
897 cp+= modelen;
898 *cp++ = '\0';
899 }
900 else
901 {
902 coreh->th_block = htonl((unsigned int) block);
903 }
904
905 pkt.off -= sizeof (struct udpiphdr);
906 pkt.off -= sizeof (struct ether_header);
907
908 eh = (struct ether_header *)&pkt.data[pkt.off];
909 enaddr_copy(&kdp_current_mac_address, eh->ether_shost);
910 enaddr_copy(&router_mac, eh->ether_dhost);
911 eh->ether_type = htons(ETHERTYPE_IP);
912
913 pkt.len += sizeof (struct ether_header);
914 return coreh;
915 }
916
917 int kdp_send_panic_packets (unsigned int request, char *corename,
918 unsigned int length, unsigned int txstart)
919 {
920 unsigned int txend = txstart + length;
921 int panic_error = 0;
922
923 if (length <= SEGSIZE) {
924 if ((panic_error = kdp_send_panic_pkt (request, corename, length, (caddr_t) txstart)) < 0) {
925 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error);
926 return panic_error ;
927 }
928 }
929 else
930 {
931 while (txstart <= (txend - SEGSIZE)) {
932 if ((panic_error = kdp_send_panic_pkt (KDP_DATA, NULL, SEGSIZE, (caddr_t) txstart)) < 0) {
933 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error);
934 return panic_error;
935 }
936 txstart += SEGSIZE;
937 if (!(panic_block % 2000))
938 printf(".");
939 }
940 if (txstart < txend) {
941 kdp_send_panic_pkt(request, corename, (txend - txstart), (caddr_t) txstart);
942 }
943 }
944 }
945
946 int
947 kdp_send_panic_pkt (unsigned int request, char *corename,
948 unsigned int length, void *panic_data)
949 {
950 struct corehdr *th = NULL;
951 int poll_count = 2500;
952
953 char rretries = 0, tretries = 0;
954 /*
955 extern signed long gIODebuggerSemaphore;
956 */
957 pkt.off = pkt.len = 0;
958
959 if (request == KDP_WRQ) /* longer timeout for initial request */
960 poll_count += 1000;
961
962 TRANSMIT_RETRY:
963 tretries++;
964
965 if (tretries > 2)
966 printf("TX retry #%d ", tretries );
967
968 if (tretries >=15) {
969 /* This iokit layer issue can potentially
970 *cause a hang, uncomment to check if it's happening.
971 */
972 /*
973 if (gIODebuggerSemaphore)
974 printf("The gIODebuggerSemaphore is raised, preventing packet transmission (2760413)\n");
975 */
976
977 printf ("Cannot contact panic server, timing out.\n");
978 return (-3);
979 }
980
981 th = create_panic_header(request, corename, length, panic_block);
982
983 if (request == KDP_DATA || request == KDP_SEEK) {
984 if (!kdp_vm_read ((caddr_t) panic_data, (caddr_t) th->th_data, length)) {
985 memset ((caddr_t) th->th_data, 'X', length);
986 }
987 }
988
989 (*kdp_en_send_pkt)(&pkt.data[pkt.off], pkt.len);
990
991 /* Now we have to listen for the ACK */
992 RECEIVE_RETRY:
993
994 while (!pkt.input && flag_panic_dump_in_progress && poll_count) {
995 kdp_poll();
996 poll_count--;
997 }
998
999 if (pkt.input) {
1000
1001 pkt.input = FALSE;
1002
1003 th = (struct corehdr *) &pkt.data[pkt.off];
1004 /* These will eventually have to be ntoh[ls]'ed as appropriate */
1005
1006 if (th->th_opcode == KDP_ACK && th->th_block == panic_block) {
1007 }
1008 else
1009 if (th->th_opcode == KDP_ERROR) {
1010 printf("Panic server returned error %d, retrying\n", th->th_code);
1011 poll_count = 1000;
1012 goto TRANSMIT_RETRY;
1013 }
1014 else
1015 if (th->th_block == (panic_block -1)) {
1016 printf("RX retry ");
1017 if (++rretries > 1)
1018 goto TRANSMIT_RETRY;
1019 else
1020 goto RECEIVE_RETRY;
1021 }
1022 }
1023 else
1024 if (!flag_panic_dump_in_progress) /* we received a debugging packet, bail*/
1025 {
1026 printf("Received a debugger packet,transferring control to debugger\n");
1027 /* Configure that if not set ..*/
1028 kdp_flag |= DBG_POST_CORE;
1029 return (-2);
1030 }
1031 else /* We timed out */
1032 if (0 == poll_count) {
1033 poll_count = 1000;
1034 kdp_us_spin ((tretries%4) * panic_timeout); /* capped linear backoff */
1035 goto TRANSMIT_RETRY;
1036 }
1037
1038 panic_block++;
1039
1040 if (request == KDP_EOF)
1041 printf ("\nTotal number of packets transmitted: %d\n", panic_block);
1042
1043 return 1;
1044 }
1045
1046 /* Since we don't seem to have an isdigit() .. */
1047 static int
1048 isdigit (char c)
1049 {
1050 return ((c > 47) && (c < 58));
1051 }
1052 /* From user mode Libc - this ought to be in a library */
1053 static char *
1054 strnstr(s, find, slen)
1055 const char *s;
1056 const char *find;
1057 size_t slen;
1058 {
1059 char c, sc;
1060 size_t len;
1061
1062 if ((c = *find++) != '\0') {
1063 len = strlen(find);
1064 do {
1065 do {
1066 if ((sc = *s++) == '\0' || slen-- < 1)
1067 return (NULL);
1068 } while (sc != c);
1069 if (len > slen)
1070 return (NULL);
1071 } while (strncmp(s, find, len) != 0);
1072 s--;
1073 }
1074 return ((char *)s);
1075 }
1076
1077 /* Horrid hack to extract xnu version if possible - a much cleaner approach
1078 * would be to have the integrator run a script which would copy the
1079 * xnu version into a string or an int somewhere at project submission
1080 * time - makes assumptions about sizeof(version), but will not fail if
1081 * it changes, but may be incorrect.
1082 */
1083
1084 static int
1085 kdp_get_xnu_version(char *versionbuf)
1086 {
1087 extern const char version[];
1088 char *versionpos;
1089 char vstr[10];
1090 int retval = -1;
1091
1092 strcpy(vstr, "custom");
1093 if (version) {
1094 if (kdp_vm_read(version, versionbuf, 90)) {
1095
1096 versionbuf[89] = '\0';
1097
1098 versionpos = strnstr(versionbuf, "xnu-", 80);
1099
1100 if (versionpos) {
1101 strncpy (vstr, versionpos, (isdigit (versionpos[7]) ? 8 : 7));
1102 vstr[(isdigit (versionpos[7]) ? 8 : 7)] = '\0';
1103 retval = 0;
1104 }
1105 }
1106 }
1107 strcpy(versionbuf, vstr);
1108 return retval;
1109 }
1110 /* Primary dispatch routine for the system dump */
1111 void
1112 kdp_panic_dump()
1113 {
1114 char corename[50];
1115 char coreprefix[10];
1116 int panic_error;
1117 extern char *debug_buf;
1118 extern vm_map_t kernel_map;
1119
1120 extern char *inet_aton(const char *cp, struct in_addr *pin);
1121
1122 extern char *debug_buf;
1123 extern char *debug_buf_ptr;
1124 uint64_t abstime;
1125
1126 printf ("Entering system dump routine\n");
1127
1128 if (!panicd_specified) {
1129 printf ("A panic server was not specified in the boot-args, terminating kernel core dump.\n");
1130 goto panic_dump_exit;
1131 }
1132
1133 flag_panic_dump_in_progress = 1;
1134 not_in_kdp = 0;
1135
1136 if (pkt.input)
1137 kdp_panic("kdp_panic_dump");
1138
1139 kdp_get_xnu_version((char *) &pkt.data[0]);
1140
1141 /* Panic log bit takes precedence over core dump bit */
1142 if ((panicstr != (char *) 0) && (kdp_flag & PANIC_LOG_DUMP))
1143 strncpy(coreprefix, "paniclog", sizeof(coreprefix));
1144 else
1145 strncpy(coreprefix, "core", sizeof(coreprefix));
1146
1147 abstime = mach_absolute_time();
1148 pkt.data[10] = '\0';
1149 snprintf (corename, sizeof(corename), "%s-%s-%d.%d.%d.%d-%x",
1150 coreprefix, &pkt.data[0],
1151 (kdp_current_ip_address & 0xff000000) >> 24,
1152 (kdp_current_ip_address & 0xff0000) >> 16,
1153 (kdp_current_ip_address & 0xff00) >> 8,
1154 (kdp_current_ip_address & 0xff),
1155 (unsigned int) (abstime & 0xffffffff));
1156
1157 if (0 == inet_aton(panicd_ip_str, (struct in_addr *) &panic_server_ip)) {
1158 printf("inet_aton() failed interpreting %s as a panic server IP\n",
1159 panicd_ip_str);
1160 }
1161 else
1162 printf("Attempting connection to panic server configured at IP %s\n",
1163 panicd_ip_str);
1164
1165 if (router_specified) {
1166 if (0 == inet_aton(router_ip_str, (struct in_addr *) &parsed_router_ip)){
1167 printf("inet_aton() failed interpreting %s as an IP\n", router_ip);
1168 }
1169 else {
1170 router_ip = parsed_router_ip;
1171 printf("Routing through specified router IP %s (%d)\n", router_ip_str, router_ip);
1172 /* We will eventually need to resolve the router's MAC ourselves,
1173 * if one is specified,rather than being set through the BSD callback
1174 * but the _router_ip option does not function currently
1175 */
1176 }
1177 }
1178 /* These & 0xffs aren't necessary,but cut&paste is ever so convenient */
1179 printf("Routing via router MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1180 router_mac.ether_addr_octet[0] & 0xff,
1181 router_mac.ether_addr_octet[1] & 0xff,
1182 router_mac.ether_addr_octet[2] & 0xff,
1183 router_mac.ether_addr_octet[3] & 0xff,
1184 router_mac.ether_addr_octet[4] & 0xff,
1185 router_mac.ether_addr_octet[5] & 0xff);
1186
1187 printf("Kernel map size is %d\n", get_vmmap_size(kernel_map));
1188 printf ("Sending write request for %s\n", corename);
1189
1190 if ((panic_error = kdp_send_panic_pkt (KDP_WRQ, corename, 0 , NULL) < 0)) {
1191 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error);
1192 goto panic_dump_exit;
1193 }
1194
1195 /* Just the panic log requested */
1196 if ((panicstr != (char *) 0) && (kdp_flag & PANIC_LOG_DUMP)) {
1197 printf("Transmitting panic log, please wait: ");
1198 kdp_send_panic_packets (KDP_DATA, corename, (debug_buf_ptr - debug_buf), (unsigned int) debug_buf);
1199 kdp_send_panic_pkt (KDP_EOF, NULL, 0, ((void *) 0));
1200 printf("Please file a bug report on this panic, if possible.\n");
1201 goto panic_dump_exit;
1202 }
1203
1204 /* We want a core dump if we're here */
1205 kern_dump();
1206 panic_dump_exit:
1207 not_in_kdp = 1;
1208 flag_panic_dump_in_progress = 0;
1209 panic_block = 0;
1210 pkt.input = FALSE;
1211 pkt.len = 0;
1212 kdp_reset();
1213 return;
1214 }
1215
1216 void
1217 abort_panic_transfer()
1218 {
1219 flag_panic_dump_in_progress = 0;
1220 not_in_kdp = 1;
1221 panic_block = 0;
1222 }