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
;
61 uint32_t kdp_src_high32
;
63 unsigned kdp_vm_read( caddr_t
, caddr_t
, unsigned);
64 unsigned kdp_vm_write( caddr_t
, caddr_t
, unsigned);
66 extern vm_offset_t sectTEXTB
, sectDATAB
, sectLINKB
, sectPRELINKB
;
67 extern int sectSizeTEXT
, sectSizeDATA
, sectSizeLINK
, sectSizePRELINK
;
69 static addr64_t
kdp_vtophys(pmap_t pmap
, addr64_t va
);
73 int flavor
; /* the number for this flavor */
74 mach_msg_type_number_t count
; /* count of ints in this flavor */
75 } mythread_state_flavor_t
;
77 static mythread_state_flavor_t thread_flavor_array
[] = {
78 {PPC_THREAD_STATE
, PPC_THREAD_STATE_COUNT
},
81 static int kdp_mynum_flavors
= 1;
82 static int MAX_TSTATE_FLAVORS
= 1;
87 mythread_state_flavor_t
*flavors
;
91 unsigned int not_in_kdp
= 1; /* Cleared when we begin to access vm functions in kdp */
93 char command_buffer
[512];
106 pp
= pmap_find_phys(pmap
, va
); /* Get the page number */
107 if(!pp
) return 0; /* Just return if no translation */
109 pa
= ((addr64_t
)pp
<< 12) | (va
& 0x0000000000000FFFULL
); /* Shove in the page offset */
112 /* Verify that src is valid, and physically copy len bytes from src to
113 * dst, translating if necessary. If translation is enabled
114 * (kdp_trans_off is 0), a non-zero kdp_pmap specifies the pmap to use
115 * when translating src.
118 unsigned kdp_vm_read(
123 addr64_t cur_virt_src
, cur_virt_dst
;
124 addr64_t cur_phys_src
, cur_phys_dst
;
129 #ifdef KDP_VM_READ_DEBUG
130 kprintf("kdp_vm_read1: src %x dst %x len %x - %08X %08X\n", src
, dst
, len
, ((unsigned long *)src
)[0], ((unsigned long *)src
)[1]);
133 cur_virt_src
= (addr64_t
)((unsigned int)src
| (((uint64_t)kdp_src_high32
) << 32));
134 cur_virt_dst
= (addr64_t
)((unsigned int)dst
);
137 resid
= len
; /* Get the length to copy */
141 if((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
145 if(!mapping_phys_lookup((ppnum_t
)(cur_virt_src
>> 12), &dummy
)) return 0; /* Can't read where there's not any memory */
147 cnt
= 4096 - (cur_virt_src
& 0xFFF); /* Get length left on page */
148 if (cnt
> (4096 - (cur_virt_dst
& 0xFFF)))
149 cnt
= 4096 - (cur_virt_dst
& 0xFFF);
151 if (cnt
> resid
) cnt
= resid
;
153 bcopy_phys(cur_virt_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
164 if(kdp_pmap
) pmap
= kdp_pmap
; /* If special pmap, use it */
165 else pmap
= kernel_pmap
; /* otherwise, use kernel's */
168 /* Always translate the destination using the kernel_pmap. */
169 if((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
172 if((cur_phys_src
= kdp_vtophys(pmap
, cur_virt_src
)) == 0)
176 if(!mapping_phys_lookup((ppnum_t
)(cur_phys_src
>> 12), &dummy
)) goto exit
; /* Can't read where there's not any memory */
178 cnt
= 4096 - (cur_virt_src
& 0xFFF); /* Get length left on page */
179 if (cnt
> (4096 - (cur_virt_dst
& 0xFFF)))
180 cnt
= 4096 - (cur_virt_dst
& 0xFFF);
182 if (cnt
> resid
) cnt
= resid
;
184 #ifdef KDP_VM_READ_DEBUG
185 kprintf("kdp_vm_read2: pmap %08X, virt %016LLX, phys %016LLX\n",
186 pmap
, cur_virt_src
, cur_phys_src
);
189 bcopy_phys(cur_phys_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
197 #ifdef KDP_VM_READ_DEBUG
198 kprintf("kdp_vm_read: ret %08X\n", len
-resid
);
200 return (len
- resid
);
206 unsigned kdp_vm_write(
211 addr64_t cur_virt_src
, cur_virt_dst
;
212 addr64_t cur_phys_src
, cur_phys_dst
;
213 unsigned resid
, cnt
, cnt_src
, cnt_dst
;
215 #ifdef KDP_VM_WRITE_DEBUG
216 printf("kdp_vm_write: src %x dst %x len %x - %08X %08X\n", src
, dst
, len
, ((unsigned long *)src
)[0], ((unsigned long *)src
)[1]);
219 cur_virt_src
= (addr64_t
)((unsigned int)src
);
220 cur_virt_dst
= (addr64_t
)((unsigned int)dst
);
225 if ((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
228 if ((cur_phys_src
= kdp_vtophys(kernel_pmap
, cur_virt_src
)) == 0)
231 cnt_src
= ((cur_phys_src
+ NBPG
) & (-NBPG
)) - cur_phys_src
;
232 cnt_dst
= ((cur_phys_dst
+ NBPG
) & (-NBPG
)) - cur_phys_dst
;
234 if (cnt_src
> cnt_dst
)
241 bcopy_phys(cur_phys_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
242 sync_cache64(cur_phys_dst
, cnt
); /* Sync caches */
249 return (len
- resid
);
254 kern_collectth_state(thread_t thread
, tir_t
*t
)
258 mythread_state_flavor_t
*flavors
;
259 struct thread_command
*tc
;
261 * Fill in thread command structure.
264 hoffset
= t
->hoffset
;
265 flavors
= t
->flavors
;
267 tc
= (struct thread_command
*) (header
+ hoffset
);
269 tc
->cmdsize
= sizeof(struct thread_command
)
271 hoffset
+= sizeof(struct thread_command
);
273 * Follow with a struct thread_state_flavor and
274 * the appropriate thread state struct for each
275 * thread state flavor.
277 for (i
= 0; i
< kdp_mynum_flavors
; i
++) {
278 *(mythread_state_flavor_t
*)(header
+hoffset
) =
280 hoffset
+= sizeof(mythread_state_flavor_t
);
282 if (machine_thread_get_kern_state(thread
, flavors
[i
].flavor
,
283 (thread_state_t
) (header
+hoffset
),
284 &flavors
[i
].count
) != KERN_SUCCESS
)
285 printf ("Failure in machine_thread_get_kern_state()\n");
286 hoffset
+= flavors
[i
].count
*sizeof(int);
289 t
->hoffset
= hoffset
;
295 __unused
struct savearea
*regs
)
297 printf ("An unexpected trap (type %d) occurred during the kernel dump, terminating.\n", type
);
298 kdp_send_crashdump_pkt(KDP_EOF
, NULL
, 0, ((void *) 0));
299 abort_panic_transfer();
300 kdp_flag
&= ~KDP_PANIC_DUMP_ENABLED
;
301 kdp_flag
&= ~PANIC_CORE_ON_NMI
;
302 kdp_flag
&= ~PANIC_LOG_DUMP
;
306 kdp_raise_exception(EXC_BAD_ACCESS
, 0, 0, kdp
.saved_state
);
311 * Kernel dump (limited to currently executing 32 bit mach_kernel only)
318 unsigned int thread_count
, segment_count
;
319 unsigned int command_size
= 0, header_size
= 0, tstate_size
= 0;
320 unsigned int hoffset
= 0, foffset
= 0, nfoffset
= 0, vmoffset
= 0;
321 unsigned int max_header_size
= 0;
323 struct mach_header
*mh
;
324 struct segment_command
*sc
;
327 vm_prot_t maxprot
= 0;
328 vm_inherit_t inherit
= 0;
330 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
333 uint32_t nesting_depth
= 0;
334 kern_return_t kret
= 0;
335 struct vm_region_submap_info_64 vbr
;
336 mach_msg_type_number_t vbrcount
= 0;
340 unsigned int txstart
= 0;
341 unsigned int mach_section_count
= 4;
342 unsigned int num_sects_txed
= 0;
345 not_in_kdp
= 0; /* Tell vm functions not to acquire locks */
348 segment_count
= get_vmmap_entries(map
);
350 printf("Kernel map has %d entries\n", segment_count
);
352 nflavors
= kdp_mynum_flavors
;
353 bcopy((char *)thread_flavor_array
,(char *) flavors
,sizeof(thread_flavor_array
));
355 for (i
= 0; i
< nflavors
; i
++)
356 tstate_size
+= sizeof(mythread_state_flavor_t
) +
357 (flavors
[i
].count
* sizeof(int));
359 command_size
= (segment_count
+ mach_section_count
) *
360 sizeof(struct segment_command
) +
361 thread_count
*sizeof(struct thread_command
) +
362 tstate_size
*thread_count
;
364 header_size
= command_size
+ sizeof(struct mach_header
);
365 header
= (vm_offset_t
) command_buffer
;
368 * Set up Mach-O header for currently executing 32 bit kernel.
370 printf ("Generated Mach-O header size was %d\n", header_size
);
372 mh
= (struct mach_header
*) header
;
373 mh
->magic
= MH_MAGIC
;
374 mh
->cputype
= cpu_type();
375 mh
->cpusubtype
= cpu_subtype(); /* XXX incorrect; should match kernel */
376 mh
->filetype
= MH_CORE
;
377 mh
->ncmds
= segment_count
+ thread_count
+ mach_section_count
;
378 mh
->sizeofcmds
= command_size
;
381 hoffset
= sizeof(struct mach_header
); /* offset into header */
382 foffset
= round_page_32(header_size
); /* offset into file */
384 if ((foffset
- header_size
) < (4*sizeof(struct segment_command
))) {
386 foffset
+= ((4*sizeof(struct segment_command
)) - (foffset
-header_size
));
389 max_header_size
= foffset
;
391 vmoffset
= VM_MIN_ADDRESS
; /* offset into VM */
393 /* Transmit the Mach-O MH_CORE header, and seek forward past the
394 * area reserved for the segment and thread commands
395 * to begin data transmission
398 if ((panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, NULL
, sizeof(nfoffset
) , &nfoffset
)) < 0) {
399 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
403 if ((panic_error
= kdp_send_crashdump_data(KDP_DATA
, NULL
, sizeof(struct mach_header
), (caddr_t
) mh
) < 0)) {
404 printf ("kdp_send_crashdump_data failed with error %d\n", panic_error
);
408 if ((panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
) < 0)) {
409 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
412 printf ("Transmitting kernel state, please wait: ");
414 while ((segment_count
> 0) || (kret
== KERN_SUCCESS
)){
415 /* Check if we've transmitted all the kernel sections */
416 if (num_sects_txed
== mach_section_count
) {
421 * Get region information for next region.
424 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
425 if((kret
= vm_region_recurse_64(map
,
426 &vmoffset
, &size
, &nesting_depth
,
427 (vm_region_recurse_info_t
)&vbr
,
428 &vbrcount
)) != KERN_SUCCESS
) {
440 if(kret
!= KERN_SUCCESS
)
443 prot
= vbr
.protection
;
444 maxprot
= vbr
.max_protection
;
445 inherit
= vbr
.inheritance
;
449 switch (num_sects_txed
) {
452 /* Transmit the kernel text section */
453 vmoffset
= sectTEXTB
;
459 vmoffset
= sectDATAB
;
465 vmoffset
= sectPRELINKB
;
466 size
= sectSizePRELINK
;
471 vmoffset
= sectLINKB
;
475 /* TODO the lowmem vector area may be useful, but its transmission is
476 * disabled for now. The traceback table area should be transmitted
477 * as well - that's indirected from 0x5080.
483 * Fill in segment command structure.
486 if (hoffset
> max_header_size
)
488 sc
= (struct segment_command
*) (header
);
489 sc
->cmd
= LC_SEGMENT
;
490 sc
->cmdsize
= sizeof(struct segment_command
);
492 sc
->vmaddr
= vmoffset
;
494 sc
->fileoff
= foffset
;
496 sc
->maxprot
= maxprot
;
500 if ((panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
501 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
505 if ((panic_error
= kdp_send_crashdump_data(KDP_DATA
, NULL
, sizeof(struct segment_command
) , (caddr_t
) sc
)) < 0) {
506 printf ("kdp_send_crashdump_data failed with error %d\n", panic_error
);
510 /* Do not transmit memory tagged VM_MEMORY_IOKIT - instead, seek past that
511 * region on the server - this creates a hole in the file
514 if ((vbr
.user_tag
!= VM_MEMORY_IOKIT
)) {
516 if ((panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
)) < 0) {
517 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
523 if ((panic_error
= kdp_send_crashdump_data(KDP_DATA
, NULL
, size
, (caddr_t
) txstart
)) < 0) {
524 printf ("kdp_send_crashdump_data failed with error %d\n", panic_error
);
529 hoffset
+= sizeof(struct segment_command
);
534 tir1
.header
= header
;
536 tir1
.flavors
= flavors
;
537 tir1
.tstate_size
= tstate_size
;
539 /* Now send out the LC_THREAD load command, with the thread information
540 * for the current activation.
541 * Note that the corefile can contain LC_SEGMENT commands with file offsets
542 * that point past the edge of the corefile, in the event that the last N
543 * VM regions were all I/O mapped or otherwise non-transferable memory,
544 * not followed by a normal VM region; i.e. there will be no hole that
545 * reaches to the end of the core file.
547 kern_collectth_state (current_thread(), &tir1
);
549 if ((panic_error
= kdp_send_crashdump_pkt(KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
550 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
554 if ((panic_error
= kdp_send_crashdump_data(KDP_DATA
, NULL
, tir1
.hoffset
, (caddr_t
) header
)) < 0) {
555 printf ("kdp_send_crashdump_data failed with error %d\n", panic_error
);
560 if ((panic_error
= kdp_send_crashdump_pkt(KDP_EOF
, NULL
, 0, ((void *) 0))) < 0)
562 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);