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>
33 #include <ppc/cpu_data.h>
35 #include <mach/thread_status.h>
36 #include <mach-o/loader.h>
37 #include <mach/vm_region.h>
38 #include <mach/vm_statistics.h>
40 #include <vm/vm_kern.h>
41 #include <vm/vm_object.h>
42 #include <vm/vm_protos.h>
43 #include <kdp/kdp_core.h>
44 #include <kdp/kdp_udp.h>
45 #include <kdp/kdp_internal.h>
47 #include <ppc/misc_protos.h>
48 #include <mach/vm_map.h>
52 boolean_t kdp_trans_off
=0;
53 boolean_t kdp_read_io
=0;
55 unsigned kdp_vm_read( caddr_t
, caddr_t
, unsigned);
56 unsigned kdp_vm_write( caddr_t
, caddr_t
, unsigned);
58 extern vm_offset_t sectTEXTB
, sectDATAB
, sectLINKB
, sectPRELINKB
;
59 extern int sectSizeTEXT
, sectSizeDATA
, sectSizeLINK
, sectSizePRELINK
;
61 /* XXX prototypes which should be in a commmon header file */
62 addr64_t
kdp_vtophys(pmap_t pmap
, addr64_t va
);
64 int kdp_dump_trap(int type
, struct savearea
*regs
);
66 * XXX the following prototype doesn't match the declaration because the
67 * XXX actual declaration is wrong.
69 extern int kdp_send_panic_packets(unsigned int request
, char *corename
,
70 unsigned int length
, caddr_t txstart
);
76 int flavor
; /* the number for this flavor */
77 int count
; /* count of ints in this flavor */
78 } mythread_state_flavor_t
;
80 /* These will need to be uncommented and completed
81 *if we support other architectures
87 static mythread_state_flavor_t thread_flavor_array
[] = {
88 {PPC_THREAD_STATE
, PPC_THREAD_STATE_COUNT
},
91 #elif defined (__i386__)
92 mythread_state_flavor_t thread_flavor_array [] = {
93 {i386_THREAD_STATE, i386_THREAD_STATE_COUNT},
96 #error architecture not supported
99 static int kdp_mynum_flavors
= 1;
100 static int MAX_TSTATE_FLAVORS
= 1;
105 mythread_state_flavor_t
*flavors
;
109 unsigned int not_in_kdp
= 1; /* Cleared when we begin to access vm functions in kdp */
111 char command_buffer
[512];
113 // XXX static struct vm_object test_object;
126 pp
= pmap_find_phys(pmap
, va
); /* Get the page number */
127 if(!pp
) return 0; /* Just return if no translation */
129 pa
= ((addr64_t
)pp
<< 12) | (va
& 0x0000000000000FFFULL
); /* Shove in the page offset */
132 /* Verify that src is valid, and physically copy len bytes from src to
133 * dst, translating if necessary. If translation is enabled
134 * (kdp_trans_off is 0), a non-zero kdp_pmap specifies the pmap to use
135 * when translating src.
138 unsigned kdp_vm_read(
143 addr64_t cur_virt_src
, cur_virt_dst
;
144 addr64_t cur_phys_src
, cur_phys_dst
;
149 #ifdef KDP_VM_READ_DEBUG
150 kprintf("kdp_vm_read1: src %x dst %x len %x - %08X %08X\n", src
, dst
, len
, ((unsigned long *)src
)[0], ((unsigned long *)src
)[1]);
153 cur_virt_src
= (addr64_t
)((unsigned int)src
);
154 cur_virt_dst
= (addr64_t
)((unsigned int)dst
);
159 resid
= len
; /* Get the length to copy */
163 if((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
167 if(!mapping_phys_lookup((ppnum_t
)(cur_virt_src
>> 12), &dummy
)) return 0; /* Can't read where there's not any memory */
169 cnt
= 4096 - (cur_virt_src
& 0xFFF); /* Get length left on page */
170 if (cnt
> (4096 - (cur_virt_dst
& 0xFFF)))
171 cnt
= 4096 - (cur_virt_dst
& 0xFFF);
173 if (cnt
> resid
) cnt
= resid
;
175 bcopy_phys(cur_virt_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
186 if(kdp_pmap
) pmap
= kdp_pmap
; /* If special pmap, use it */
187 else pmap
= kernel_pmap
; /* otherwise, use kernel's */
190 /* Always translate the destination using the kernel_pmap. */
191 if((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
194 if((cur_phys_src
= kdp_vtophys(pmap
, cur_virt_src
)) == 0)
198 if(!mapping_phys_lookup((ppnum_t
)(cur_phys_src
>> 12), &dummy
)) goto exit
; /* Can't read where there's not any memory */
200 cnt
= 4096 - (cur_virt_src
& 0xFFF); /* Get length left on page */
201 if (cnt
> (4096 - (cur_virt_dst
& 0xFFF)))
202 cnt
= 4096 - (cur_virt_dst
& 0xFFF);
204 if (cnt
> resid
) cnt
= resid
;
206 #ifdef KDP_VM_READ_DEBUG
207 kprintf("kdp_vm_read2: pmap %08X, virt %016LLX, phys %016LLX\n",
208 pmap
, cur_virt_src
, cur_phys_src
);
211 bcopy_phys(cur_phys_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
219 #ifdef KDP_VM_READ_DEBUG
220 kprintf("kdp_vm_read: ret %08X\n", len
-resid
);
222 return (len
- resid
);
228 unsigned kdp_vm_write(
233 addr64_t cur_virt_src
, cur_virt_dst
;
234 addr64_t cur_phys_src
, cur_phys_dst
;
235 unsigned resid
, cnt
, cnt_src
, cnt_dst
;
237 #ifdef KDP_VM_WRITE_DEBUG
238 printf("kdp_vm_write: src %x dst %x len %x - %08X %08X\n", src
, dst
, len
, ((unsigned long *)src
)[0], ((unsigned long *)src
)[1]);
241 cur_virt_src
= (addr64_t
)((unsigned int)src
);
242 cur_virt_dst
= (addr64_t
)((unsigned int)dst
);
247 if ((cur_phys_dst
= kdp_vtophys(kernel_pmap
, cur_virt_dst
)) == 0)
250 if ((cur_phys_src
= kdp_vtophys(kernel_pmap
, cur_virt_src
)) == 0)
253 cnt_src
= ((cur_phys_src
+ NBPG
) & (-NBPG
)) - cur_phys_src
;
254 cnt_dst
= ((cur_phys_dst
+ NBPG
) & (-NBPG
)) - cur_phys_dst
;
256 if (cnt_src
> cnt_dst
)
263 bcopy_phys(cur_phys_src
, cur_phys_dst
, cnt
); /* Copy stuff over */
264 sync_cache64(cur_phys_dst
, cnt
); /* Sync caches */
271 return (len
- resid
);
276 kern_collectth_state(thread_t thread
, tir_t
*t
)
280 mythread_state_flavor_t
*flavors
;
281 struct thread_command
*tc
;
283 * Fill in thread command structure.
286 hoffset
= t
->hoffset
;
287 flavors
= t
->flavors
;
289 tc
= (struct thread_command
*) (header
+ hoffset
);
291 tc
->cmdsize
= sizeof(struct thread_command
)
293 hoffset
+= sizeof(struct thread_command
);
295 * Follow with a struct thread_state_flavor and
296 * the appropriate thread state struct for each
297 * thread state flavor.
299 for (i
= 0; i
< kdp_mynum_flavors
; i
++) {
300 *(mythread_state_flavor_t
*)(header
+hoffset
) =
302 hoffset
+= sizeof(mythread_state_flavor_t
);
304 if (machine_thread_get_kern_state(thread
, flavors
[i
].flavor
,
305 (thread_state_t
) (header
+hoffset
),
306 &flavors
[i
].count
) != KERN_SUCCESS
)
307 printf ("Failure in machine_thread_get_kern_state()\n");
308 hoffset
+= flavors
[i
].count
*sizeof(int);
311 t
->hoffset
= hoffset
;
317 __unused
struct savearea
*regs
)
319 printf ("An unexpected trap (type %d) occurred during the kernel dump, terminating.\n", type
);
320 kdp_send_panic_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
321 abort_panic_transfer();
322 kdp_flag
&= ~KDP_PANIC_DUMP_ENABLED
;
323 kdp_flag
&= ~PANIC_CORE_ON_NMI
;
324 kdp_flag
&= ~PANIC_LOG_DUMP
;
328 kdp_raise_exception(EXC_BAD_ACCESS
, 0, 0, kdp
.saved_state
);
333 * Kernel dump (limited to currently executing 32 bit mach_kernel only)
340 unsigned int thread_count
, segment_count
;
341 unsigned int command_size
= 0, header_size
= 0, tstate_size
= 0;
342 unsigned int hoffset
= 0, foffset
= 0, nfoffset
= 0, vmoffset
= 0;
343 unsigned int max_header_size
= 0;
345 struct mach_header
*mh
;
346 struct segment_command
*sc
;
349 vm_prot_t maxprot
= 0;
350 vm_inherit_t inherit
= 0;
352 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
355 int nesting_depth
= 0;
356 kern_return_t kret
= 0;
357 struct vm_region_submap_info_64 vbr
;
362 unsigned int txstart
= 0;
363 unsigned int mach_section_count
= 4;
364 unsigned int num_sects_txed
= 0;
367 not_in_kdp
= 0; /* Tell vm functions not to acquire locks */
370 segment_count
= get_vmmap_entries(map
);
372 printf("Kernel map has %d entries\n", segment_count
);
374 nflavors
= kdp_mynum_flavors
;
375 bcopy((char *)thread_flavor_array
,(char *) flavors
,sizeof(thread_flavor_array
));
377 for (i
= 0; i
< nflavors
; i
++)
378 tstate_size
+= sizeof(mythread_state_flavor_t
) +
379 (flavors
[i
].count
* sizeof(int));
381 command_size
= (segment_count
+ mach_section_count
) *
382 sizeof(struct segment_command
) +
383 thread_count
*sizeof(struct thread_command
) +
384 tstate_size
*thread_count
;
386 header_size
= command_size
+ sizeof(struct mach_header
);
387 header
= (vm_offset_t
) command_buffer
;
390 * Set up Mach-O header for currently executing 32 bit kernel.
392 printf ("Generated Mach-O header size was %d\n", header_size
);
394 mh
= (struct mach_header
*) header
;
395 mh
->magic
= MH_MAGIC
;
396 mh
->cputype
= cpu_type();
397 mh
->cpusubtype
= cpu_subtype(); /* XXX incorrect; should match kernel */
398 mh
->filetype
= MH_CORE
;
399 mh
->ncmds
= segment_count
+ thread_count
+ mach_section_count
;
400 mh
->sizeofcmds
= command_size
;
403 hoffset
= sizeof(struct mach_header
); /* offset into header */
404 foffset
= round_page_32(header_size
); /* offset into file */
406 if ((foffset
- header_size
) < (4*sizeof(struct segment_command
))) {
408 foffset
+= ((4*sizeof(struct segment_command
)) - (foffset
-header_size
));
411 max_header_size
= foffset
;
413 vmoffset
= VM_MIN_ADDRESS
; /* offset into VM */
415 /* Transmit the Mach-O MH_CORE header, and seek forward past the
416 * area reserved for the segment and thread commands
417 * to begin data transmission
420 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(nfoffset
) , &nfoffset
)) < 0) {
421 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
425 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, sizeof(struct mach_header
), (caddr_t
) mh
) < 0)) {
426 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
430 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
) < 0)) {
431 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
434 printf ("Transmitting kernel state, please wait: ");
436 while ((segment_count
> 0) || (kret
== KERN_SUCCESS
)){
437 /* Check if we've transmitted all the kernel sections */
438 if (num_sects_txed
== mach_section_count
-1) {
443 * Get region information for next region.
446 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
447 if((kret
= vm_region_recurse_64(map
,
448 &vmoffset
, &size
, &nesting_depth
,
449 (vm_region_recurse_info_t
)&vbr
,
450 &vbrcount
)) != KERN_SUCCESS
) {
462 if(kret
!= KERN_SUCCESS
)
465 prot
= vbr
.protection
;
466 maxprot
= vbr
.max_protection
;
467 inherit
= vbr
.inheritance
;
471 switch (num_sects_txed
) {
474 /* Transmit the kernel text section */
475 vmoffset
= sectTEXTB
;
481 vmoffset
= sectDATAB
;
487 vmoffset
= sectPRELINKB
;
488 size
= sectSizePRELINK
;
493 vmoffset
= sectLINKB
;
497 /* TODO the lowmem vector area may be useful, but its transmission is
498 * disabled for now. The traceback table area should be transmitted
499 * as well - that's indirected from 0x5080.
505 * Fill in segment command structure.
508 if (hoffset
> max_header_size
)
510 sc
= (struct segment_command
*) (header
);
511 sc
->cmd
= LC_SEGMENT
;
512 sc
->cmdsize
= sizeof(struct segment_command
);
514 sc
->vmaddr
= vmoffset
;
516 sc
->fileoff
= foffset
;
518 sc
->maxprot
= maxprot
;
522 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
523 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
527 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, sizeof(struct segment_command
) , (caddr_t
) sc
)) < 0) {
528 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
532 /* Do not transmit memory tagged VM_MEMORY_IOKIT - instead, seek past that
533 * region on the server - this creates a hole in the file
536 if ((vbr
.user_tag
!= VM_MEMORY_IOKIT
)) {
538 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
)) < 0) {
539 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
545 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, size
, (caddr_t
) txstart
)) < 0) {
546 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
551 hoffset
+= sizeof(struct segment_command
);
556 tir1
.header
= header
;
558 tir1
.flavors
= flavors
;
559 tir1
.tstate_size
= tstate_size
;
561 /* Now send out the LC_THREAD load command, with the thread information
562 * for the current activation.
563 * Note that the corefile can contain LC_SEGMENT commands with file offsets
564 * that point past the edge of the corefile, in the event that the last N
565 * VM regions were all I/O mapped or otherwise non-transferable memory,
566 * not followed by a normal VM region; i.e. there will be no hole that
567 * reaches to the end of the core file.
569 kern_collectth_state (current_thread(), &tir1
);
571 if ((panic_error
= kdp_send_panic_pkt (KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
572 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);
576 if ((panic_error
= kdp_send_panic_packets (KDP_DATA
, NULL
, tir1
.hoffset
, (caddr_t
) header
)) < 0) {
577 printf ("kdp_send_panic_packets failed with error %d\n", panic_error
);
582 if ((panic_error
= kdp_send_panic_pkt (KDP_EOF
, NULL
, 0, ((void *) 0))) < 0)
584 printf ("kdp_send_panic_pkt failed with error %d\n", panic_error
);