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