2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <mach/mach_types.h>
24 #include <mach/vm_attributes.h>
25 #include <mach/vm_param.h>
29 #include <ppc/proc_reg.h>
30 #include <ppc/machparam.h>
33 #include <ppc/mappings.h>
34 #include <ppc/cpu_data.h>
36 #include <mach/thread_status.h>
37 #include <mach-o/loader.h>
38 #include <mach/vm_region.h>
39 #include <mach/vm_statistics.h>
41 #include <vm/vm_kern.h>
42 #include <vm/vm_object.h>
43 #include <vm/vm_protos.h>
44 #include <kdp/kdp_core.h>
45 #include <kdp/kdp_udp.h>
46 #include <kdp/kdp_internal.h>
48 #include <ppc/misc_protos.h>
49 #include <mach/vm_map.h>
53 boolean_t kdp_trans_off
=0;
54 boolean_t kdp_read_io
=0;
56 unsigned kdp_vm_read( caddr_t
, caddr_t
, unsigned);
57 unsigned kdp_vm_write( caddr_t
, caddr_t
, unsigned);
59 extern vm_offset_t sectTEXTB
, sectDATAB
, sectLINKB
, sectPRELINKB
;
60 extern int sectSizeTEXT
, sectSizeDATA
, sectSizeLINK
, sectSizePRELINK
;
62 /* XXX prototypes which should be in a commmon header file */
63 addr64_t
kdp_vtophys(pmap_t pmap
, addr64_t va
);
65 int kdp_dump_trap(int type
, struct savearea
*regs
);
67 * XXX the following prototype doesn't match the declaration because the
68 * XXX actual declaration is wrong.
70 extern int kdp_send_panic_packets(unsigned int request
, char *corename
,
71 unsigned int length
, caddr_t txstart
);
77 int flavor
; /* the number for this flavor */
78 int count
; /* count of ints in this flavor */
79 } mythread_state_flavor_t
;
81 /* These will need to be uncommented and completed
82 *if we support other architectures
88 static mythread_state_flavor_t thread_flavor_array
[] = {
89 {PPC_THREAD_STATE
, PPC_THREAD_STATE_COUNT
},
92 #elif defined (__i386__)
93 mythread_state_flavor_t thread_flavor_array [] = {
94 {i386_THREAD_STATE, i386_THREAD_STATE_COUNT},
97 #error architecture not supported
100 static int kdp_mynum_flavors
= 1;
101 static int MAX_TSTATE_FLAVORS
= 1;
106 mythread_state_flavor_t
*flavors
;
110 unsigned int not_in_kdp
= 1; /* Cleared when we begin to access vm functions in kdp */
112 char command_buffer
[512];
114 // XXX static struct vm_object test_object;
127 pp
= pmap_find_phys(pmap
, va
); /* Get the page number */
128 if(!pp
) return 0; /* Just return if no translation */
130 pa
= ((addr64_t
)pp
<< 12) | (va
& 0x0000000000000FFFULL
); /* Shove in the page offset */
133 /* Verify that src is valid, and physically copy len bytes from src to
134 * dst, translating if necessary. If translation is enabled
135 * (kdp_trans_off is 0), a non-zero kdp_pmap specifies the pmap to use
136 * when translating src.
139 unsigned kdp_vm_read(
144 addr64_t cur_virt_src
, cur_virt_dst
;
145 addr64_t cur_phys_src
, cur_phys_dst
;
150 #ifdef KDP_VM_READ_DEBUG
151 kprintf("kdp_vm_read1: src %x dst %x len %x - %08X %08X\n", src
, dst
, len
, ((unsigned long *)src
)[0], ((unsigned long *)src
)[1]);
154 cur_virt_src
= (addr64_t
)((unsigned int)src
);
155 cur_virt_dst
= (addr64_t
)((unsigned int)dst
);
160 resid
= len
; /* Get the length to copy */
164 if((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
168 if(!mapping_phys_lookup((ppnum_t
)(cur_virt_src
>> 12), &dummy
)) return 0; /* 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 bcopy_phys(cur_virt_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
187 if(kdp_pmap
) pmap
= kdp_pmap
; /* If special pmap, use it */
188 else pmap
= kernel_pmap
; /* otherwise, use kernel's */
191 /* Always translate the destination using the kernel_pmap. */
192 if((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
195 if((cur_phys_src
= kdp_vtophys(pmap
, cur_virt_src
)) == 0)
199 if(!mapping_phys_lookup((ppnum_t
)(cur_phys_src
>> 12), &dummy
)) goto exit
; /* Can't read where there's not any memory */
201 cnt
= 4096 - (cur_virt_src
& 0xFFF); /* Get length left on page */
202 if (cnt
> (4096 - (cur_virt_dst
& 0xFFF)))
203 cnt
= 4096 - (cur_virt_dst
& 0xFFF);
205 if (cnt
> resid
) cnt
= resid
;
207 #ifdef KDP_VM_READ_DEBUG
208 kprintf("kdp_vm_read2: pmap %08X, virt %016LLX, phys %016LLX\n",
209 pmap
, cur_virt_src
, cur_phys_src
);
212 bcopy_phys(cur_phys_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
220 #ifdef KDP_VM_READ_DEBUG
221 kprintf("kdp_vm_read: ret %08X\n", len
-resid
);
223 return (len
- resid
);
229 unsigned kdp_vm_write(
234 addr64_t cur_virt_src
, cur_virt_dst
;
235 addr64_t cur_phys_src
, cur_phys_dst
;
236 unsigned resid
, cnt
, cnt_src
, cnt_dst
;
238 #ifdef KDP_VM_WRITE_DEBUG
239 printf("kdp_vm_write: src %x dst %x len %x - %08X %08X\n", src
, dst
, len
, ((unsigned long *)src
)[0], ((unsigned long *)src
)[1]);
242 cur_virt_src
= (addr64_t
)((unsigned int)src
);
243 cur_virt_dst
= (addr64_t
)((unsigned int)dst
);
248 if ((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
251 if ((cur_phys_src
= kdp_vtophys(kernel_pmap
, cur_virt_src
)) == 0)
254 cnt_src
= ((cur_phys_src
+ NBPG
) & (-NBPG
)) - cur_phys_src
;
255 cnt_dst
= ((cur_phys_dst
+ NBPG
) & (-NBPG
)) - cur_phys_dst
;
257 if (cnt_src
> cnt_dst
)
264 bcopy_phys(cur_phys_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
265 sync_cache64(cur_phys_dst
, cnt
); /* Sync caches */
272 return (len
- resid
);
277 kern_collectth_state(thread_t thread
, tir_t
*t
)
281 mythread_state_flavor_t
*flavors
;
282 struct thread_command
*tc
;
284 * Fill in thread command structure.
287 hoffset
= t
->hoffset
;
288 flavors
= t
->flavors
;
290 tc
= (struct thread_command
*) (header
+ hoffset
);
292 tc
->cmdsize
= sizeof(struct thread_command
)
294 hoffset
+= sizeof(struct thread_command
);
296 * Follow with a struct thread_state_flavor and
297 * the appropriate thread state struct for each
298 * thread state flavor.
300 for (i
= 0; i
< kdp_mynum_flavors
; i
++) {
301 *(mythread_state_flavor_t
*)(header
+hoffset
) =
303 hoffset
+= sizeof(mythread_state_flavor_t
);
305 if (machine_thread_get_kern_state(thread
, flavors
[i
].flavor
,
306 (thread_state_t
) (header
+hoffset
),
307 &flavors
[i
].count
) != KERN_SUCCESS
)
308 printf ("Failure in machine_thread_get_kern_state()\n");
309 hoffset
+= flavors
[i
].count
*sizeof(int);
312 t
->hoffset
= hoffset
;
318 __unused
struct savearea
*regs
)
320 printf ("An unexpected trap (type %d) occurred during the kernel dump, terminating.\n", type
);
321 kdp_send_panic_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
322 abort_panic_transfer();
323 kdp_flag
&= ~KDP_PANIC_DUMP_ENABLED
;
324 kdp_flag
&= ~PANIC_CORE_ON_NMI
;
325 kdp_flag
&= ~PANIC_LOG_DUMP
;
329 kdp_raise_exception(EXC_BAD_ACCESS
, 0, 0, kdp
.saved_state
);
334 * Kernel dump (limited to currently executing 32 bit mach_kernel only)
341 unsigned int thread_count
, segment_count
;
342 unsigned int command_size
= 0, header_size
= 0, tstate_size
= 0;
343 unsigned int hoffset
= 0, foffset
= 0, nfoffset
= 0, vmoffset
= 0;
344 unsigned int max_header_size
= 0;
346 struct mach_header
*mh
;
347 struct segment_command
*sc
;
350 vm_prot_t maxprot
= 0;
351 vm_inherit_t inherit
= 0;
353 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
356 int nesting_depth
= 0;
357 kern_return_t kret
= 0;
358 struct vm_region_submap_info_64 vbr
;
363 unsigned int txstart
= 0;
364 unsigned int mach_section_count
= 4;
365 unsigned int num_sects_txed
= 0;
368 not_in_kdp
= 0; /* Tell vm functions not to acquire locks */
371 segment_count
= get_vmmap_entries(map
);
373 printf("Kernel map has %d entries\n", segment_count
);
375 nflavors
= kdp_mynum_flavors
;
376 bcopy((char *)thread_flavor_array
,(char *) flavors
,sizeof(thread_flavor_array
));
378 for (i
= 0; i
< nflavors
; i
++)
379 tstate_size
+= sizeof(mythread_state_flavor_t
) +
380 (flavors
[i
].count
* sizeof(int));
382 command_size
= (segment_count
+ mach_section_count
) *
383 sizeof(struct segment_command
) +
384 thread_count
*sizeof(struct thread_command
) +
385 tstate_size
*thread_count
;
387 header_size
= command_size
+ sizeof(struct mach_header
);
388 header
= (vm_offset_t
) command_buffer
;
391 * Set up Mach-O header for currently executing 32 bit kernel.
393 printf ("Generated Mach-O header size was %d\n", header_size
);
395 mh
= (struct mach_header
*) header
;
396 mh
->magic
= MH_MAGIC
;
397 mh
->cputype
= cpu_type();
398 mh
->cpusubtype
= cpu_subtype(); /* XXX incorrect; should match kernel */
399 mh
->filetype
= MH_CORE
;
400 mh
->ncmds
= segment_count
+ thread_count
+ mach_section_count
;
401 mh
->sizeofcmds
= command_size
;
404 hoffset
= sizeof(struct mach_header
); /* offset into header */
405 foffset
= round_page_32(header_size
); /* offset into file */
407 if ((foffset
- header_size
) < (4*sizeof(struct segment_command
))) {
409 foffset
+= ((4*sizeof(struct segment_command
)) - (foffset
-header_size
));
412 max_header_size
= foffset
;
414 vmoffset
= VM_MIN_ADDRESS
; /* offset into VM */
416 /* Transmit the Mach-O MH_CORE header, and seek forward past the
417 * area reserved for the segment and thread commands
418 * to begin data transmission
421 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(nfoffset
) , &nfoffset
)) < 0) {
422 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
426 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, sizeof(struct mach_header
), (caddr_t
) mh
) < 0)) {
427 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
431 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
) < 0)) {
432 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
435 printf ("Transmitting kernel state, please wait: ");
437 while ((segment_count
> 0) || (kret
== KERN_SUCCESS
)){
438 /* Check if we've transmitted all the kernel sections */
439 if (num_sects_txed
== mach_section_count
-1) {
444 * Get region information for next region.
447 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
448 if((kret
= vm_region_recurse_64(map
,
449 &vmoffset
, &size
, &nesting_depth
,
450 (vm_region_recurse_info_t
)&vbr
,
451 &vbrcount
)) != KERN_SUCCESS
) {
463 if(kret
!= KERN_SUCCESS
)
466 prot
= vbr
.protection
;
467 maxprot
= vbr
.max_protection
;
468 inherit
= vbr
.inheritance
;
472 switch (num_sects_txed
) {
475 /* Transmit the kernel text section */
476 vmoffset
= sectTEXTB
;
482 vmoffset
= sectDATAB
;
488 vmoffset
= sectPRELINKB
;
489 size
= sectSizePRELINK
;
494 vmoffset
= sectLINKB
;
498 /* TODO the lowmem vector area may be useful, but its transmission is
499 * disabled for now. The traceback table area should be transmitted
500 * as well - that's indirected from 0x5080.
506 * Fill in segment command structure.
509 if (hoffset
> max_header_size
)
511 sc
= (struct segment_command
*) (header
);
512 sc
->cmd
= LC_SEGMENT
;
513 sc
->cmdsize
= sizeof(struct segment_command
);
515 sc
->vmaddr
= vmoffset
;
517 sc
->fileoff
= foffset
;
519 sc
->maxprot
= maxprot
;
523 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
524 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
528 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, sizeof(struct segment_command
) , (caddr_t
) sc
)) < 0) {
529 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
533 /* Do not transmit memory tagged VM_MEMORY_IOKIT - instead, seek past that
534 * region on the server - this creates a hole in the file
537 if ((vbr
.user_tag
!= VM_MEMORY_IOKIT
)) {
539 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
)) < 0) {
540 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
546 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, size
, (caddr_t
) txstart
)) < 0) {
547 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
552 hoffset
+= sizeof(struct segment_command
);
557 tir1
.header
= header
;
559 tir1
.flavors
= flavors
;
560 tir1
.tstate_size
= tstate_size
;
562 /* Now send out the LC_THREAD load command, with the thread information
563 * for the current activation.
564 * Note that the corefile can contain LC_SEGMENT commands with file offsets
565 * that point past the edge of the corefile, in the event that the last N
566 * VM regions were all I/O mapped or otherwise non-transferable memory,
567 * not followed by a normal VM region; i.e. there will be no hole that
568 * reaches to the end of the core file.
570 kern_collectth_state (current_thread(), &tir1
);
572 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
573 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
577 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, tir1
.hoffset
, (caddr_t
) header
)) < 0) {
578 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
583 if ((panic_error
= kdp_send_panic_pkt (KDP_EOF
, NULL
, 0, ((void *) 0))) < 0)
585 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);