2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 #include <mach/mach_types.h>
26 #include <mach/vm_attributes.h>
27 #include <mach/vm_param.h>
31 #include <ppc/proc_reg.h>
32 #include <ppc/machparam.h>
35 #include <ppc/mappings.h>
37 #include <mach/thread_status.h>
38 #include <mach-o/loader.h>
39 #include <mach/vm_region.h>
40 #include <mach/vm_statistics.h>
42 #include <vm/vm_kern.h>
43 #include <kdp/kdp_core.h>
44 #include <kdp/kdp_udp.h>
45 #include <kdp/kdp_internal.h>
49 boolean_t kdp_trans_off
=0;
50 boolean_t kdp_read_io
=0;
52 unsigned kdp_vm_read( caddr_t
, caddr_t
, unsigned);
53 unsigned kdp_vm_write( caddr_t
, caddr_t
, unsigned);
57 int flavor
; /* the number for this flavor */
58 int count
; /* count of ints in this flavor */
59 } mythread_state_flavor_t
;
61 /* These will need to be uncommented and completed
62 *if we support other architectures
68 static mythread_state_flavor_t thread_flavor_array
[] = {
69 {PPC_THREAD_STATE
, PPC_THREAD_STATE_COUNT
},
72 #elif defined (__i386__)
73 mythread_state_flavor_t thread_flavor_array [] = {
74 {i386_THREAD_STATE, i386_THREAD_STATE_COUNT},
77 #error architecture not supported
80 static int kdp_mynum_flavors
= 1;
81 static int MAX_TSTATE_FLAVORS
= 1;
86 mythread_state_flavor_t
*flavors
;
90 unsigned int not_in_kdp
= 1; /* Cleared when we begin to access vm functions in kdp */
92 char command_buffer
[512];
94 static struct vm_object test_object
;
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 */
114 * Note that kdp_vm_read() does not translate the destination address.Therefore
115 * there's an implicit assumption that the destination will be a statically
116 * allocated structure, since those map to the same phys. and virt. addresses
118 unsigned kdp_vm_read(
123 addr64_t cur_virt_src
, cur_virt_dst
;
124 addr64_t cur_phys_src
;
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
);
134 cur_virt_dst
= (addr64_t
)((unsigned int)dst
);
139 resid
= len
; /* Get the length to copy */
144 if(!mapping_phys_lookup((ppnum_t
)(cur_virt_src
>> 12), &dummy
)) return 0; /* Can't read where there's not any memory */
146 cnt
= 4096 - (cur_virt_src
& 0xFFF); /* Get length left on page */
148 if (cnt
> resid
) cnt
= resid
;
150 bcopy_phys(cur_virt_src
, cur_virt_dst
, cnt
); /* Copy stuff over */
161 if(kdp_pmap
) pmap
= kdp_pmap
; /* If special pmap, use it */
162 else pmap
= kernel_pmap
; /* otherwise, use kernel's */
166 if((cur_phys_src
= kdp_vtophys(pmap
, cur_virt_src
)) == 0) goto exit
;
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
> resid
) cnt
= resid
;
173 #ifdef KDP_VM_READ_DEBUG
174 kprintf("kdp_vm_read2: pmap %08X, virt %016LLX, phys %016LLX\n",
175 pmap
, cur_virt_src
, cur_phys_src
);
178 bcopy_phys(cur_phys_src
, cur_virt_dst
, cnt
); /* Copy stuff over */
186 #ifdef KDP_VM_READ_DEBUG
187 kprintf("kdp_vm_read: ret %08X\n", len
-resid
);
189 return (len
- resid
);
195 unsigned kdp_vm_write(
200 addr64_t cur_virt_src
, cur_virt_dst
;
201 addr64_t cur_phys_src
, cur_phys_dst
;
202 unsigned resid
, cnt
, cnt_src
, cnt_dst
;
204 #ifdef KDP_VM_WRITE_DEBUG
205 printf("kdp_vm_write: src %x dst %x len %x - %08X %08X\n", src
, dst
, len
, ((unsigned long *)src
)[0], ((unsigned long *)src
)[1]);
208 cur_virt_src
= (addr64_t
)((unsigned int)src
);
209 cur_virt_dst
= (addr64_t
)((unsigned int)dst
);
214 if ((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
216 if ((cur_phys_src
= kdp_vtophys(kernel_pmap
, cur_virt_src
)) == 0)
219 cnt_src
= ((cur_phys_src
+ NBPG
) & (-NBPG
)) - cur_phys_src
;
220 cnt_dst
= ((cur_phys_dst
+ NBPG
) & (-NBPG
)) - cur_phys_dst
;
222 if (cnt_src
> cnt_dst
)
229 bcopy_phys(cur_phys_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
230 sync_cache64(cur_phys_dst
, cnt
); /* Sync caches */
237 return (len
- resid
);
242 kern_collectth_state(thread_act_t th_act
, tir_t
*t
)
246 mythread_state_flavor_t
*flavors
;
247 struct thread_command
*tc
;
249 * Fill in thread command structure.
252 hoffset
= t
->hoffset
;
253 flavors
= t
->flavors
;
255 tc
= (struct thread_command
*) (header
+ hoffset
);
257 tc
->cmdsize
= sizeof(struct thread_command
)
259 hoffset
+= sizeof(struct thread_command
);
261 * Follow with a struct thread_state_flavor and
262 * the appropriate thread state struct for each
263 * thread state flavor.
265 for (i
= 0; i
< kdp_mynum_flavors
; i
++) {
266 *(mythread_state_flavor_t
*)(header
+hoffset
) =
268 hoffset
+= sizeof(mythread_state_flavor_t
);
270 if (machine_thread_get_kern_state(th_act
, flavors
[i
].flavor
,
271 (thread_state_t
) (header
+hoffset
),
272 &flavors
[i
].count
) != KERN_SUCCESS
)
273 printf ("Failure in machine_thread_get_kern_state()\n");
274 hoffset
+= flavors
[i
].count
*sizeof(int);
277 t
->hoffset
= hoffset
;
283 struct savearea
*regs
)
287 printf ("An unexpected trap (type %d) occurred during the kernel dump, terminating.\n", type
);
288 kdp_send_panic_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
289 abort_panic_transfer();
290 kdp_flag
&= ~KDP_PANIC_DUMP_ENABLED
;
291 kdp_flag
&= ~PANIC_CORE_ON_NMI
;
292 kdp_flag
&= ~PANIC_LOG_DUMP
;
296 kdp_raise_exception(EXC_BAD_ACCESS
, 0, 0, kdp
.saved_state
);
305 unsigned int thread_count
, segment_count
;
306 unsigned int command_size
= 0, header_size
= 0, tstate_size
= 0;
307 unsigned int hoffset
= 0, foffset
= 0, nfoffset
= 0, vmoffset
= 0;
308 unsigned int max_header_size
= 0;
310 struct machine_slot
*ms
;
311 struct mach_header
*mh
;
312 struct segment_command
*sc
;
313 struct thread_command
*tc
;
316 vm_prot_t maxprot
= 0;
317 vm_inherit_t inherit
= 0;
320 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
323 int nesting_depth
= 0;
325 struct vm_region_submap_info_64 vbr
;
330 unsigned int txstart
= 0;
331 unsigned int mach_section_count
= 4;
332 unsigned int num_sects_txed
= 0;
337 extern vm_offset_t sectTEXTB
, sectDATAB
, sectLINKB
, sectPRELINKB
;
338 extern int sectSizeTEXT
, sectSizeDATA
, sectSizeLINK
, sectSizePRELINK
;
341 not_in_kdp
= 0; /* Tell vm functions not to acquire locks */
344 segment_count
= get_vmmap_entries(map
);
346 printf("Kernel map has %d entries\n", segment_count
);
348 nflavors
= kdp_mynum_flavors
;
349 bcopy((char *)thread_flavor_array
,(char *) flavors
,sizeof(thread_flavor_array
));
351 for (i
= 0; i
< nflavors
; i
++)
352 tstate_size
+= sizeof(mythread_state_flavor_t
) +
353 (flavors
[i
].count
* sizeof(int));
355 command_size
= (segment_count
+ mach_section_count
) *
356 sizeof(struct segment_command
) +
357 thread_count
*sizeof(struct thread_command
) +
358 tstate_size
*thread_count
;
360 header_size
= command_size
+ sizeof(struct mach_header
);
361 header
= (vm_offset_t
) command_buffer
;
364 * Set up Mach-O header.
366 printf ("Generated Mach-O header size was %d\n", header_size
);
368 mh
= (struct mach_header
*) header
;
369 ms
= &machine_slot
[cpu_number()];
370 mh
->magic
= MH_MAGIC
;
371 mh
->cputype
= ms
->cpu_type
;
372 mh
->cpusubtype
= ms
->cpu_subtype
;
373 mh
->filetype
= MH_CORE
;
374 mh
->ncmds
= segment_count
+ thread_count
+ mach_section_count
;
375 mh
->sizeofcmds
= command_size
;
378 hoffset
= sizeof(struct mach_header
); /* offset into header */
379 foffset
= round_page_32(header_size
); /* offset into file */
381 if ((foffset
- header_size
) < (4*sizeof(struct segment_command
))) {
383 foffset
+= ((4*sizeof(struct segment_command
)) - (foffset
-header_size
));
386 max_header_size
= foffset
;
388 vmoffset
= VM_MIN_ADDRESS
; /* offset into VM */
390 /* Transmit the Mach-O MH_CORE header, and seek forward past the
391 * area reserved for the segment and thread commands
392 * to begin data transmission
395 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(nfoffset
) , &nfoffset
)) < 0) {
396 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
400 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, sizeof(struct mach_header
), (caddr_t
) mh
) < 0)) {
401 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
405 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
) < 0)) {
406 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
409 printf ("Transmitting kernel state, please wait: ");
411 while ((segment_count
> 0) || (kret
== KERN_SUCCESS
)){
412 /* Check if we've transmitted all the kernel sections */
413 if (num_sects_txed
== mach_section_count
-1) {
418 * Get region information for next region.
421 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
422 if((kret
= vm_region_recurse_64(map
,
423 &vmoffset
, &size
, &nesting_depth
,
424 &vbr
, &vbrcount
)) != KERN_SUCCESS
) {
436 if(kret
!= KERN_SUCCESS
)
439 prot
= vbr
.protection
;
440 maxprot
= vbr
.max_protection
;
441 inherit
= vbr
.inheritance
;
445 switch (num_sects_txed
) {
448 /* Transmit the kernel text section */
449 vmoffset
= sectTEXTB
;
455 vmoffset
= sectDATAB
;
461 vmoffset
= sectPRELINKB
;
462 size
= sectSizePRELINK
;
467 vmoffset
= sectLINKB
;
471 /* TODO the lowmem vector area may be useful, but its transmission is
472 * disabled for now. The traceback table area should be transmitted
473 * as well - that's indirected from 0x5080.
479 * Fill in segment command structure.
482 if (hoffset
> max_header_size
)
484 sc
= (struct segment_command
*) (header
);
485 sc
->cmd
= LC_SEGMENT
;
486 sc
->cmdsize
= sizeof(struct segment_command
);
488 sc
->vmaddr
= vmoffset
;
490 sc
->fileoff
= foffset
;
492 sc
->maxprot
= maxprot
;
496 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
497 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
501 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, sizeof(struct segment_command
) , (caddr_t
) sc
)) < 0) {
502 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
506 /* Do not transmit memory tagged VM_MEMORY_IOKIT - instead, seek past that
507 * region on the server - this creates a hole in the file
510 if ((vbr
.user_tag
!= VM_MEMORY_IOKIT
)) {
512 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
)) < 0) {
513 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
519 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, size
, (caddr_t
) txstart
)) < 0) {
520 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
525 hoffset
+= sizeof(struct segment_command
);
530 tir1
.header
= header
;
532 tir1
.flavors
= flavors
;
533 tir1
.tstate_size
= tstate_size
;
535 /* Now send out the LC_THREAD load command, with the thread information
536 * for the current activation.
537 * Note that the corefile can contain LC_SEGMENT commands with file offsets
538 * that point past the edge of the corefile, in the event that the last N
539 * VM regions were all I/O mapped or otherwise non-transferable memory,
540 * not followed by a normal VM region; i.e. there will be no hole that
541 * reaches to the end of the core file.
543 kern_collectth_state (current_act(), &tir1
);
545 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
546 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
550 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, tir1
.hoffset
, (caddr_t
) header
)) < 0) {
551 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
556 if ((panic_error
= kdp_send_panic_pkt (KDP_EOF
, NULL
, 0, ((void *) 0))) < 0)
558 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);