2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <mach/mach_types.h>
29 #include <mach/vm_attributes.h>
30 #include <mach/vm_param.h>
31 #include <libsa/types.h>
33 #include <vm/vm_map.h>
34 #include <i386/pmap.h>
36 #include <kdp/kdp_core.h>
37 #include <kdp/kdp_internal.h>
38 #include <mach-o/loader.h>
39 #include <mach/vm_map.h>
40 #include <mach/vm_statistics.h>
41 #include <mach/thread_status.h>
42 #include <i386/thread.h>
44 #include <vm/vm_protos.h>
45 #include <vm/vm_kern.h>
48 int kdp_dump_trap(int type
, x86_saved_state32_t
*regs
);
51 int flavor
; /* the number for this flavor */
52 mach_msg_type_number_t count
; /* count of ints in this flavor */
53 } mythread_state_flavor_t
;
55 static mythread_state_flavor_t thread_flavor_array
[] = {
56 {x86_THREAD_STATE32
, x86_THREAD_STATE32_COUNT
}
59 static int kdp_mynum_flavors
= 1;
60 static int MAX_TSTATE_FLAVORS
= 1;
65 mythread_state_flavor_t
*flavors
;
69 char command_buffer
[512];
72 kern_collectth_state(thread_t thread
, tir_t
*t
)
76 mythread_state_flavor_t
*flavors
;
77 struct thread_command
*tc
;
79 * Fill in thread command structure.
85 tc
= (struct thread_command
*) (header
+ hoffset
);
87 tc
->cmdsize
= sizeof(struct thread_command
) + t
->tstate_size
;
88 hoffset
+= sizeof(struct thread_command
);
90 * Follow with a struct thread_state_flavor and
91 * the appropriate thread state struct for each
92 * thread state flavor.
94 for (i
= 0; i
< kdp_mynum_flavors
; i
++) {
95 *(mythread_state_flavor_t
*)(header
+hoffset
) =
97 hoffset
+= sizeof(mythread_state_flavor_t
);
98 /* Locate and obtain the non-volatile register context
99 * for this kernel thread. This should ideally be
100 * encapsulated in machine_thread_get_kern_state()
101 * but that routine appears to have been co-opted
102 * by CHUD to obtain pre-interrupt state.
104 if (flavors
[i
].flavor
== x86_THREAD_STATE32
) {
105 x86_thread_state32_t
*tstate
= (x86_thread_state32_t
*) (header
+ hoffset
);
107 bzero(tstate
, x86_THREAD_STATE32_COUNT
* sizeof(int));
108 if ((kstack
= thread
->kernel_stack
) != 0){
109 struct x86_kernel_state
*iks
= STACK_IKS(kstack
);
110 tstate
->ebx
= iks
->k_ebx
;
111 tstate
->esp
= iks
->k_esp
;
112 tstate
->ebp
= iks
->k_ebp
;
113 tstate
->edi
= iks
->k_edi
;
114 tstate
->esi
= iks
->k_esi
;
115 tstate
->eip
= iks
->k_eip
;
118 else if (machine_thread_get_kern_state(thread
,
119 flavors
[i
].flavor
, (thread_state_t
) (header
+hoffset
),
120 &flavors
[i
].count
) != KERN_SUCCESS
)
121 printf ("Failure in machine_thread_get_kern_state()\n");
122 hoffset
+= flavors
[i
].count
*sizeof(int);
125 t
->hoffset
= hoffset
;
128 /* Intended to be called from the kernel trap handler if an unrecoverable fault
129 * occurs during a crashdump (which shouldn't happen since we validate mappings
130 * and so on). This should be reworked to attempt some form of recovery.
135 __unused x86_saved_state32_t
*saved_state
)
137 printf ("An unexpected trap (type %d) occurred during the system dump, terminating.\n", type
);
138 kdp_send_crashdump_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
139 abort_panic_transfer();
140 kdp_flag
&= ~KDP_PANIC_DUMP_ENABLED
;
141 kdp_flag
&= ~PANIC_CORE_ON_NMI
;
142 kdp_flag
&= ~PANIC_LOG_DUMP
;
146 kdp_raise_exception(EXC_BAD_ACCESS
, 0, 0, kdp
.saved_state
);
154 unsigned int thread_count
, segment_count
;
155 unsigned int command_size
= 0, header_size
= 0, tstate_size
= 0;
157 uint64_t hoffset
= 0, foffset
= 0, nfoffset
= 0, max_header_size
;
158 vm_offset_t header
, txstart
;
159 vm_address_t vmoffset
;
161 struct mach_header
*mh
;
162 struct segment_command
*sc
;
165 vm_prot_t maxprot
= 0;
166 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
169 uint32_t nesting_depth
= 0;
170 kern_return_t kret
= 0;
171 struct vm_region_submap_info_64 vbr
;
172 mach_msg_type_number_t vbrcount
= 0;
181 segment_count
= get_vmmap_entries(map
);
183 printf("Kernel map has %d entries\n", segment_count
);
185 nflavors
= kdp_mynum_flavors
;
186 bcopy((char *)thread_flavor_array
,(char *) flavors
,sizeof(thread_flavor_array
));
188 for (i
= 0; i
< nflavors
; i
++)
189 tstate_size
+= sizeof(mythread_state_flavor_t
) +
190 (flavors
[i
].count
* sizeof(int));
192 command_size
= (segment_count
) *
193 sizeof(struct segment_command
) +
194 thread_count
* sizeof(struct thread_command
) +
195 tstate_size
* thread_count
;
197 header_size
= command_size
+ sizeof(struct mach_header
);
198 header
= (vm_offset_t
) command_buffer
;
201 * Set up Mach-O header for currently executing 32 bit kernel.
203 printf ("Generated Mach-O header size was %d\n", header_size
);
205 mh
= (struct mach_header
*) header
;
206 mh
->magic
= MH_MAGIC
;
207 mh
->cputype
= cpu_type();
208 mh
->cpusubtype
= cpu_subtype();
209 mh
->filetype
= MH_CORE
;
210 mh
->ncmds
= segment_count
+ thread_count
;
211 mh
->sizeofcmds
= command_size
;
214 hoffset
= sizeof(struct mach_header
); /* offset into header */
215 foffset
= round_page_32(header_size
); /* offset into file */
217 if ((foffset
- header_size
) < (4*sizeof(struct segment_command
))) {
218 foffset
+= ((4*sizeof(struct segment_command
)) - (foffset
-header_size
));
221 max_header_size
= foffset
;
223 vmoffset
= VM_MIN_KERNEL_ADDRESS
; /* offset into VM */
225 /* Transmit the Mach-O MH_CORE header, and seek forward past the
226 * area reserved for the segment and thread commands
227 * to begin data transmission
230 if ((panic_error
= kdp_send_crashdump_pkt (KDP_SEEK
, NULL
, sizeof(nfoffset
) , &nfoffset
)) < 0) {
231 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
236 if ((panic_error
= kdp_send_crashdump_data (KDP_DATA
, NULL
, sizeof(struct mach_header
), (caddr_t
) mh
) < 0)) {
237 printf ("kdp_send_crashdump_data failed with error %d\n", panic_error
);
242 if ((panic_error
= kdp_send_crashdump_pkt (KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
) < 0)) {
243 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
247 printf ("Transmitting kernel state:\n");
249 while ((segment_count
> 0) || (kret
== KERN_SUCCESS
)) {
253 * Get region information for next region.
256 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
257 if((kret
= vm_region_recurse_64(map
,
258 &vmoffset
, &size
, &nesting_depth
,
259 (vm_region_recurse_info_t
)&vbr
,
260 &vbrcount
)) != KERN_SUCCESS
) {
272 if(kret
!= KERN_SUCCESS
)
275 prot
= vbr
.protection
;
276 maxprot
= vbr
.max_protection
;
278 * Fill in segment command structure.
281 if (hoffset
> max_header_size
)
283 sc
= (struct segment_command
*) (header
);
284 sc
->cmd
= LC_SEGMENT
;
285 sc
->cmdsize
= sizeof(struct segment_command
);
287 sc
->vmaddr
= vmoffset
;
289 sc
->fileoff
= (uint32_t) foffset
;
291 sc
->maxprot
= maxprot
;
295 if ((panic_error
= kdp_send_crashdump_pkt (KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
296 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
301 if ((panic_error
= kdp_send_crashdump_data (KDP_DATA
, NULL
, sizeof(struct segment_command
) , (caddr_t
) sc
)) < 0) {
302 printf ("kdp_send_crashdump_data failed with error %d\n", panic_error
);
307 /* Do not transmit memory tagged VM_MEMORY_IOKIT - instead,
308 * seek past that region on the server - this creates a
312 if ((vbr
.user_tag
!= VM_MEMORY_IOKIT
)) {
314 if ((panic_error
= kdp_send_crashdump_pkt (KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
)) < 0) {
315 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
322 if ((panic_error
= kdp_send_crashdump_data (KDP_DATA
, NULL
, size
, (caddr_t
) txstart
)) < 0) {
323 printf ("kdp_send_crashdump_data failed with error %d\n", panic_error
);
329 hoffset
+= sizeof(struct segment_command
);
334 tir1
.header
= header
;
336 tir1
.flavors
= flavors
;
337 tir1
.tstate_size
= tstate_size
;
339 /* Now send out the LC_THREAD load command, with the thread information
340 * for the current activation.
341 * Note that the corefile can contain LC_SEGMENT commands with file
342 * offsets that point past the edge of the corefile, in the event that
343 * the last N VM regions were all I/O mapped or otherwise
344 * non-transferable memory, not followed by a normal VM region;
345 * i.e. there will be no hole that reaches to the end of the core file.
347 kern_collectth_state (current_thread(), &tir1
);
349 if ((panic_error
= kdp_send_crashdump_pkt (KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
350 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
355 if ((panic_error
= kdp_send_crashdump_data (KDP_DATA
, NULL
, tir1
.hoffset
, (caddr_t
) header
)) < 0) {
356 printf ("kdp_send_crashdump_data failed with error %d\n", panic_error
);
362 if ((panic_error
= kdp_send_crashdump_pkt (KDP_EOF
, NULL
, 0, ((void *) 0))) < 0) {
363 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);