2 * Copyright (c) 2000-2006 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@
28 #include <mach/mach_types.h>
29 #include <mach/vm_attributes.h>
30 #include <mach/vm_param.h>
34 #include <ppc/proc_reg.h>
35 #include <ppc/machparam.h>
38 #include <ppc/mappings.h>
39 #include <ppc/cpu_data.h>
40 #include <ppc/misc_protos.h>
42 #include <mach/thread_status.h>
43 #include <mach-o/loader.h>
44 #include <mach/vm_region.h>
45 #include <mach/vm_statistics.h>
47 #include <vm/vm_kern.h>
48 #include <vm/vm_object.h>
49 #include <vm/vm_protos.h>
50 #include <kdp/kdp_core.h>
51 #include <kdp/kdp_udp.h>
52 #include <kdp/kdp_internal.h>
54 #include <ppc/misc_protos.h>
55 #include <mach/vm_map.h>
59 boolean_t kdp_trans_off
;
60 boolean_t kdp_read_io
;
62 extern vm_offset_t sectTEXTB
, sectDATAB
, sectLINKB
, sectPRELINKB
;
63 extern unsigned long sectSizeTEXT
, sectSizeDATA
, sectSizeLINK
, sectSizePRELINK
;
65 static addr64_t
kdp_vtophys(pmap_t pmap
, addr64_t va
);
69 int flavor
; /* the number for this flavor */
70 mach_msg_type_number_t count
; /* count of ints in this flavor */
71 } mythread_state_flavor_t
;
73 static mythread_state_flavor_t thread_flavor_array
[] = {
74 {PPC_THREAD_STATE
, PPC_THREAD_STATE_COUNT
},
77 static int kdp_mynum_flavors
= 1;
78 static int MAX_TSTATE_FLAVORS
= 1;
83 mythread_state_flavor_t
*flavors
;
87 char command_buffer
[512];
100 pp
= pmap_find_phys(pmap
, va
); /* Get the page number */
101 if(!pp
) return 0; /* Just return if no translation */
103 pa
= ((addr64_t
)pp
<< 12) | (va
& 0x0000000000000FFFULL
); /* Shove in the page offset */
106 /* Verify that src is valid, and physically copy len bytes from src to
107 * dst, translating if necessary. If translation is enabled
108 * (kdp_trans_off is 0), a non-zero kdp_pmap specifies the pmap to use
109 * when translating src.
113 kdp_machine_vm_read( mach_vm_address_t src
, caddr_t dst
, mach_vm_size_t len
)
115 addr64_t cur_virt_src
, cur_virt_dst
;
116 addr64_t cur_phys_src
, cur_phys_dst
;
121 #ifdef KDP_VM_READ_DEBUG
122 kprintf("kdp_machine_vm_read1: src %llx dst %llx len %x - %08X %08X\n", src
, dst
, len
, ((unsigned long *)src
)[0], ((unsigned long *)src
)[1]);
125 cur_virt_src
= (addr64_t
)src
;
126 cur_virt_dst
= (addr64_t
)(intptr_t)dst
;
129 resid
= len
; /* Get the length to copy */
133 if((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
137 if(!mapping_phys_lookup((ppnum_t
)(cur_virt_src
>> 12), &dummy
)) return 0; /* Can't read where there's not any memory */
139 cnt
= 4096 - (cur_virt_src
& 0xFFF); /* Get length left on page */
140 if (cnt
> (4096 - (cur_virt_dst
& 0xFFF)))
141 cnt
= 4096 - (cur_virt_dst
& 0xFFF);
143 if (cnt
> resid
) cnt
= resid
;
145 bcopy_phys(cur_virt_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
156 if(kdp_pmap
) pmap
= kdp_pmap
; /* If special pmap, use it */
157 else pmap
= kernel_pmap
; /* otherwise, use kernel's */
160 /* Always translate the destination using the kernel_pmap. */
161 if((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
164 if((cur_phys_src
= kdp_vtophys(pmap
, cur_virt_src
)) == 0)
168 if(!mapping_phys_lookup((ppnum_t
)(cur_phys_src
>> 12), &dummy
)) goto exit
; /* Can't read where there's not any memory */
170 cnt
= 4096 - (cur_virt_src
& 0xFFF); /* Get length left on page */
171 if (cnt
> (4096 - (cur_virt_dst
& 0xFFF)))
172 cnt
= 4096 - (cur_virt_dst
& 0xFFF);
174 if (cnt
> resid
) cnt
= resid
;
176 #ifdef KDP_VM_READ_DEBUG
177 kprintf("kdp_machine_vm_read2: pmap %08X, virt %016LLX, phys %016LLX\n",
178 pmap
, cur_virt_src
, cur_phys_src
);
181 bcopy_phys(cur_phys_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
189 #ifdef KDP_VM_READ_DEBUG
190 kprintf("kdp_machine_vm_read: ret %08X\n", len
-resid
);
192 return (len
- resid
);
196 kdp_machine_phys_read(kdp_readphysmem64_req_t
*rq __unused
, caddr_t dst __unused
, uint16_t lcpu __unused
)
198 return 0; /* unimplemented */
205 kdp_machine_vm_write( caddr_t src
, mach_vm_address_t dst
, mach_vm_size_t len
)
207 addr64_t cur_virt_src
, cur_virt_dst
;
208 addr64_t cur_phys_src
, cur_phys_dst
;
209 unsigned resid
, cnt
, cnt_src
, cnt_dst
;
211 #ifdef KDP_VM_WRITE_DEBUG
212 printf("kdp_vm_write: src %x dst %x len %x - %08X %08X\n", src
, dst
, len
, ((unsigned long *)src
)[0], ((unsigned long *)src
)[1]);
215 cur_virt_src
= (addr64_t
)(intptr_t)src
;
216 cur_virt_dst
= (addr64_t
)dst
;
221 if ((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
224 if ((cur_phys_src
= kdp_vtophys(kernel_pmap
, cur_virt_src
)) == 0)
227 cnt_src
= ((cur_phys_src
+ NBPG
) & (-NBPG
)) - cur_phys_src
;
228 cnt_dst
= ((cur_phys_dst
+ NBPG
) & (-NBPG
)) - cur_phys_dst
;
230 if (cnt_src
> cnt_dst
)
237 bcopy_phys(cur_phys_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
238 sync_cache64(cur_phys_dst
, cnt
); /* Sync caches */
245 return (len
- resid
);
249 kdp_machine_phys_write(kdp_writephysmem64_req_t
*rq __unused
, caddr_t src __unused
,
250 uint16_t lcpu __unused
)
252 return 0; /* unimplemented */
256 kern_collectth_state(thread_t thread
, tir_t
*t
)
260 mythread_state_flavor_t
*flavors
;
261 struct thread_command
*tc
;
263 * Fill in thread command structure.
266 hoffset
= t
->hoffset
;
267 flavors
= t
->flavors
;
269 tc
= (struct thread_command
*) (header
+ hoffset
);
271 tc
->cmdsize
= sizeof(struct thread_command
)
273 hoffset
+= sizeof(struct thread_command
);
275 * Follow with a struct thread_state_flavor and
276 * the appropriate thread state struct for each
277 * thread state flavor.
279 for (i
= 0; i
< kdp_mynum_flavors
; i
++) {
280 *(mythread_state_flavor_t
*)(header
+hoffset
) =
282 hoffset
+= sizeof(mythread_state_flavor_t
);
284 if (machine_thread_get_kern_state(thread
, flavors
[i
].flavor
,
285 (thread_state_t
) (header
+hoffset
),
286 &flavors
[i
].count
) != KERN_SUCCESS
)
287 printf ("Failure in machine_thread_get_kern_state()\n");
288 hoffset
+= flavors
[i
].count
*sizeof(int);
291 t
->hoffset
= hoffset
;
297 __unused
struct savearea
*regs
)
299 printf ("An unexpected trap (type %d) occurred during the kernel dump, terminating.\n", type
);
300 kdp_send_crashdump_pkt(KDP_EOF
, NULL
, 0, ((void *) 0));
301 abort_panic_transfer();
302 kdp_flag
&= ~KDP_PANIC_DUMP_ENABLED
;
303 kdp_flag
&= ~PANIC_CORE_ON_NMI
;
304 kdp_flag
&= ~PANIC_LOG_DUMP
;
308 kdp_raise_exception(EXC_BAD_ACCESS
, 0, 0, kdp
.saved_state
);
313 * Kernel dump (limited to currently executing 32 bit mach_kernel only)
320 unsigned int thread_count
, segment_count
;
321 unsigned int command_size
= 0, header_size
= 0, tstate_size
= 0;
322 unsigned int hoffset
= 0, foffset
= 0, nfoffset
= 0, vmoffset
= 0;
323 unsigned int max_header_size
= 0;
325 struct mach_header
*mh
;
326 struct segment_command
*sc
;
329 vm_prot_t maxprot
= 0;
330 vm_inherit_t inherit
= 0;
332 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
335 uint32_t nesting_depth
= 0;
336 kern_return_t kret
= 0;
337 struct vm_region_submap_info_64 vbr
;
338 mach_msg_type_number_t vbrcount
= 0;
342 unsigned int txstart
= 0;
343 unsigned int mach_section_count
= 4;
344 unsigned int num_sects_txed
= 0;
349 segment_count
= get_vmmap_entries(map
);
351 printf("Kernel map has %d entries\n", segment_count
);
353 nflavors
= kdp_mynum_flavors
;
354 bcopy((char *)thread_flavor_array
,(char *) flavors
,sizeof(thread_flavor_array
));
356 for (i
= 0; i
< nflavors
; i
++)
357 tstate_size
+= sizeof(mythread_state_flavor_t
) +
358 (flavors
[i
].count
* sizeof(int));
360 command_size
= (segment_count
+ mach_section_count
) *
361 sizeof(struct segment_command
) +
362 thread_count
*sizeof(struct thread_command
) +
363 tstate_size
*thread_count
;
365 header_size
= command_size
+ sizeof(struct mach_header
);
366 header
= (vm_offset_t
) command_buffer
;
369 * Set up Mach-O header for currently executing 32 bit kernel.
371 printf ("Generated Mach-O header size was %d\n", header_size
);
373 mh
= (struct mach_header
*) header
;
374 mh
->magic
= MH_MAGIC
;
375 mh
->cputype
= cpu_type();
376 mh
->cpusubtype
= cpu_subtype(); /* XXX incorrect; should match kernel */
377 mh
->filetype
= MH_CORE
;
378 mh
->ncmds
= segment_count
+ thread_count
+ mach_section_count
;
379 mh
->sizeofcmds
= command_size
;
382 hoffset
= sizeof(struct mach_header
); /* offset into header */
383 foffset
= round_page_32(header_size
); /* offset into file */
385 if ((foffset
- header_size
) < (4*sizeof(struct segment_command
))) {
387 foffset
+= ((4*sizeof(struct segment_command
)) - (foffset
-header_size
));
390 max_header_size
= foffset
;
392 vmoffset
= VM_MIN_ADDRESS
; /* offset into VM */
394 /* Transmit the Mach-O MH_CORE header, and seek forward past the
395 * area reserved for the segment and thread commands
396 * to begin data transmission
399 if ((panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, NULL
, sizeof(nfoffset
) , &nfoffset
)) < 0) {
400 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
404 if ((panic_error
= kdp_send_crashdump_data(KDP_DATA
, NULL
, sizeof(struct mach_header
), (caddr_t
) mh
) < 0)) {
405 printf ("kdp_send_crashdump_data failed with error %d\n", panic_error
);
409 if ((panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
) < 0)) {
410 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
413 printf ("Transmitting kernel state, please wait: ");
415 while ((segment_count
> 0) || (kret
== KERN_SUCCESS
)){
416 /* Check if we've transmitted all the kernel sections */
417 if (num_sects_txed
== mach_section_count
) {
422 * Get region information for next region.
425 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
426 if((kret
= vm_region_recurse_64(map
,
427 &vmoffset
, &size
, &nesting_depth
,
428 (vm_region_recurse_info_t
)&vbr
,
429 &vbrcount
)) != KERN_SUCCESS
) {
441 if(kret
!= KERN_SUCCESS
)
444 prot
= vbr
.protection
;
445 maxprot
= vbr
.max_protection
;
446 inherit
= vbr
.inheritance
;
450 switch (num_sects_txed
) {
453 /* Transmit the kernel text section */
454 vmoffset
= sectTEXTB
;
460 vmoffset
= sectDATAB
;
466 vmoffset
= sectPRELINKB
;
467 size
= sectSizePRELINK
;
472 vmoffset
= sectLINKB
;
476 /* TODO the lowmem vector area may be useful, but its transmission is
477 * disabled for now. The traceback table area should be transmitted
478 * as well - that's indirected from 0x5080.
484 * Fill in segment command structure.
487 if (hoffset
> max_header_size
)
489 sc
= (struct segment_command
*) (header
);
490 sc
->cmd
= LC_SEGMENT
;
491 sc
->cmdsize
= sizeof(struct segment_command
);
493 sc
->vmaddr
= vmoffset
;
495 sc
->fileoff
= foffset
;
497 sc
->maxprot
= maxprot
;
501 if ((panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
502 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
506 if ((panic_error
= kdp_send_crashdump_data(KDP_DATA
, NULL
, sizeof(struct segment_command
) , (caddr_t
) sc
)) < 0) {
507 printf ("kdp_send_crashdump_data failed with error %d\n", panic_error
);
511 /* Do not transmit memory tagged VM_MEMORY_IOKIT - instead, seek past that
512 * region on the server - this creates a hole in the file
515 if ((vbr
.user_tag
!= VM_MEMORY_IOKIT
)) {
517 if ((panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
)) < 0) {
518 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
524 if ((panic_error
= kdp_send_crashdump_data(KDP_DATA
, NULL
, size
, (caddr_t
) txstart
)) < 0) {
525 printf ("kdp_send_crashdump_data failed with error %d\n", panic_error
);
530 hoffset
+= sizeof(struct segment_command
);
535 tir1
.header
= header
;
537 tir1
.flavors
= flavors
;
538 tir1
.tstate_size
= tstate_size
;
540 /* Now send out the LC_THREAD load command, with the thread information
541 * for the current activation.
542 * Note that the corefile can contain LC_SEGMENT commands with file offsets
543 * that point past the edge of the corefile, in the event that the last N
544 * VM regions were all I/O mapped or otherwise non-transferable memory,
545 * not followed by a normal VM region; i.e. there will be no hole that
546 * reaches to the end of the core file.
548 kern_collectth_state (current_thread(), &tir1
);
550 if ((panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
551 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
555 if ((panic_error
= kdp_send_crashdump_data(KDP_DATA
, NULL
, tir1
.hoffset
, (caddr_t
) header
)) < 0) {
556 printf ("kdp_send_crashdump_data failed with error %d\n", panic_error
);
561 if ((panic_error
= kdp_send_crashdump_pkt(KDP_EOF
, NULL
, 0, ((void *) 0))) < 0)
563 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);