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