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