2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 #include <mach/mach_types.h>
23 #include <mach/vm_attributes.h>
24 #include <mach/vm_param.h>
28 #include <ppc/proc_reg.h>
29 #include <ppc/machparam.h>
32 #include <ppc/mappings.h>
34 #include <mach/thread_status.h>
35 #include <mach-o/loader.h>
36 #include <mach/vm_region.h>
37 #include <mach/vm_statistics.h>
39 #include <vm/vm_kern.h>
40 #include <kdp/kdp_core.h>
41 #include <kdp/kdp_udp.h>
42 #include <kdp/kdp_internal.h>
46 boolean_t kdp_trans_off
=0;
47 boolean_t kdp_read_io
=0;
49 unsigned kdp_vm_read( caddr_t
, caddr_t
, unsigned);
50 unsigned kdp_vm_write( caddr_t
, caddr_t
, unsigned);
54 int flavor
; /* the number for this flavor */
55 int count
; /* count of ints in this flavor */
56 } mythread_state_flavor_t
;
58 /* These will need to be uncommented and completed
59 *if we support other architectures
65 static mythread_state_flavor_t thread_flavor_array
[] = {
66 {PPC_THREAD_STATE
, PPC_THREAD_STATE_COUNT
},
69 #elif defined (__i386__)
70 mythread_state_flavor_t thread_flavor_array [] = {
71 {i386_THREAD_STATE, i386_THREAD_STATE_COUNT},
74 #error architecture not supported
77 static int kdp_mynum_flavors
= 1;
78 static int MAX_TSTATE_FLAVORS
= 1;
83 mythread_state_flavor_t
*flavors
;
87 unsigned int not_in_kdp
= 1; /* Cleared when we begin to access vm functions in kdp */
89 char command_buffer
[512];
91 static struct vm_object test_object
;
103 pp
= pmap_find_phys(pmap
, va
); /* Get the page number */
104 if(!pp
) return 0; /* Just return if no translation */
106 pa
= ((addr64_t
)pp
<< 12) | (va
& 0x0000000000000FFFULL
); /* Shove in the page offset */
111 * Note that kdp_vm_read() does not translate the destination address.Therefore
112 * there's an implicit assumption that the destination will be a statically
113 * allocated structure, since those map to the same phys. and virt. addresses
115 unsigned kdp_vm_read(
120 addr64_t cur_virt_src
, cur_virt_dst
;
121 addr64_t cur_phys_src
;
126 #ifdef KDP_VM_READ_DEBUG
127 kprintf("kdp_vm_read1: src %x dst %x len %x - %08X %08X\n", src
, dst
, len
, ((unsigned long *)src
)[0], ((unsigned long *)src
)[1]);
130 cur_virt_src
= (addr64_t
)((unsigned int)src
);
131 cur_virt_dst
= (addr64_t
)((unsigned int)dst
);
136 resid
= len
; /* Get the length to copy */
141 if(!mapping_phys_lookup((ppnum_t
)(cur_virt_src
>> 12), &dummy
)) return 0; /* Can't read where there's not any memory */
143 cnt
= 4096 - (cur_virt_src
& 0xFFF); /* Get length left on page */
145 if (cnt
> resid
) cnt
= resid
;
147 bcopy_phys(cur_virt_src
, cur_virt_dst
, cnt
); /* Copy stuff over */
158 if(kdp_pmap
) pmap
= kdp_pmap
; /* If special pmap, use it */
159 else pmap
= kernel_pmap
; /* otherwise, use kernel's */
163 if((cur_phys_src
= kdp_vtophys(pmap
, cur_virt_src
)) == 0) goto exit
;
165 if(!mapping_phys_lookup((ppnum_t
)(cur_phys_src
>> 12), &dummy
)) goto exit
; /* Can't read where there's not any memory */
167 cnt
= 4096 - (cur_virt_src
& 0xFFF); /* Get length left on page */
168 if (cnt
> resid
) cnt
= resid
;
170 #ifdef KDP_VM_READ_DEBUG
171 kprintf("kdp_vm_read2: pmap %08X, virt %016LLX, phys %016LLX\n",
172 pmap
, cur_virt_src
, cur_phys_src
);
175 bcopy_phys(cur_phys_src
, cur_virt_dst
, cnt
); /* Copy stuff over */
183 #ifdef KDP_VM_READ_DEBUG
184 kprintf("kdp_vm_read: ret %08X\n", len
-resid
);
186 return (len
- resid
);
192 unsigned kdp_vm_write(
197 addr64_t cur_virt_src
, cur_virt_dst
;
198 addr64_t cur_phys_src
, cur_phys_dst
;
199 unsigned resid
, cnt
, cnt_src
, cnt_dst
;
201 #ifdef KDP_VM_WRITE_DEBUG
202 printf("kdp_vm_write: src %x dst %x len %x - %08X %08X\n", src
, dst
, len
, ((unsigned long *)src
)[0], ((unsigned long *)src
)[1]);
205 cur_virt_src
= (addr64_t
)((unsigned int)src
);
206 cur_virt_dst
= (addr64_t
)((unsigned int)dst
);
211 if ((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
213 if ((cur_phys_src
= kdp_vtophys(kernel_pmap
, cur_virt_src
)) == 0)
216 cnt_src
= ((cur_phys_src
+ NBPG
) & (-NBPG
)) - cur_phys_src
;
217 cnt_dst
= ((cur_phys_dst
+ NBPG
) & (-NBPG
)) - cur_phys_dst
;
219 if (cnt_src
> cnt_dst
)
226 bcopy_phys(cur_phys_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
227 sync_cache64(cur_phys_dst
, cnt
); /* Sync caches */
234 return (len
- resid
);
239 kern_collectth_state(thread_act_t th_act
, tir_t
*t
)
243 mythread_state_flavor_t
*flavors
;
244 struct thread_command
*tc
;
246 * Fill in thread command structure.
249 hoffset
= t
->hoffset
;
250 flavors
= t
->flavors
;
252 tc
= (struct thread_command
*) (header
+ hoffset
);
254 tc
->cmdsize
= sizeof(struct thread_command
)
256 hoffset
+= sizeof(struct thread_command
);
258 * Follow with a struct thread_state_flavor and
259 * the appropriate thread state struct for each
260 * thread state flavor.
262 for (i
= 0; i
< kdp_mynum_flavors
; i
++) {
263 *(mythread_state_flavor_t
*)(header
+hoffset
) =
265 hoffset
+= sizeof(mythread_state_flavor_t
);
267 if (machine_thread_get_kern_state(th_act
, flavors
[i
].flavor
,
268 (thread_state_t
) (header
+hoffset
),
269 &flavors
[i
].count
) != KERN_SUCCESS
)
270 printf ("Failure in machine_thread_get_kern_state()\n");
271 hoffset
+= flavors
[i
].count
*sizeof(int);
274 t
->hoffset
= hoffset
;
280 struct savearea
*regs
)
284 printf ("An unexpected trap (type %d) occurred during the kernel dump, terminating.\n", type
);
285 kdp_send_panic_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
286 abort_panic_transfer();
287 kdp_flag
&= ~KDP_PANIC_DUMP_ENABLED
;
288 kdp_flag
&= ~PANIC_CORE_ON_NMI
;
289 kdp_flag
&= ~PANIC_LOG_DUMP
;
293 kdp_raise_exception(EXC_BAD_ACCESS
, 0, 0, kdp
.saved_state
);
302 unsigned int thread_count
, segment_count
;
303 unsigned int command_size
= 0, header_size
= 0, tstate_size
= 0;
304 unsigned int hoffset
= 0, foffset
= 0, nfoffset
= 0, vmoffset
= 0;
305 unsigned int max_header_size
= 0;
307 struct machine_slot
*ms
;
308 struct mach_header
*mh
;
309 struct segment_command
*sc
;
310 struct thread_command
*tc
;
313 vm_prot_t maxprot
= 0;
314 vm_inherit_t inherit
= 0;
317 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
320 int nesting_depth
= 0;
322 struct vm_region_submap_info_64 vbr
;
327 unsigned int txstart
= 0;
328 unsigned int mach_section_count
= 4;
329 unsigned int num_sects_txed
= 0;
334 extern vm_offset_t sectTEXTB
, sectDATAB
, sectLINKB
, sectPRELINKB
;
335 extern int sectSizeTEXT
, sectSizeDATA
, sectSizeLINK
, sectSizePRELINK
;
338 not_in_kdp
= 0; /* Tell vm functions not to acquire locks */
341 segment_count
= get_vmmap_entries(map
);
343 printf("Kernel map has %d entries\n", segment_count
);
345 nflavors
= kdp_mynum_flavors
;
346 bcopy((char *)thread_flavor_array
,(char *) flavors
,sizeof(thread_flavor_array
));
348 for (i
= 0; i
< nflavors
; i
++)
349 tstate_size
+= sizeof(mythread_state_flavor_t
) +
350 (flavors
[i
].count
* sizeof(int));
352 command_size
= (segment_count
+ mach_section_count
) *
353 sizeof(struct segment_command
) +
354 thread_count
*sizeof(struct thread_command
) +
355 tstate_size
*thread_count
;
357 header_size
= command_size
+ sizeof(struct mach_header
);
358 header
= (vm_offset_t
) command_buffer
;
361 * Set up Mach-O header.
363 printf ("Generated Mach-O header size was %d\n", header_size
);
365 mh
= (struct mach_header
*) header
;
366 ms
= &machine_slot
[cpu_number()];
367 mh
->magic
= MH_MAGIC
;
368 mh
->cputype
= ms
->cpu_type
;
369 mh
->cpusubtype
= ms
->cpu_subtype
;
370 mh
->filetype
= MH_CORE
;
371 mh
->ncmds
= segment_count
+ thread_count
+ mach_section_count
;
372 mh
->sizeofcmds
= command_size
;
375 hoffset
= sizeof(struct mach_header
); /* offset into header */
376 foffset
= round_page_32(header_size
); /* offset into file */
378 if ((foffset
- header_size
) < (4*sizeof(struct segment_command
))) {
380 foffset
+= ((4*sizeof(struct segment_command
)) - (foffset
-header_size
));
383 max_header_size
= foffset
;
385 vmoffset
= VM_MIN_ADDRESS
; /* offset into VM */
387 /* Transmit the Mach-O MH_CORE header, and seek forward past the
388 * area reserved for the segment and thread commands
389 * to begin data transmission
392 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(nfoffset
) , &nfoffset
)) < 0) {
393 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
397 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, sizeof(struct mach_header
), (caddr_t
) mh
) < 0)) {
398 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
402 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
) < 0)) {
403 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
406 printf ("Transmitting kernel state, please wait: ");
408 while ((segment_count
> 0) || (kret
== KERN_SUCCESS
)){
409 /* Check if we've transmitted all the kernel sections */
410 if (num_sects_txed
== mach_section_count
-1) {
415 * Get region information for next region.
418 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
419 if((kret
= vm_region_recurse_64(map
,
420 &vmoffset
, &size
, &nesting_depth
,
421 &vbr
, &vbrcount
)) != KERN_SUCCESS
) {
433 if(kret
!= KERN_SUCCESS
)
436 prot
= vbr
.protection
;
437 maxprot
= vbr
.max_protection
;
438 inherit
= vbr
.inheritance
;
442 switch (num_sects_txed
) {
445 /* Transmit the kernel text section */
446 vmoffset
= sectTEXTB
;
452 vmoffset
= sectDATAB
;
458 vmoffset
= sectPRELINKB
;
459 size
= sectSizePRELINK
;
464 vmoffset
= sectLINKB
;
468 /* TODO the lowmem vector area may be useful, but its transmission is
469 * disabled for now. The traceback table area should be transmitted
470 * as well - that's indirected from 0x5080.
476 * Fill in segment command structure.
479 if (hoffset
> max_header_size
)
481 sc
= (struct segment_command
*) (header
);
482 sc
->cmd
= LC_SEGMENT
;
483 sc
->cmdsize
= sizeof(struct segment_command
);
485 sc
->vmaddr
= vmoffset
;
487 sc
->fileoff
= foffset
;
489 sc
->maxprot
= maxprot
;
493 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
494 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
498 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, sizeof(struct segment_command
) , (caddr_t
) sc
)) < 0) {
499 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
503 /* Do not transmit memory tagged VM_MEMORY_IOKIT - instead, seek past that
504 * region on the server - this creates a hole in the file
507 if ((vbr
.user_tag
!= VM_MEMORY_IOKIT
)) {
509 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
)) < 0) {
510 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
516 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, size
, (caddr_t
) txstart
)) < 0) {
517 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
522 hoffset
+= sizeof(struct segment_command
);
527 tir1
.header
= header
;
529 tir1
.flavors
= flavors
;
530 tir1
.tstate_size
= tstate_size
;
532 /* Now send out the LC_THREAD load command, with the thread information
533 * for the current activation.
534 * Note that the corefile can contain LC_SEGMENT commands with file offsets
535 * that point past the edge of the corefile, in the event that the last N
536 * VM regions were all I/O mapped or otherwise non-transferable memory,
537 * not followed by a normal VM region; i.e. there will be no hole that
538 * reaches to the end of the core file.
540 kern_collectth_state (current_act(), &tir1
);
542 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
543 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
547 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, tir1
.hoffset
, (caddr_t
) header
)) < 0) {
548 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
553 if ((panic_error
= kdp_send_panic_pkt (KDP_EOF
, NULL
, 0, ((void *) 0))) < 0)
555 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);