]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kdp/kdp.c
xnu-517.7.21.tar.gz
[apple/xnu.git] / osfmk / kdp / kdp.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 #include <mach/mach_types.h>
24 #include <kern/debug.h>
25
26 #include <kdp/kdp_internal.h>
27 #include <kdp/kdp_private.h>
28
29 #include <libsa/types.h>
30
31 int kdp_vm_read( caddr_t, caddr_t, unsigned int);
32 int kdp_vm_write( caddr_t, caddr_t, unsigned int);
33
34 #define DO_ALIGN 1 /* align all packet data accesses */
35
36 #define KDP_TEST_HARNESS 0
37 #if KDP_TEST_HARNESS
38 #define dprintf(x) kprintf x
39 #else
40 #define dprintf(x)
41 #endif
42
43 static kdp_dispatch_t
44 dispatch_table[KDP_REATTACH - KDP_CONNECT +1] =
45 {
46 /* 0 */ kdp_connect,
47 /* 1 */ kdp_disconnect,
48 /* 2 */ kdp_hostinfo,
49 /* 3 */ kdp_version,
50 /* 4 */ kdp_maxbytes,
51 /* 5 */ kdp_readmem,
52 /* 6 */ kdp_writemem,
53 /* 7 */ kdp_readregs,
54 /* 8 */ kdp_writeregs,
55 /* 9 */ kdp_unknown,
56 /* A */ kdp_unknown,
57 /* B */ kdp_suspend,
58 /* C */ kdp_resumecpus,
59 /* D */ kdp_unknown,
60 /* E */ kdp_unknown,
61 /* F */ kdp_breakpoint_set,
62 /*10 */ kdp_breakpoint_remove,
63 /*11 */ kdp_regions,
64 /*12 */ kdp_reattach
65 };
66
67 kdp_glob_t kdp;
68
69
70 #define MAX_BREAKPOINTS 100
71 #define KDP_MAX_BREAKPOINTS 100
72
73 #define BREAKPOINT_NOT_FOUND 101
74 #define BREAKPOINT_ALREADY_SET 102
75
76 #define KDP_VERSION 10
77
78 typedef struct{
79 unsigned int address;
80 unsigned int old_instruction;
81 } kdp_breakpoint_record_t;
82
83 static kdp_breakpoint_record_t breakpoint_list[MAX_BREAKPOINTS];
84 static unsigned int breakpoints_initialized = 0;
85
86 int reattach_wait = 0;
87 int noresume_on_disconnect = 0;
88
89 boolean_t
90 kdp_packet(
91 unsigned char *pkt,
92 int *len,
93 unsigned short *reply_port
94 )
95 {
96 static unsigned aligned_pkt[1538/sizeof(unsigned)+1]; // max ether pkt
97 kdp_pkt_t *rd = (kdp_pkt_t *)&aligned_pkt;
98 int plen = *len;
99 unsigned int req;
100 boolean_t ret;
101
102 #if DO_ALIGN
103 bcopy((char *)pkt, (char *)rd, sizeof(aligned_pkt));
104 #else
105 rd = (kdp_pkt_t *)pkt;
106 #endif
107 if (plen < sizeof (rd->hdr) || rd->hdr.len != plen) {
108 printf("kdp_packet bad len pkt %d hdr %d\n", plen, rd->hdr.len);
109
110 return (FALSE);
111 }
112
113 if (rd->hdr.is_reply) {
114 printf("kdp_packet reply recvd req %x seq %x\n",
115 rd->hdr.request, rd->hdr.seq);
116
117 return (FALSE);
118 }
119
120 req = rd->hdr.request;
121 if ((req < KDP_CONNECT) || (req > KDP_REATTACH)) {
122 printf("kdp_packet bad request %x len %d seq %x key %x\n",
123 rd->hdr.request, rd->hdr.len, rd->hdr.seq, rd->hdr.key);
124
125 return (FALSE);
126 }
127
128 ret = ((*dispatch_table[req - KDP_CONNECT])(rd, len, reply_port));
129 #if DO_ALIGN
130 bcopy((char *)rd, (char *) pkt, *len);
131 #endif
132 return ret;
133 }
134
135 static boolean_t
136 kdp_unknown(
137 kdp_pkt_t *pkt,
138 int *len,
139 unsigned short *reply_port
140 )
141 {
142 kdp_pkt_t *rd = (kdp_pkt_t *)pkt;
143
144 printf("kdp_unknown request %x len %d seq %x key %x\n",
145 rd->hdr.request, rd->hdr.len, rd->hdr.seq, rd->hdr.key);
146
147 return (FALSE);
148 }
149
150 static boolean_t
151 kdp_connect(
152 kdp_pkt_t *pkt,
153 int *len,
154 unsigned short *reply_port
155 )
156 {
157 kdp_connect_req_t *rq = &pkt->connect_req;
158 int plen = *len;
159 kdp_connect_reply_t *rp = &pkt->connect_reply;
160
161 if (plen < sizeof (*rq))
162 return (FALSE);
163
164 dprintf(("kdp_connect seq %x greeting %s\n", rq->hdr.seq, rq->greeting));
165
166 if (kdp.is_conn) {
167 if (rq->hdr.seq == kdp.conn_seq) /* duplicate request */
168 rp->error = KDPERR_NO_ERROR;
169 else
170 rp->error = KDPERR_ALREADY_CONNECTED;
171 }
172 else {
173 kdp.reply_port = rq->req_reply_port;
174 kdp.exception_port = rq->exc_note_port;
175 kdp.is_conn = TRUE;
176 kdp.conn_seq = rq->hdr.seq;
177
178 rp->error = KDPERR_NO_ERROR;
179 }
180
181 rp->hdr.is_reply = 1;
182 rp->hdr.len = sizeof (*rp);
183
184 *reply_port = kdp.reply_port;
185 *len = rp->hdr.len;
186
187 if (current_debugger == KDP_CUR_DB)
188 active_debugger=1;
189
190 return (TRUE);
191 }
192
193 static boolean_t
194 kdp_disconnect(
195 kdp_pkt_t *pkt,
196 int *len,
197 unsigned short *reply_port
198 )
199 {
200 kdp_disconnect_req_t *rq = &pkt->disconnect_req;
201 int plen = *len;
202 kdp_disconnect_reply_t *rp = &pkt->disconnect_reply;
203
204 if (plen < sizeof (*rq))
205 return (FALSE);
206
207 if (!kdp.is_conn)
208 return (FALSE);
209
210 dprintf(("kdp_disconnect\n"));
211
212 *reply_port = kdp.reply_port;
213
214 kdp.reply_port = kdp.exception_port = 0;
215 kdp.is_halted = kdp.is_conn = FALSE;
216 kdp.exception_seq = kdp.conn_seq = 0;
217
218 if (noresume_on_disconnect == 1) {
219 reattach_wait = 1;
220 noresume_on_disconnect = 0;
221 }
222
223 rp->hdr.is_reply = 1;
224 rp->hdr.len = sizeof (*rp);
225
226 *len = rp->hdr.len;
227
228 if (current_debugger == KDP_CUR_DB)
229 active_debugger=0;
230
231 return (TRUE);
232 }
233
234 static boolean_t
235 kdp_reattach(
236 kdp_pkt_t *pkt,
237 int *len,
238 unsigned short *reply_port
239 )
240 {
241 kdp_reattach_req_t *rq = &pkt->reattach_req;
242 kdp_disconnect_reply_t *rp = &pkt->disconnect_reply;
243
244 kdp.is_conn = TRUE;
245 kdp_disconnect(pkt, len, reply_port);
246 *reply_port = rq->req_reply_port;
247 reattach_wait = 1;
248 return (TRUE);
249 }
250
251 static boolean_t
252 kdp_hostinfo(
253 kdp_pkt_t *pkt,
254 int *len,
255 unsigned short *reply_port
256 )
257 {
258 kdp_hostinfo_req_t *rq = &pkt->hostinfo_req;
259 int plen = *len;
260 kdp_hostinfo_reply_t *rp = &pkt->hostinfo_reply;
261
262 if (plen < sizeof (*rq))
263 return (FALSE);
264
265 rp->hdr.is_reply = 1;
266 rp->hdr.len = sizeof (*rp);
267
268 kdp_machine_hostinfo(&rp->hostinfo);
269
270 *reply_port = kdp.reply_port;
271 *len = rp->hdr.len;
272
273 return (TRUE);
274 }
275
276 static boolean_t
277 kdp_suspend(
278 kdp_pkt_t *pkt,
279 int *len,
280 unsigned short *reply_port
281 )
282 {
283 kdp_suspend_req_t *rq = &pkt->suspend_req;
284 int plen = *len;
285 kdp_suspend_reply_t *rp = &pkt->suspend_reply;
286
287 if (plen < sizeof (*rq))
288 return (FALSE);
289
290 rp->hdr.is_reply = 1;
291 rp->hdr.len = sizeof (*rp);
292
293 dprintf(("kdp_suspend\n"));
294
295 kdp.is_halted = TRUE;
296
297 *reply_port = kdp.reply_port;
298 *len = rp->hdr.len;
299
300 return (TRUE);
301 }
302
303 static boolean_t
304 kdp_resumecpus(
305 kdp_pkt_t *pkt,
306 int *len,
307 unsigned short *reply_port
308 )
309 {
310 kdp_resumecpus_req_t *rq = &pkt->resumecpus_req;
311 int plen = *len;
312 kdp_resumecpus_reply_t *rp = &pkt->resumecpus_reply;
313
314 if (plen < sizeof (*rq))
315 return (FALSE);
316
317 rp->hdr.is_reply = 1;
318 rp->hdr.len = sizeof (*rp);
319
320 dprintf(("kdp_resumecpus %x\n", rq->cpu_mask));
321
322 kdp.is_halted = FALSE;
323
324 *reply_port = kdp.reply_port;
325 *len = rp->hdr.len;
326
327 return (TRUE);
328 }
329
330 static boolean_t
331 kdp_writemem(
332 kdp_pkt_t *pkt,
333 int *len,
334 unsigned short *reply_port
335 )
336 {
337 kdp_writemem_req_t *rq = &pkt->writemem_req;
338 int plen = *len;
339 kdp_writemem_reply_t *rp = &pkt->writemem_reply;
340 int cnt;
341
342 if (plen < sizeof (*rq))
343 return (FALSE);
344
345 if (rq->nbytes > MAX_KDP_DATA_SIZE)
346 rp->error = KDPERR_BAD_NBYTES;
347 else {
348 dprintf(("kdp_writemem addr %x size %d\n", rq->address, rq->nbytes));
349
350 cnt = kdp_vm_write((caddr_t)rq->data, (caddr_t)rq->address, rq->nbytes);
351 rp->error = KDPERR_NO_ERROR;
352 }
353
354 rp->hdr.is_reply = 1;
355 rp->hdr.len = sizeof (*rp);
356
357 *reply_port = kdp.reply_port;
358 *len = rp->hdr.len;
359
360 return (TRUE);
361 }
362
363 static boolean_t
364 kdp_readmem(
365 kdp_pkt_t *pkt,
366 int *len,
367 unsigned short *reply_port
368 )
369 {
370 kdp_readmem_req_t *rq = &pkt->readmem_req;
371 int plen = *len;
372 kdp_readmem_reply_t *rp = &pkt->readmem_reply;
373 int cnt;
374
375 if (plen < sizeof (*rq))
376 return (FALSE);
377
378 rp->hdr.is_reply = 1;
379 rp->hdr.len = sizeof (*rp);
380
381 if (rq->nbytes > MAX_KDP_DATA_SIZE)
382 rp->error = KDPERR_BAD_NBYTES;
383 else {
384 unsigned int n = rq->nbytes;
385
386 dprintf(("kdp_readmem addr %x size %d\n", rq->address, rq->nbytes));
387
388 cnt = kdp_vm_read((caddr_t)rq->address, (caddr_t)rp->data, rq->nbytes);
389 rp->error = KDPERR_NO_ERROR;
390
391 rp->hdr.len += cnt;
392 }
393
394 *reply_port = kdp.reply_port;
395 *len = rp->hdr.len;
396
397 return (TRUE);
398 }
399
400 static boolean_t
401 kdp_maxbytes(
402 kdp_pkt_t *pkt,
403 int *len,
404 unsigned short *reply_port
405 )
406 {
407 kdp_maxbytes_req_t *rq = &pkt->maxbytes_req;
408 int plen = *len;
409 kdp_maxbytes_reply_t *rp = &pkt->maxbytes_reply;
410
411 if (plen < sizeof (*rq))
412 return (FALSE);
413
414 rp->hdr.is_reply = 1;
415 rp->hdr.len = sizeof (*rp);
416
417 dprintf(("kdp_maxbytes\n"));
418
419 rp->max_bytes = MAX_KDP_DATA_SIZE;
420
421 *reply_port = kdp.reply_port;
422 *len = rp->hdr.len;
423
424 return (TRUE);
425 }
426
427 static boolean_t
428 kdp_version(
429 kdp_pkt_t *pkt,
430 int *len,
431 unsigned short *reply_port
432 )
433 {
434 kdp_version_req_t *rq = &pkt->version_req;
435 int plen = *len;
436 kdp_version_reply_t *rp = &pkt->version_reply;
437 kdp_region_t *r;
438
439 if (plen < sizeof (*rq))
440 return (FALSE);
441
442 rp->hdr.is_reply = 1;
443 rp->hdr.len = sizeof (*rp);
444
445 dprintf(("kdp_version\n"));
446
447 rp->version = KDP_VERSION;
448 #ifdef __ppc__
449 if (!(kdp_flag & KDP_BP_DIS))
450 rp->feature = KDP_FEATURE_BP;
451 else
452 rp->feature = 0;
453 #else
454 rp->feature = 0;
455 #endif
456
457 *reply_port = kdp.reply_port;
458 *len = rp->hdr.len;
459
460 return (TRUE);
461 }
462
463 static boolean_t
464 kdp_regions(
465 kdp_pkt_t *pkt,
466 int *len,
467 unsigned short *reply_port
468 )
469 {
470 kdp_regions_req_t *rq = &pkt->regions_req;
471 int plen = *len;
472 kdp_regions_reply_t *rp = &pkt->regions_reply;
473 kdp_region_t *r;
474
475 if (plen < sizeof (*rq))
476 return (FALSE);
477
478 rp->hdr.is_reply = 1;
479 rp->hdr.len = sizeof (*rp);
480
481 dprintf(("kdp_regions\n"));
482
483 r = rp->regions;
484 rp->nregions = 0;
485
486 (vm_offset_t)r->address = 0;
487 r->nbytes = 0xffffffff;
488
489 r->protection = VM_PROT_ALL; r++; rp->nregions++;
490
491 rp->hdr.len += rp->nregions * sizeof (kdp_region_t);
492
493 *reply_port = kdp.reply_port;
494 *len = rp->hdr.len;
495
496 return (TRUE);
497 }
498
499 static boolean_t
500 kdp_writeregs(
501 kdp_pkt_t *pkt,
502 int *len,
503 unsigned short *reply_port
504 )
505 {
506 kdp_writeregs_req_t *rq = &pkt->writeregs_req;
507 int plen = *len;
508 int size;
509 kdp_writeregs_reply_t *rp = &pkt->writeregs_reply;
510
511 if (plen < sizeof (*rq))
512 return (FALSE);
513
514 size = rq->hdr.len - sizeof(kdp_hdr_t) - sizeof(unsigned int);
515 rp->error = kdp_machine_write_regs(rq->cpu, rq->flavor, rq->data, &size);
516
517 rp->hdr.is_reply = 1;
518 rp->hdr.len = sizeof (*rp);
519
520 *reply_port = kdp.reply_port;
521 *len = rp->hdr.len;
522
523 return (TRUE);
524 }
525
526 static boolean_t
527 kdp_readregs(
528 kdp_pkt_t *pkt,
529 int *len,
530 unsigned short *reply_port
531 )
532 {
533 kdp_readregs_req_t *rq = &pkt->readregs_req;
534 int plen = *len;
535 kdp_readregs_reply_t *rp = &pkt->readregs_reply;
536 int size;
537
538 if (plen < sizeof (*rq))
539 return (FALSE);
540
541 rp->hdr.is_reply = 1;
542 rp->hdr.len = sizeof (*rp);
543
544 rp->error = kdp_machine_read_regs(rq->cpu, rq->flavor, rp->data, &size);
545 rp->hdr.len += size;
546
547 *reply_port = kdp.reply_port;
548 *len = rp->hdr.len;
549
550 return (TRUE);
551 }
552
553 static boolean_t
554 kdp_breakpoint_set(
555 kdp_pkt_t *pkt,
556 int *len,
557 unsigned short *reply_port
558 )
559 {
560 kdp_breakpoint_req_t *rq = &pkt->breakpoint_req;
561 kdp_breakpoint_reply_t *rp = &pkt->breakpoint_reply;
562 int plen = *len;
563 int cnt, i;
564 unsigned int old_instruction = 0;
565 unsigned int breakinstr = kdp_ml_get_breakinsn();
566
567 if(breakpoints_initialized == 0)
568 {
569 for(i=0;(i < MAX_BREAKPOINTS); breakpoint_list[i].address=0, i++);
570 breakpoints_initialized++;
571 }
572 if (plen < sizeof (*rq))
573 return (FALSE);
574 cnt = kdp_vm_read((caddr_t)rq->address, (caddr_t)(&old_instruction), sizeof(int));
575
576 if (old_instruction==breakinstr)
577 {
578 printf("A trap was already set at that address, not setting new breakpoint\n");
579 rp->error = BREAKPOINT_ALREADY_SET;
580
581 rp->hdr.is_reply = 1;
582 rp->hdr.len = sizeof (*rp);
583 *reply_port = kdp.reply_port;
584 *len = rp->hdr.len;
585
586 return (TRUE);
587 }
588
589 for(i=0;(i < MAX_BREAKPOINTS) && (breakpoint_list[i].address != 0); i++);
590
591 if (i == MAX_BREAKPOINTS)
592 {
593 rp->error = KDP_MAX_BREAKPOINTS;
594
595 rp->hdr.is_reply = 1;
596 rp->hdr.len = sizeof (*rp);
597 *reply_port = kdp.reply_port;
598 *len = rp->hdr.len;
599
600 return (TRUE);
601 }
602 breakpoint_list[i].address = rq->address;
603 breakpoint_list[i].old_instruction = old_instruction;
604
605 cnt = kdp_vm_write((caddr_t)&breakinstr, (caddr_t)rq->address, sizeof(&breakinstr));
606
607 rp->error = KDPERR_NO_ERROR;
608 rp->hdr.is_reply = 1;
609 rp->hdr.len = sizeof (*rp);
610 *reply_port = kdp.reply_port;
611 *len = rp->hdr.len;
612
613 return (TRUE);
614 }
615
616 static boolean_t
617 kdp_breakpoint_remove(
618 kdp_pkt_t *pkt,
619 int *len,
620 unsigned short *reply_port
621 )
622 {
623 kdp_breakpoint_req_t *rq = &pkt->breakpoint_req;
624 kdp_breakpoint_reply_t *rp = &pkt->breakpoint_reply;
625 int plen = *len;
626 int cnt,i;
627
628 if (plen < sizeof (*rq))
629 return (FALSE);
630
631 for(i=0;(i < MAX_BREAKPOINTS) && (breakpoint_list[i].address != rq->address); i++);
632 if (i == MAX_BREAKPOINTS)
633 {
634 rp->error = BREAKPOINT_NOT_FOUND;
635 rp->hdr.is_reply = 1;
636 rp->hdr.len = sizeof (*rp);
637 *reply_port = kdp.reply_port;
638 *len = rp->hdr.len;
639
640 return (TRUE); /* Check if it needs to be FALSE in case of error */
641 }
642
643 breakpoint_list[i].address = 0;
644 cnt = kdp_vm_write((caddr_t)&(breakpoint_list[i].old_instruction), (caddr_t)rq->address, sizeof(int));
645 rp->error = KDPERR_NO_ERROR;
646 rp->hdr.is_reply = 1;
647 rp->hdr.len = sizeof (*rp);
648 *reply_port = kdp.reply_port;
649 *len = rp->hdr.len;
650
651 return (TRUE);
652 }
653
654 boolean_t
655 kdp_remove_all_breakpoints()
656 {
657 int i;
658 boolean_t breakpoint_found = FALSE;
659
660 if (breakpoints_initialized)
661 {
662 for(i=0;i < MAX_BREAKPOINTS; i++)
663 {
664 if (breakpoint_list[i].address)
665 {
666 kdp_vm_write((caddr_t)&(breakpoint_list[i].old_instruction), (caddr_t)breakpoint_list[i].address, sizeof(int));
667 breakpoint_found = TRUE;
668 breakpoint_list[i].address = 0;
669 }
670 }
671 if (breakpoint_found)
672 printf("kdp_remove_all_breakpoints: found extant breakpoints, removing them.\n");
673 }
674 return breakpoint_found;
675 }