2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 #include <mach/mach_types.h>
31 #include <mach/vm_attributes.h>
32 #include <mach/vm_param.h>
36 #include <ppc/proc_reg.h>
37 #include <ppc/machparam.h>
40 #include <ppc/mappings.h>
41 #include <ppc/cpu_data.h>
43 #include <mach/thread_status.h>
44 #include <mach-o/loader.h>
45 #include <mach/vm_region.h>
46 #include <mach/vm_statistics.h>
48 #include <vm/vm_kern.h>
49 #include <vm/vm_object.h>
50 #include <vm/vm_protos.h>
51 #include <kdp/kdp_core.h>
52 #include <kdp/kdp_udp.h>
53 #include <kdp/kdp_internal.h>
55 #include <ppc/misc_protos.h>
56 #include <mach/vm_map.h>
60 boolean_t kdp_trans_off
=0;
61 boolean_t kdp_read_io
=0;
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 /* XXX prototypes which should be in a commmon header file */
70 addr64_t
kdp_vtophys(pmap_t pmap
, addr64_t va
);
72 int kdp_dump_trap(int type
, struct savearea
*regs
);
74 * XXX the following prototype doesn't match the declaration because the
75 * XXX actual declaration is wrong.
77 extern int kdp_send_panic_packets(unsigned int request
, char *corename
,
78 unsigned int length
, caddr_t txstart
);
84 int flavor
; /* the number for this flavor */
85 int count
; /* count of ints in this flavor */
86 } mythread_state_flavor_t
;
88 /* These will need to be uncommented and completed
89 *if we support other architectures
95 static mythread_state_flavor_t thread_flavor_array
[] = {
96 {PPC_THREAD_STATE
, PPC_THREAD_STATE_COUNT
},
99 #elif defined (__i386__)
100 mythread_state_flavor_t thread_flavor_array [] = {
101 {i386_THREAD_STATE, i386_THREAD_STATE_COUNT},
104 #error architecture not supported
107 static int kdp_mynum_flavors
= 1;
108 static int MAX_TSTATE_FLAVORS
= 1;
113 mythread_state_flavor_t
*flavors
;
117 unsigned int not_in_kdp
= 1; /* Cleared when we begin to access vm functions in kdp */
119 char command_buffer
[512];
121 // XXX static struct vm_object test_object;
134 pp
= pmap_find_phys(pmap
, va
); /* Get the page number */
135 if(!pp
) return 0; /* Just return if no translation */
137 pa
= ((addr64_t
)pp
<< 12) | (va
& 0x0000000000000FFFULL
); /* Shove in the page offset */
140 /* Verify that src is valid, and physically copy len bytes from src to
141 * dst, translating if necessary. If translation is enabled
142 * (kdp_trans_off is 0), a non-zero kdp_pmap specifies the pmap to use
143 * when translating src.
146 unsigned kdp_vm_read(
151 addr64_t cur_virt_src
, cur_virt_dst
;
152 addr64_t cur_phys_src
, cur_phys_dst
;
157 #ifdef KDP_VM_READ_DEBUG
158 kprintf("kdp_vm_read1: src %x dst %x len %x - %08X %08X\n", src
, dst
, len
, ((unsigned long *)src
)[0], ((unsigned long *)src
)[1]);
161 cur_virt_src
= (addr64_t
)((unsigned int)src
);
162 cur_virt_dst
= (addr64_t
)((unsigned int)dst
);
167 resid
= len
; /* Get the length to copy */
171 if((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
175 if(!mapping_phys_lookup((ppnum_t
)(cur_virt_src
>> 12), &dummy
)) return 0; /* Can't read where there's not any memory */
177 cnt
= 4096 - (cur_virt_src
& 0xFFF); /* Get length left on page */
178 if (cnt
> (4096 - (cur_virt_dst
& 0xFFF)))
179 cnt
= 4096 - (cur_virt_dst
& 0xFFF);
181 if (cnt
> resid
) cnt
= resid
;
183 bcopy_phys(cur_virt_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
194 if(kdp_pmap
) pmap
= kdp_pmap
; /* If special pmap, use it */
195 else pmap
= kernel_pmap
; /* otherwise, use kernel's */
198 /* Always translate the destination using the kernel_pmap. */
199 if((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
202 if((cur_phys_src
= kdp_vtophys(pmap
, cur_virt_src
)) == 0)
206 if(!mapping_phys_lookup((ppnum_t
)(cur_phys_src
>> 12), &dummy
)) goto exit
; /* Can't read where there's not any memory */
208 cnt
= 4096 - (cur_virt_src
& 0xFFF); /* Get length left on page */
209 if (cnt
> (4096 - (cur_virt_dst
& 0xFFF)))
210 cnt
= 4096 - (cur_virt_dst
& 0xFFF);
212 if (cnt
> resid
) cnt
= resid
;
214 #ifdef KDP_VM_READ_DEBUG
215 kprintf("kdp_vm_read2: pmap %08X, virt %016LLX, phys %016LLX\n",
216 pmap
, cur_virt_src
, cur_phys_src
);
219 bcopy_phys(cur_phys_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
227 #ifdef KDP_VM_READ_DEBUG
228 kprintf("kdp_vm_read: ret %08X\n", len
-resid
);
230 return (len
- resid
);
236 unsigned kdp_vm_write(
241 addr64_t cur_virt_src
, cur_virt_dst
;
242 addr64_t cur_phys_src
, cur_phys_dst
;
243 unsigned resid
, cnt
, cnt_src
, cnt_dst
;
245 #ifdef KDP_VM_WRITE_DEBUG
246 printf("kdp_vm_write: src %x dst %x len %x - %08X %08X\n", src
, dst
, len
, ((unsigned long *)src
)[0], ((unsigned long *)src
)[1]);
249 cur_virt_src
= (addr64_t
)((unsigned int)src
);
250 cur_virt_dst
= (addr64_t
)((unsigned int)dst
);
255 if ((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
258 if ((cur_phys_src
= kdp_vtophys(kernel_pmap
, cur_virt_src
)) == 0)
261 cnt_src
= ((cur_phys_src
+ NBPG
) & (-NBPG
)) - cur_phys_src
;
262 cnt_dst
= ((cur_phys_dst
+ NBPG
) & (-NBPG
)) - cur_phys_dst
;
264 if (cnt_src
> cnt_dst
)
271 bcopy_phys(cur_phys_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
272 sync_cache64(cur_phys_dst
, cnt
); /* Sync caches */
279 return (len
- resid
);
284 kern_collectth_state(thread_t thread
, tir_t
*t
)
288 mythread_state_flavor_t
*flavors
;
289 struct thread_command
*tc
;
291 * Fill in thread command structure.
294 hoffset
= t
->hoffset
;
295 flavors
= t
->flavors
;
297 tc
= (struct thread_command
*) (header
+ hoffset
);
299 tc
->cmdsize
= sizeof(struct thread_command
)
301 hoffset
+= sizeof(struct thread_command
);
303 * Follow with a struct thread_state_flavor and
304 * the appropriate thread state struct for each
305 * thread state flavor.
307 for (i
= 0; i
< kdp_mynum_flavors
; i
++) {
308 *(mythread_state_flavor_t
*)(header
+hoffset
) =
310 hoffset
+= sizeof(mythread_state_flavor_t
);
312 if (machine_thread_get_kern_state(thread
, flavors
[i
].flavor
,
313 (thread_state_t
) (header
+hoffset
),
314 &flavors
[i
].count
) != KERN_SUCCESS
)
315 printf ("Failure in machine_thread_get_kern_state()\n");
316 hoffset
+= flavors
[i
].count
*sizeof(int);
319 t
->hoffset
= hoffset
;
325 __unused
struct savearea
*regs
)
327 printf ("An unexpected trap (type %d) occurred during the kernel dump, terminating.\n", type
);
328 kdp_send_panic_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
329 abort_panic_transfer();
330 kdp_flag
&= ~KDP_PANIC_DUMP_ENABLED
;
331 kdp_flag
&= ~PANIC_CORE_ON_NMI
;
332 kdp_flag
&= ~PANIC_LOG_DUMP
;
336 kdp_raise_exception(EXC_BAD_ACCESS
, 0, 0, kdp
.saved_state
);
341 * Kernel dump (limited to currently executing 32 bit mach_kernel only)
348 unsigned int thread_count
, segment_count
;
349 unsigned int command_size
= 0, header_size
= 0, tstate_size
= 0;
350 unsigned int hoffset
= 0, foffset
= 0, nfoffset
= 0, vmoffset
= 0;
351 unsigned int max_header_size
= 0;
353 struct mach_header
*mh
;
354 struct segment_command
*sc
;
357 vm_prot_t maxprot
= 0;
358 vm_inherit_t inherit
= 0;
360 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
363 int nesting_depth
= 0;
364 kern_return_t kret
= 0;
365 struct vm_region_submap_info_64 vbr
;
370 unsigned int txstart
= 0;
371 unsigned int mach_section_count
= 4;
372 unsigned int num_sects_txed
= 0;
375 not_in_kdp
= 0; /* Tell vm functions not to acquire locks */
378 segment_count
= get_vmmap_entries(map
);
380 printf("Kernel map has %d entries\n", segment_count
);
382 nflavors
= kdp_mynum_flavors
;
383 bcopy((char *)thread_flavor_array
,(char *) flavors
,sizeof(thread_flavor_array
));
385 for (i
= 0; i
< nflavors
; i
++)
386 tstate_size
+= sizeof(mythread_state_flavor_t
) +
387 (flavors
[i
].count
* sizeof(int));
389 command_size
= (segment_count
+ mach_section_count
) *
390 sizeof(struct segment_command
) +
391 thread_count
*sizeof(struct thread_command
) +
392 tstate_size
*thread_count
;
394 header_size
= command_size
+ sizeof(struct mach_header
);
395 header
= (vm_offset_t
) command_buffer
;
398 * Set up Mach-O header for currently executing 32 bit kernel.
400 printf ("Generated Mach-O header size was %d\n", header_size
);
402 mh
= (struct mach_header
*) header
;
403 mh
->magic
= MH_MAGIC
;
404 mh
->cputype
= cpu_type();
405 mh
->cpusubtype
= cpu_subtype(); /* XXX incorrect; should match kernel */
406 mh
->filetype
= MH_CORE
;
407 mh
->ncmds
= segment_count
+ thread_count
+ mach_section_count
;
408 mh
->sizeofcmds
= command_size
;
411 hoffset
= sizeof(struct mach_header
); /* offset into header */
412 foffset
= round_page_32(header_size
); /* offset into file */
414 if ((foffset
- header_size
) < (4*sizeof(struct segment_command
))) {
416 foffset
+= ((4*sizeof(struct segment_command
)) - (foffset
-header_size
));
419 max_header_size
= foffset
;
421 vmoffset
= VM_MIN_ADDRESS
; /* offset into VM */
423 /* Transmit the Mach-O MH_CORE header, and seek forward past the
424 * area reserved for the segment and thread commands
425 * to begin data transmission
428 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(nfoffset
) , &nfoffset
)) < 0) {
429 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
433 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, sizeof(struct mach_header
), (caddr_t
) mh
) < 0)) {
434 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
438 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
) < 0)) {
439 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
442 printf ("Transmitting kernel state, please wait: ");
444 while ((segment_count
> 0) || (kret
== KERN_SUCCESS
)){
445 /* Check if we've transmitted all the kernel sections */
446 if (num_sects_txed
== mach_section_count
-1) {
451 * Get region information for next region.
454 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
455 if((kret
= vm_region_recurse_64(map
,
456 &vmoffset
, &size
, &nesting_depth
,
457 (vm_region_recurse_info_t
)&vbr
,
458 &vbrcount
)) != KERN_SUCCESS
) {
470 if(kret
!= KERN_SUCCESS
)
473 prot
= vbr
.protection
;
474 maxprot
= vbr
.max_protection
;
475 inherit
= vbr
.inheritance
;
479 switch (num_sects_txed
) {
482 /* Transmit the kernel text section */
483 vmoffset
= sectTEXTB
;
489 vmoffset
= sectDATAB
;
495 vmoffset
= sectPRELINKB
;
496 size
= sectSizePRELINK
;
501 vmoffset
= sectLINKB
;
505 /* TODO the lowmem vector area may be useful, but its transmission is
506 * disabled for now. The traceback table area should be transmitted
507 * as well - that's indirected from 0x5080.
513 * Fill in segment command structure.
516 if (hoffset
> max_header_size
)
518 sc
= (struct segment_command
*) (header
);
519 sc
->cmd
= LC_SEGMENT
;
520 sc
->cmdsize
= sizeof(struct segment_command
);
522 sc
->vmaddr
= vmoffset
;
524 sc
->fileoff
= foffset
;
526 sc
->maxprot
= maxprot
;
530 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
531 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
535 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, sizeof(struct segment_command
) , (caddr_t
) sc
)) < 0) {
536 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
540 /* Do not transmit memory tagged VM_MEMORY_IOKIT - instead, seek past that
541 * region on the server - this creates a hole in the file
544 if ((vbr
.user_tag
!= VM_MEMORY_IOKIT
)) {
546 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
)) < 0) {
547 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
553 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, size
, (caddr_t
) txstart
)) < 0) {
554 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
559 hoffset
+= sizeof(struct segment_command
);
564 tir1
.header
= header
;
566 tir1
.flavors
= flavors
;
567 tir1
.tstate_size
= tstate_size
;
569 /* Now send out the LC_THREAD load command, with the thread information
570 * for the current activation.
571 * Note that the corefile can contain LC_SEGMENT commands with file offsets
572 * that point past the edge of the corefile, in the event that the last N
573 * VM regions were all I/O mapped or otherwise non-transferable memory,
574 * not followed by a normal VM region; i.e. there will be no hole that
575 * reaches to the end of the core file.
577 kern_collectth_state (current_thread(), &tir1
);
579 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
580 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
584 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, tir1
.hoffset
, (caddr_t
) header
)) < 0) {
585 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
590 if ((panic_error
= kdp_send_panic_pkt (KDP_EOF
, NULL
, 0, ((void *) 0))) < 0)
592 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);