2 * Copyright (c) 2000-2007 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <mach/mach_types.h>
30 #include <sys/appleapiopts.h>
31 #include <kern/debug.h>
33 #include <kdp/kdp_internal.h>
34 #include <kdp/kdp_private.h>
35 #include <kdp/kdp_core.h>
37 #include <libsa/types.h>
39 #include <string.h> /* bcopy */
41 #include <kern/processor.h>
42 #include <kern/thread.h>
43 #include <vm/vm_map.h>
44 #include <vm/vm_kern.h>
46 #define DO_ALIGN 1 /* align all packet data accesses */
48 #define KDP_TEST_HARNESS 0
50 #define dprintf(x) kprintf x
56 dispatch_table
[KDP_INVALID_REQUEST
-KDP_CONNECT
] =
59 /* 1 */ kdp_disconnect
,
66 /* 8 */ kdp_writeregs
,
70 /* C */ kdp_resumecpus
,
73 /* F */ kdp_breakpoint_set
,
74 /*10 */ kdp_breakpoint_remove
,
78 /*14 */ kdp_readmem64
,
79 /*15 */ kdp_writemem64
,
80 /*16 */ kdp_breakpoint64_set
,
81 /*17 */ kdp_breakpoint64_remove
,
82 /*18 */ kdp_kernelversion
,
83 /*19 */ kdp_readphysmem64
,
84 /*1A */ kdp_writephysmem64
,
85 /*1B */ kdp_readioport
,
86 /*1C */ kdp_writeioport
,
87 /*1D */ kdp_readmsr64
,
88 /*1E */ kdp_writemsr64
,
94 #define MAX_BREAKPOINTS 100
97 * Version 11 of the KDP Protocol adds support for 64-bit wide memory
98 * addresses (read/write and breakpoints) as well as a dedicated
99 * kernelversion request. Version 12 adds read/writing of physical
100 * memory with 64-bit wide memory addresses.
102 #define KDP_VERSION 12
105 mach_vm_address_t address
;
107 uint8_t oldbytes
[MAX_BREAKINSN_BYTES
];
108 } kdp_breakpoint_record_t
;
110 static kdp_breakpoint_record_t breakpoint_list
[MAX_BREAKPOINTS
];
111 static unsigned int breakpoints_initialized
= 0;
113 int reattach_wait
= 0;
114 int noresume_on_disconnect
= 0;
115 extern unsigned int return_on_panic
;
117 typedef struct thread_snapshot
*thread_snapshot_t
;
120 machine_trace_thread(thread_t thread
, char *tracepos
, char *tracebound
, int nframes
, boolean_t user_p
);
122 machine_trace_thread64(thread_t thread
, char *tracepos
, char *tracebound
, int nframes
, boolean_t user_p
);
126 proc_name_kdp(task_t task
, char *buf
, int size
);
129 kdp_snapshot_postflight(void);
132 pid_from_task(task_t task
);
135 kdp_set_breakpoint_internal(
136 mach_vm_address_t address
140 kdp_remove_breakpoint_internal(
141 mach_vm_address_t address
146 kdp_stackshot(int pid
, void *tracebuf
, uint32_t tracebuf_size
, unsigned trace_options
, uint32_t *pbytesTraced
);
148 boolean_t
kdp_copyin(pmap_t
, uint64_t, void *, size_t);
149 extern void bcopy_phys(addr64_t
, addr64_t
, vm_size_t
);
151 extern char version
[];
157 unsigned short *reply_port
160 static unsigned aligned_pkt
[1538/sizeof(unsigned)+1]; // max ether pkt
161 kdp_pkt_t
*rd
= (kdp_pkt_t
*)&aligned_pkt
;
167 bcopy((char *)pkt
, (char *)rd
, sizeof(aligned_pkt
));
169 rd
= (kdp_pkt_t
*)pkt
;
171 if (plen
< sizeof (rd
->hdr
) || rd
->hdr
.len
!= plen
) {
172 printf("kdp_packet bad len pkt %lu hdr %d\n", plen
, rd
->hdr
.len
);
177 if (rd
->hdr
.is_reply
) {
178 printf("kdp_packet reply recvd req %x seq %x\n",
179 rd
->hdr
.request
, rd
->hdr
.seq
);
184 req
= rd
->hdr
.request
;
185 if (req
>= KDP_INVALID_REQUEST
) {
186 printf("kdp_packet bad request %x len %d seq %x key %x\n",
187 rd
->hdr
.request
, rd
->hdr
.len
, rd
->hdr
.seq
, rd
->hdr
.key
);
192 ret
= ((*dispatch_table
[req
- KDP_CONNECT
])(rd
, len
, reply_port
));
194 bcopy((char *)rd
, (char *) pkt
, *len
);
203 __unused
unsigned short *reply_port
206 kdp_pkt_t
*rd
= (kdp_pkt_t
*)pkt
;
208 printf("kdp_unknown request %x len %d seq %x key %x\n",
209 rd
->hdr
.request
, rd
->hdr
.len
, rd
->hdr
.seq
, rd
->hdr
.key
);
218 unsigned short *reply_port
221 kdp_connect_req_t
*rq
= &pkt
->connect_req
;
223 kdp_connect_reply_t
*rp
= &pkt
->connect_reply
;
224 uint16_t rport
, eport
;
228 if (plen
< sizeof (*rq
))
231 dprintf(("kdp_connect seq %x greeting %s\n", rq
->hdr
.seq
, rq
->greeting
));
233 rport
= rq
->req_reply_port
;
234 eport
= rq
->exc_note_port
;
238 if ((seq
== kdp
.conn_seq
) && /* duplicate request */
239 (rport
== kdp
.reply_port
) &&
240 (eport
== kdp
.exception_port
) &&
241 (key
== kdp
.session_key
))
242 rp
->error
= KDPERR_NO_ERROR
;
244 rp
->error
= KDPERR_ALREADY_CONNECTED
;
247 kdp
.reply_port
= rport
;
248 kdp
.exception_port
= eport
;
251 kdp
.session_key
= key
;
253 rp
->error
= KDPERR_NO_ERROR
;
256 rp
->hdr
.is_reply
= 1;
257 rp
->hdr
.len
= sizeof (*rp
);
262 if (current_debugger
== KDP_CUR_DB
)
272 unsigned short *reply_port
275 kdp_disconnect_req_t
*rq
= &pkt
->disconnect_req
;
277 kdp_disconnect_reply_t
*rp
= &pkt
->disconnect_reply
;
279 if (plen
< sizeof (*rq
))
285 dprintf(("kdp_disconnect\n"));
287 *reply_port
= kdp
.reply_port
;
289 kdp
.reply_port
= kdp
.exception_port
= 0;
290 kdp
.is_halted
= kdp
.is_conn
= FALSE
;
291 kdp
.exception_seq
= kdp
.conn_seq
= 0;
294 if ((panicstr
!= NULL
) && (return_on_panic
== 0))
297 if (noresume_on_disconnect
== 1) {
299 noresume_on_disconnect
= 0;
302 rp
->hdr
.is_reply
= 1;
303 rp
->hdr
.len
= sizeof (*rp
);
307 if (current_debugger
== KDP_CUR_DB
)
317 unsigned short *reply_port
320 kdp_reattach_req_t
*rq
= &pkt
->reattach_req
;
323 kdp_disconnect(pkt
, len
, reply_port
);
324 *reply_port
= rq
->req_reply_port
;
333 unsigned short *reply_port
336 kdp_hostinfo_req_t
*rq
= &pkt
->hostinfo_req
;
338 kdp_hostinfo_reply_t
*rp
= &pkt
->hostinfo_reply
;
340 if (plen
< sizeof (*rq
))
343 dprintf(("kdp_hostinfo\n"));
345 rp
->hdr
.is_reply
= 1;
346 rp
->hdr
.len
= sizeof (*rp
);
348 kdp_machine_hostinfo(&rp
->hostinfo
);
350 *reply_port
= kdp
.reply_port
;
360 unsigned short *reply_port
363 kdp_kernelversion_req_t
*rq
= &pkt
->kernelversion_req
;
365 kdp_kernelversion_reply_t
*rp
= &pkt
->kernelversion_reply
;
368 if (plen
< sizeof (*rq
))
371 rp
->hdr
.is_reply
= 1;
372 rp
->hdr
.len
= sizeof (*rp
);
374 dprintf(("kdp_kernelversion\n"));
375 slen
= strlcpy(rp
->version
, version
, MAX_KDP_DATA_SIZE
);
377 rp
->hdr
.len
+= slen
+ 1; /* strlcpy returns the amount copied with NUL */
379 *reply_port
= kdp
.reply_port
;
389 unsigned short *reply_port
392 kdp_suspend_req_t
*rq
= &pkt
->suspend_req
;
394 kdp_suspend_reply_t
*rp
= &pkt
->suspend_reply
;
396 if (plen
< sizeof (*rq
))
399 rp
->hdr
.is_reply
= 1;
400 rp
->hdr
.len
= sizeof (*rp
);
402 dprintf(("kdp_suspend\n"));
404 kdp
.is_halted
= TRUE
;
406 *reply_port
= kdp
.reply_port
;
416 unsigned short *reply_port
419 kdp_resumecpus_req_t
*rq
= &pkt
->resumecpus_req
;
421 kdp_resumecpus_reply_t
*rp
= &pkt
->resumecpus_reply
;
423 if (plen
< sizeof (*rq
))
426 rp
->hdr
.is_reply
= 1;
427 rp
->hdr
.len
= sizeof (*rp
);
429 dprintf(("kdp_resumecpus %x\n", rq
->cpu_mask
));
431 kdp
.is_halted
= FALSE
;
433 *reply_port
= kdp
.reply_port
;
443 unsigned short *reply_port
446 kdp_writemem_req_t
*rq
= &pkt
->writemem_req
;
448 kdp_writemem_reply_t
*rp
= &pkt
->writemem_reply
;
451 if (plen
< sizeof (*rq
))
454 if (rq
->nbytes
> MAX_KDP_DATA_SIZE
)
455 rp
->error
= KDPERR_BAD_NBYTES
;
457 dprintf(("kdp_writemem addr %x size %d\n", rq
->address
, rq
->nbytes
));
459 cnt
= kdp_machine_vm_write((caddr_t
)rq
->data
, (mach_vm_address_t
)rq
->address
, rq
->nbytes
);
460 rp
->error
= KDPERR_NO_ERROR
;
463 rp
->hdr
.is_reply
= 1;
464 rp
->hdr
.len
= sizeof (*rp
);
466 *reply_port
= kdp
.reply_port
;
476 unsigned short *reply_port
479 kdp_writemem64_req_t
*rq
= &pkt
->writemem64_req
;
481 kdp_writemem64_reply_t
*rp
= &pkt
->writemem64_reply
;
484 if (plen
< sizeof (*rq
))
487 if (rq
->nbytes
> MAX_KDP_DATA_SIZE
)
488 rp
->error
= KDPERR_BAD_NBYTES
;
490 dprintf(("kdp_writemem64 addr %llx size %d\n", rq
->address
, rq
->nbytes
));
492 cnt
= kdp_machine_vm_write((caddr_t
)rq
->data
, (mach_vm_address_t
)rq
->address
, (mach_vm_size_t
)rq
->nbytes
);
493 rp
->error
= KDPERR_NO_ERROR
;
496 rp
->hdr
.is_reply
= 1;
497 rp
->hdr
.len
= sizeof (*rp
);
499 *reply_port
= kdp
.reply_port
;
509 unsigned short *reply_port
512 kdp_writephysmem64_req_t
*rq
= &pkt
->writephysmem64_req
;
514 kdp_writephysmem64_reply_t
*rp
= &pkt
->writephysmem64_reply
;
516 if (plen
< sizeof (*rq
))
519 if (rq
->nbytes
> MAX_KDP_DATA_SIZE
)
520 rp
->error
= KDPERR_BAD_NBYTES
;
522 dprintf(("kdp_writephysmem64 addr %llx size %d\n", rq
->address
, rq
->nbytes
));
523 kdp_machine_phys_write(rq
, rq
->data
, rq
->lcpu
);
524 rp
->error
= KDPERR_NO_ERROR
;
527 rp
->hdr
.is_reply
= 1;
528 rp
->hdr
.len
= sizeof (*rp
);
530 *reply_port
= kdp
.reply_port
;
540 unsigned short *reply_port
543 kdp_readmem_req_t
*rq
= &pkt
->readmem_req
;
545 kdp_readmem_reply_t
*rp
= &pkt
->readmem_reply
;
547 #if __i386__ || __arm__
548 void *pversion
= &version
;
551 if (plen
< sizeof (*rq
))
554 rp
->hdr
.is_reply
= 1;
555 rp
->hdr
.len
= sizeof (*rp
);
557 if (rq
->nbytes
> MAX_KDP_DATA_SIZE
)
558 rp
->error
= KDPERR_BAD_NBYTES
;
560 unsigned int n
= rq
->nbytes
;
562 dprintf(("kdp_readmem addr %x size %d\n", rq
->address
, n
));
563 #if __i386__ || __arm__
564 /* XXX This is a hack to facilitate the "showversion" macro
565 * on i386/ARM, which is used to obtain the kernel version without
566 * symbols - a pointer to the version string should eventually
567 * be pinned at a fixed address when an equivalent of the
568 * VECTORS segment (loaded at a fixed load address, and contains
569 * a table) is implemented on these architectures, as with PPC.
570 * N.B.: x86 now has a low global page, and the version indirection
571 * is pinned at 0x201C. We retain the 0x501C address override
572 * for compatibility. Future architectures should instead use
573 * the KDP_KERNELVERSION request.
575 if (rq
->address
== 0x501C)
576 rq
->address
= (uintptr_t)&pversion
;
578 cnt
= kdp_machine_vm_read((mach_vm_address_t
)rq
->address
, (caddr_t
)rp
->data
, n
);
579 rp
->error
= KDPERR_NO_ERROR
;
584 *reply_port
= kdp
.reply_port
;
594 unsigned short *reply_port
597 kdp_readmem64_req_t
*rq
= &pkt
->readmem64_req
;
599 kdp_readmem64_reply_t
*rp
= &pkt
->readmem64_reply
;
602 if (plen
< sizeof (*rq
))
605 rp
->hdr
.is_reply
= 1;
606 rp
->hdr
.len
= sizeof (*rp
);
608 if (rq
->nbytes
> MAX_KDP_DATA_SIZE
)
609 rp
->error
= KDPERR_BAD_NBYTES
;
612 dprintf(("kdp_readmem64 addr %llx size %d\n", rq
->address
, rq
->nbytes
));
614 cnt
= kdp_machine_vm_read((mach_vm_address_t
)rq
->address
, (caddr_t
)rp
->data
, rq
->nbytes
);
615 rp
->error
= KDPERR_NO_ERROR
;
620 *reply_port
= kdp
.reply_port
;
630 unsigned short *reply_port
633 kdp_readphysmem64_req_t
*rq
= &pkt
->readphysmem64_req
;
635 kdp_readphysmem64_reply_t
*rp
= &pkt
->readphysmem64_reply
;
638 if (plen
< sizeof (*rq
))
641 rp
->hdr
.is_reply
= 1;
642 rp
->hdr
.len
= sizeof (*rp
);
644 if (rq
->nbytes
> MAX_KDP_DATA_SIZE
)
645 rp
->error
= KDPERR_BAD_NBYTES
;
648 dprintf(("kdp_readphysmem64 addr %llx size %d\n", rq
->address
, rq
->nbytes
));
650 cnt
= (int)kdp_machine_phys_read(rq
, rp
->data
, rq
->lcpu
);
651 rp
->error
= KDPERR_NO_ERROR
;
656 *reply_port
= kdp
.reply_port
;
666 unsigned short *reply_port
669 kdp_maxbytes_req_t
*rq
= &pkt
->maxbytes_req
;
671 kdp_maxbytes_reply_t
*rp
= &pkt
->maxbytes_reply
;
673 if (plen
< sizeof (*rq
))
676 rp
->hdr
.is_reply
= 1;
677 rp
->hdr
.len
= sizeof (*rp
);
679 dprintf(("kdp_maxbytes\n"));
681 rp
->max_bytes
= MAX_KDP_DATA_SIZE
;
683 *reply_port
= kdp
.reply_port
;
693 unsigned short *reply_port
696 kdp_version_req_t
*rq
= &pkt
->version_req
;
698 kdp_version_reply_t
*rp
= &pkt
->version_reply
;
700 if (plen
< sizeof (*rq
))
703 rp
->hdr
.is_reply
= 1;
704 rp
->hdr
.len
= sizeof (*rp
);
706 dprintf(("kdp_version\n"));
708 rp
->version
= KDP_VERSION
;
709 if (!(kdp_flag
& KDP_BP_DIS
))
710 rp
->feature
= KDP_FEATURE_BP
;
714 *reply_port
= kdp
.reply_port
;
724 unsigned short *reply_port
727 kdp_regions_req_t
*rq
= &pkt
->regions_req
;
729 kdp_regions_reply_t
*rp
= &pkt
->regions_reply
;
732 if (plen
< sizeof (*rq
))
735 rp
->hdr
.is_reply
= 1;
736 rp
->hdr
.len
= sizeof (*rp
);
738 dprintf(("kdp_regions\n"));
744 r
->nbytes
= 0xffffffff;
746 r
->protection
= VM_PROT_ALL
; r
++; rp
->nregions
++;
748 rp
->hdr
.len
+= rp
->nregions
* sizeof (kdp_region_t
);
750 *reply_port
= kdp
.reply_port
;
760 unsigned short *reply_port
763 kdp_writeregs_req_t
*rq
= &pkt
->writeregs_req
;
766 kdp_writeregs_reply_t
*rp
= &pkt
->writeregs_reply
;
768 if (plen
< sizeof (*rq
))
771 size
= rq
->hdr
.len
- (unsigned)sizeof(kdp_hdr_t
) - (unsigned)sizeof(unsigned int);
772 rp
->error
= kdp_machine_write_regs(rq
->cpu
, rq
->flavor
, rq
->data
, &size
);
774 rp
->hdr
.is_reply
= 1;
775 rp
->hdr
.len
= sizeof (*rp
);
777 *reply_port
= kdp
.reply_port
;
787 unsigned short *reply_port
790 kdp_readregs_req_t
*rq
= &pkt
->readregs_req
;
792 kdp_readregs_reply_t
*rp
= &pkt
->readregs_reply
;
795 if (plen
< sizeof (*rq
))
798 rp
->hdr
.is_reply
= 1;
799 rp
->hdr
.len
= sizeof (*rp
);
801 rp
->error
= kdp_machine_read_regs(rq
->cpu
, rq
->flavor
, rp
->data
, &size
);
804 *reply_port
= kdp
.reply_port
;
815 unsigned short *reply_port
818 kdp_breakpoint_req_t
*rq
= &pkt
->breakpoint_req
;
819 kdp_breakpoint_reply_t
*rp
= &pkt
->breakpoint_reply
;
823 if (plen
< sizeof (*rq
))
826 dprintf(("kdp_breakpoint_set %x\n", rq
->address
));
828 kerr
= kdp_set_breakpoint_internal((mach_vm_address_t
)rq
->address
);
832 rp
->hdr
.is_reply
= 1;
833 rp
->hdr
.len
= sizeof (*rp
);
834 *reply_port
= kdp
.reply_port
;
841 kdp_breakpoint64_set(
844 unsigned short *reply_port
847 kdp_breakpoint64_req_t
*rq
= &pkt
->breakpoint64_req
;
848 kdp_breakpoint64_reply_t
*rp
= &pkt
->breakpoint64_reply
;
852 if (plen
< sizeof (*rq
))
855 dprintf(("kdp_breakpoint64_set %llx\n", rq
->address
));
857 kerr
= kdp_set_breakpoint_internal((mach_vm_address_t
)rq
->address
);
861 rp
->hdr
.is_reply
= 1;
862 rp
->hdr
.len
= sizeof (*rp
);
863 *reply_port
= kdp
.reply_port
;
870 kdp_breakpoint_remove(
873 unsigned short *reply_port
876 kdp_breakpoint_req_t
*rq
= &pkt
->breakpoint_req
;
877 kdp_breakpoint_reply_t
*rp
= &pkt
->breakpoint_reply
;
880 if (plen
< sizeof (*rq
))
883 dprintf(("kdp_breakpoint_remove %x\n", rq
->address
));
885 kerr
= kdp_remove_breakpoint_internal((mach_vm_address_t
)rq
->address
);
889 rp
->hdr
.is_reply
= 1;
890 rp
->hdr
.len
= sizeof (*rp
);
891 *reply_port
= kdp
.reply_port
;
898 kdp_breakpoint64_remove(
901 unsigned short *reply_port
904 kdp_breakpoint64_req_t
*rq
= &pkt
->breakpoint64_req
;
905 kdp_breakpoint64_reply_t
*rp
= &pkt
->breakpoint64_reply
;
909 if (plen
< sizeof (*rq
))
912 dprintf(("kdp_breakpoint64_remove %llx\n", rq
->address
));
914 kerr
= kdp_remove_breakpoint_internal((mach_vm_address_t
)rq
->address
);
918 rp
->hdr
.is_reply
= 1;
919 rp
->hdr
.len
= sizeof (*rp
);
920 *reply_port
= kdp
.reply_port
;
928 kdp_set_breakpoint_internal(
929 mach_vm_address_t address
933 uint8_t breakinstr
[MAX_BREAKINSN_BYTES
], oldinstr
[MAX_BREAKINSN_BYTES
];
934 uint32_t breakinstrsize
= sizeof(breakinstr
);
938 kdp_machine_get_breakinsn(breakinstr
, &breakinstrsize
);
940 if(breakpoints_initialized
== 0)
942 for(i
=0;(i
< MAX_BREAKPOINTS
); breakpoint_list
[i
].address
=0, i
++);
943 breakpoints_initialized
++;
946 cnt
= kdp_machine_vm_read(address
, (caddr_t
)&oldinstr
, (mach_vm_size_t
)breakinstrsize
);
948 if (0 == memcmp(oldinstr
, breakinstr
, breakinstrsize
)) {
949 printf("A trap was already set at that address, not setting new breakpoint\n");
951 return KDPERR_BREAKPOINT_ALREADY_SET
;
954 for(i
=0;(i
< MAX_BREAKPOINTS
) && (breakpoint_list
[i
].address
!= 0); i
++);
956 if (i
== MAX_BREAKPOINTS
) {
957 return KDPERR_MAX_BREAKPOINTS
;
960 breakpoint_list
[i
].address
= address
;
961 memcpy(breakpoint_list
[i
].oldbytes
, oldinstr
, breakinstrsize
);
962 breakpoint_list
[i
].bytesused
= breakinstrsize
;
964 cnt
= kdp_machine_vm_write((caddr_t
)&breakinstr
, address
, breakinstrsize
);
966 return KDPERR_NO_ERROR
;
970 kdp_remove_breakpoint_internal(
971 mach_vm_address_t address
977 for(i
=0;(i
< MAX_BREAKPOINTS
) && (breakpoint_list
[i
].address
!= address
); i
++);
979 if (i
== MAX_BREAKPOINTS
)
981 return KDPERR_BREAKPOINT_NOT_FOUND
;
984 breakpoint_list
[i
].address
= 0;
985 cnt
= kdp_machine_vm_write((caddr_t
)&breakpoint_list
[i
].oldbytes
, address
, breakpoint_list
[i
].bytesused
);
987 return KDPERR_NO_ERROR
;
991 kdp_remove_all_breakpoints(void)
994 boolean_t breakpoint_found
= FALSE
;
996 if (breakpoints_initialized
)
998 for(i
=0;i
< MAX_BREAKPOINTS
; i
++)
1000 if (breakpoint_list
[i
].address
)
1002 kdp_machine_vm_write((caddr_t
)&(breakpoint_list
[i
].oldbytes
), (mach_vm_address_t
)breakpoint_list
[i
].address
, (mach_vm_size_t
)breakpoint_list
[i
].bytesused
);
1003 breakpoint_found
= TRUE
;
1004 breakpoint_list
[i
].address
= 0;
1008 if (breakpoint_found
)
1009 printf("kdp_remove_all_breakpoints: found extant breakpoints, removing them.\n");
1011 return breakpoint_found
;
1016 __unused kdp_pkt_t
*pkt
,
1018 __unused
unsigned short *reply_port
1021 dprintf(("kdp_reboot\n"));
1023 kdp_machine_reboot();
1025 return (TRUE
); // no, not really, we won't return
1028 #define MAX_FRAMES 1000
1030 static int pid_from_task(task_t task
)
1035 pid
= proc_pid(task
->bsd_info
);
1041 kdp_copyin(pmap_t p
, uint64_t uaddr
, void *dest
, size_t size
) {
1043 char *kvaddr
= dest
;
1046 ppnum_t upn
= pmap_find_phys(p
, uaddr
);
1047 uint64_t phys_src
= (upn
<< PAGE_SHIFT
) | (uaddr
& PAGE_MASK
);
1048 uint64_t phys_dest
= kvtophys((vm_offset_t
)kvaddr
);
1049 uint64_t src_rem
= PAGE_SIZE
- (phys_src
& PAGE_MASK
);
1050 uint64_t dst_rem
= PAGE_SIZE
- (phys_dest
& PAGE_MASK
);
1051 size_t cur_size
= (uint32_t) MIN(src_rem
, dst_rem
);
1052 cur_size
= MIN(cur_size
, rem
);
1054 if (upn
&& pmap_valid_page(upn
) && phys_dest
) {
1055 bcopy_phys(phys_src
, phys_dest
, cur_size
);
1067 kdp_stackshot(int pid
, void *tracebuf
, uint32_t tracebuf_size
, unsigned trace_options
, uint32_t *pbytesTraced
)
1069 char *tracepos
= (char *) tracebuf
;
1070 char *tracebound
= tracepos
+ tracebuf_size
;
1071 uint32_t tracebytes
= 0;
1074 task_t task
= TASK_NULL
;
1075 thread_t thread
= THREAD_NULL
;
1076 int nframes
= trace_options
;
1077 thread_snapshot_t tsnap
= NULL
;
1078 unsigned framesize
= 2 * sizeof(vm_offset_t
);
1079 boolean_t dispatch_p
= ((trace_options
& STACKSHOT_GET_DQ
) != 0);
1080 uint16_t dispatch_offset
= (trace_options
& STACKSHOT_DISPATCH_OFFSET_MASK
) >> STACKSHOT_DISPATCH_OFFSET_SHIFT
;
1082 struct thread cthread
;
1084 if ((nframes
<= 0) || nframes
> MAX_FRAMES
)
1085 nframes
= MAX_FRAMES
;
1087 queue_iterate(&tasks
, task
, task_t
, tasks
) {
1088 if ((task
== NULL
) || (ml_nofault_copy((vm_offset_t
) task
, (vm_offset_t
) &ctask
, sizeof(struct task
)) != sizeof(struct task
)))
1090 /* Trace everything, unless a process was specified */
1091 if ((pid
== -1) || (pid
== pid_from_task(task
)))
1092 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
){
1093 if ((thread
== NULL
) || (ml_nofault_copy((vm_offset_t
) thread
, (vm_offset_t
) &cthread
, sizeof(struct thread
)) != sizeof(struct thread
)))
1095 if (((tracepos
+ 4 * sizeof(struct thread_snapshot
)) > tracebound
)) {
1099 /* Populate the thread snapshot header */
1100 tsnap
= (thread_snapshot_t
) tracepos
;
1101 tsnap
->thread_id
= (uint64_t) (uintptr_t)thread
;
1102 tsnap
->state
= thread
->state
;
1103 tsnap
->wait_event
= thread
->wait_event
;
1104 tsnap
->continuation
= (uint64_t) (uintptr_t) thread
->continuation
;
1105 /* Add the BSD process identifiers */
1106 if ((tsnap
->pid
= pid_from_task(task
)) != -1)
1107 proc_name_kdp(task
, tsnap
->p_comm
, sizeof(tsnap
->p_comm
));
1109 tsnap
->p_comm
[0] = '\0';
1111 tsnap
->snapshot_magic
= 0xfeedface;
1112 tracepos
+= sizeof(struct thread_snapshot
);
1113 tsnap
->ss_flags
= 0;
1115 if (dispatch_p
&& (task
!= kernel_task
) && (task
->active
) && (task
->map
)) {
1116 uint64_t dqkeyaddr
= thread_dispatchqaddr(thread
);
1117 if (dqkeyaddr
!= 0) {
1118 boolean_t task64
= task_has_64BitAddr(task
);
1119 uint64_t dqaddr
= 0;
1120 if (kdp_copyin(task
->map
->pmap
, dqkeyaddr
, &dqaddr
, (task64
? 8 : 4)) && (dqaddr
!= 0)) {
1121 uint64_t dqserialnumaddr
= dqaddr
+ dispatch_offset
;
1122 uint64_t dqserialnum
= 0;
1123 if (kdp_copyin(task
->map
->pmap
, dqserialnumaddr
, &dqserialnum
, (task64
? 8 : 4))) {
1124 tsnap
->ss_flags
|= kHasDispatchSerial
;
1125 *(uint64_t *)tracepos
= dqserialnum
;
1131 /* Call through to the machine specific trace routines
1132 * Frames are added past the snapshot header.
1134 if (thread
->kernel_stack
!= 0) {
1135 #if defined(__LP64__)
1136 tracebytes
= machine_trace_thread64(thread
, tracepos
, tracebound
, nframes
, FALSE
);
1137 tsnap
->ss_flags
|= kKernel64_p
;
1140 tracebytes
= machine_trace_thread(thread
, tracepos
, tracebound
, nframes
, FALSE
);
1144 tsnap
->nkern_frames
= tracebytes
/framesize
;
1145 tracepos
+= tracebytes
;
1147 /* Trace user stack, if any */
1148 if (thread
->task
->map
!= kernel_map
) {
1150 if (task_has_64BitAddr(thread
->task
)) {
1151 tracebytes
= machine_trace_thread64(thread
, tracepos
, tracebound
, nframes
, TRUE
);
1152 tsnap
->ss_flags
|= kUser64_p
;
1156 tracebytes
= machine_trace_thread(thread
, tracepos
, tracebound
, nframes
, TRUE
);
1160 tsnap
->nuser_frames
= tracebytes
/framesize
;
1161 tracepos
+= tracebytes
;
1167 /* Release stack snapshot wait indicator */
1168 kdp_snapshot_postflight();
1170 *pbytesTraced
= (uint32_t)(tracepos
- (char *) tracebuf
);
1176 kdp_readioport(kdp_pkt_t
*pkt
,
1178 unsigned short *reply_port
1181 kdp_readioport_req_t
*rq
= &pkt
->readioport_req
;
1182 kdp_readioport_reply_t
*rp
= &pkt
->readioport_reply
;
1185 if (plen
< sizeof (*rq
))
1188 rp
->hdr
.is_reply
= 1;
1189 rp
->hdr
.len
= sizeof (*rp
);
1191 if (rq
->nbytes
> MAX_KDP_DATA_SIZE
)
1192 rp
->error
= KDPERR_BAD_NBYTES
;
1194 #if KDP_TEST_HARNESS
1195 uint16_t addr
= rq
->address
;
1197 uint16_t size
= rq
->nbytes
;
1198 dprintf(("kdp_readioport addr %x size %d\n", addr
, size
));
1200 rp
->error
= kdp_machine_ioport_read(rq
, rp
->data
, rq
->lcpu
);
1201 if (rp
->error
== KDPERR_NO_ERROR
)
1202 rp
->hdr
.len
+= size
;
1205 *reply_port
= kdp
.reply_port
;
1215 unsigned short *reply_port
1218 kdp_writeioport_req_t
*rq
= &pkt
->writeioport_req
;
1219 kdp_writeioport_reply_t
*rp
= &pkt
->writeioport_reply
;
1222 if (plen
< sizeof (*rq
))
1225 if (rq
->nbytes
> MAX_KDP_DATA_SIZE
)
1226 rp
->error
= KDPERR_BAD_NBYTES
;
1228 dprintf(("kdp_writeioport addr %x size %d\n", rq
->address
,
1231 rp
->error
= kdp_machine_ioport_write(rq
, rq
->data
, rq
->lcpu
);
1234 rp
->hdr
.is_reply
= 1;
1235 rp
->hdr
.len
= sizeof (*rp
);
1237 *reply_port
= kdp
.reply_port
;
1244 kdp_readmsr64(kdp_pkt_t
*pkt
,
1246 unsigned short *reply_port
1249 kdp_readmsr64_req_t
*rq
= &pkt
->readmsr64_req
;
1250 kdp_readmsr64_reply_t
*rp
= &pkt
->readmsr64_reply
;
1253 if (plen
< sizeof (*rq
))
1256 rp
->hdr
.is_reply
= 1;
1257 rp
->hdr
.len
= sizeof (*rp
);
1259 dprintf(("kdp_readmsr64 lcpu %x addr %x\n", rq
->lcpu
, rq
->address
));
1260 rp
->error
= kdp_machine_msr64_read(rq
, rp
->data
, rq
->lcpu
);
1261 if (rp
->error
== KDPERR_NO_ERROR
)
1262 rp
->hdr
.len
+= sizeof(uint64_t);
1264 *reply_port
= kdp
.reply_port
;
1274 unsigned short *reply_port
1277 kdp_writemsr64_req_t
*rq
= &pkt
->writemsr64_req
;
1278 kdp_writemsr64_reply_t
*rp
= &pkt
->writemsr64_reply
;
1281 if (plen
< sizeof (*rq
))
1284 dprintf(("kdp_writemsr64 lcpu %x addr %x\n", rq
->lcpu
, rq
->address
));
1285 rp
->error
= kdp_machine_msr64_write(rq
, rq
->data
, rq
->lcpu
);
1287 rp
->hdr
.is_reply
= 1;
1288 rp
->hdr
.len
= sizeof (*rp
);
1290 *reply_port
= kdp
.reply_port
;
1300 unsigned short *reply_port
1303 kdp_dumpinfo_req_t
*rq
= &pkt
->dumpinfo_req
;
1304 kdp_dumpinfo_reply_t
*rp
= &pkt
->dumpinfo_reply
;
1307 if (plen
< sizeof (*rq
))
1310 dprintf(("kdp_dumpinfo file=%s destip=%s routerip=%s\n", rq
->name
, rq
->destip
, rq
->routerip
));
1311 rp
->hdr
.is_reply
= 1;
1312 rp
->hdr
.len
= sizeof (*rp
);
1314 if ((rq
->type
& KDP_DUMPINFO_MASK
) != KDP_DUMPINFO_GETINFO
) {
1315 kdp_set_dump_info(rq
->type
, rq
->name
, rq
->destip
, rq
->routerip
,
1319 /* gather some stats for reply */
1320 kdp_get_dump_info(&rp
->type
, rp
->name
, rp
->destip
, rp
->routerip
,
1323 *reply_port
= kdp
.reply_port
;