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>
47 extern vm_offset_t sectTEXTB
, sectDATAB
, sectLINKB
, sectPRELINKB
;
48 extern unsigned long sectSizeTEXT
, sectSizeDATA
, sectSizeLINK
, sectSizePRELINK
;
51 int kdp_dump_trap(int type
, x86_saved_state32_t
*regs
);
54 int flavor
; /* the number for this flavor */
55 mach_msg_type_number_t count
; /* count of ints in this flavor */
56 } mythread_state_flavor_t
;
58 static mythread_state_flavor_t thread_flavor_array
[] = {
59 {x86_THREAD_STATE32
, x86_THREAD_STATE32_COUNT
}
62 static int kdp_mynum_flavors
= 1;
63 static int MAX_TSTATE_FLAVORS
= 1;
68 mythread_state_flavor_t
*flavors
;
72 char command_buffer
[512];
75 kern_collectth_state(thread_t thread
, tir_t
*t
)
79 mythread_state_flavor_t
*flavors
;
80 struct thread_command
*tc
;
82 * Fill in thread command structure.
88 tc
= (struct thread_command
*) (header
+ hoffset
);
90 tc
->cmdsize
= sizeof(struct thread_command
) + t
->tstate_size
;
91 hoffset
+= sizeof(struct thread_command
);
93 * Follow with a struct thread_state_flavor and
94 * the appropriate thread state struct for each
95 * thread state flavor.
97 for (i
= 0; i
< kdp_mynum_flavors
; i
++) {
98 *(mythread_state_flavor_t
*)(header
+hoffset
) =
100 hoffset
+= sizeof(mythread_state_flavor_t
);
101 /* Locate and obtain the non-volatile register context
102 * for this kernel thread. This should ideally be
103 * encapsulated in machine_thread_get_kern_state()
104 * but that routine appears to have been co-opted
105 * by CHUD to obtain pre-interrupt state.
107 if (flavors
[i
].flavor
== x86_THREAD_STATE32
) {
108 x86_thread_state32_t
*tstate
= (x86_thread_state32_t
*) (header
+ hoffset
);
110 bzero(tstate
, x86_THREAD_STATE32_COUNT
* sizeof(int));
111 if ((kstack
= thread
->kernel_stack
) != 0){
112 struct x86_kernel_state
*iks
= STACK_IKS(kstack
);
113 tstate
->ebx
= iks
->k_ebx
;
114 tstate
->esp
= iks
->k_esp
;
115 tstate
->ebp
= iks
->k_ebp
;
116 tstate
->edi
= iks
->k_edi
;
117 tstate
->esi
= iks
->k_esi
;
118 tstate
->eip
= iks
->k_eip
;
121 else if (machine_thread_get_kern_state(thread
,
122 flavors
[i
].flavor
, (thread_state_t
) (header
+hoffset
),
123 &flavors
[i
].count
) != KERN_SUCCESS
)
124 printf ("Failure in machine_thread_get_kern_state()\n");
125 hoffset
+= flavors
[i
].count
*sizeof(int);
128 t
->hoffset
= hoffset
;
131 /* Intended to be called from the kernel trap handler if an unrecoverable fault
132 * occurs during a crashdump (which shouldn't happen since we validate mappings
133 * and so on). This should be reworked to attempt some form of recovery.
138 __unused x86_saved_state32_t
*saved_state
)
140 printf ("An unexpected trap (type %d) occurred during the system dump, terminating.\n", type
);
141 kdp_send_crashdump_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
142 abort_panic_transfer();
143 kdp_flag
&= ~KDP_PANIC_DUMP_ENABLED
;
144 kdp_flag
&= ~PANIC_CORE_ON_NMI
;
145 kdp_flag
&= ~PANIC_LOG_DUMP
;
149 kdp_raise_exception(EXC_BAD_ACCESS
, 0, 0, kdp
.saved_state
);
157 unsigned int thread_count
, segment_count
;
158 unsigned int command_size
= 0, header_size
= 0, tstate_size
= 0;
159 unsigned int hoffset
= 0, foffset
= 0, nfoffset
= 0, vmoffset
= 0;
160 unsigned int max_header_size
= 0;
162 struct mach_header
*mh
;
163 struct segment_command
*sc
;
166 vm_prot_t maxprot
= 0;
167 vm_inherit_t inherit
= 0;
168 mythread_state_flavor_t flavors
[MAX_TSTATE_FLAVORS
];
171 uint32_t nesting_depth
= 0;
172 kern_return_t kret
= 0;
173 struct vm_region_submap_info_64 vbr
;
174 mach_msg_type_number_t vbrcount
= 0;
179 unsigned int txstart
= 0;
180 unsigned int mach_section_count
= 4;
181 unsigned int num_sects_txed
= 0;
186 segment_count
= get_vmmap_entries(map
);
188 printf("Kernel map has %d entries\n", segment_count
);
190 nflavors
= kdp_mynum_flavors
;
191 bcopy((char *)thread_flavor_array
,(char *) flavors
,sizeof(thread_flavor_array
));
193 for (i
= 0; i
< nflavors
; i
++)
194 tstate_size
+= sizeof(mythread_state_flavor_t
) +
195 (flavors
[i
].count
* sizeof(int));
197 command_size
= (segment_count
+ mach_section_count
) *
198 sizeof(struct segment_command
) +
199 thread_count
* sizeof(struct thread_command
) +
200 tstate_size
* thread_count
;
202 header_size
= command_size
+ sizeof(struct mach_header
);
203 header
= (vm_offset_t
) command_buffer
;
206 * Set up Mach-O header for currently executing 32 bit kernel.
208 printf ("Generated Mach-O header size was %d\n", header_size
);
210 mh
= (struct mach_header
*) header
;
211 mh
->magic
= MH_MAGIC
;
212 mh
->cputype
= cpu_type();
213 mh
->cpusubtype
= cpu_subtype();
214 mh
->filetype
= MH_CORE
;
215 mh
->ncmds
= segment_count
+ thread_count
+ mach_section_count
;
216 mh
->sizeofcmds
= command_size
;
219 hoffset
= sizeof(struct mach_header
); /* offset into header */
220 foffset
= round_page_32(header_size
); /* offset into file */
222 if ((foffset
- header_size
) < (4*sizeof(struct segment_command
))) {
223 foffset
+= ((4*sizeof(struct segment_command
)) - (foffset
-header_size
));
226 max_header_size
= foffset
;
228 vmoffset
= VM_MIN_ADDRESS
; /* offset into VM */
230 /* Transmit the Mach-O MH_CORE header, and seek forward past the
231 * area reserved for the segment and thread commands
232 * to begin data transmission
235 if ((panic_error
= kdp_send_crashdump_pkt (KDP_SEEK
, NULL
, sizeof(nfoffset
) , &nfoffset
)) < 0) {
236 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
241 if ((panic_error
= kdp_send_crashdump_data (KDP_DATA
, NULL
, sizeof(struct mach_header
), (caddr_t
) mh
) < 0)) {
242 printf ("kdp_send_crashdump_data failed with error %d\n", panic_error
);
247 if ((panic_error
= kdp_send_crashdump_pkt (KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
) < 0)) {
248 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
252 printf ("Transmitting kernel state, please wait: ");
254 while ((segment_count
> 0) || (kret
== KERN_SUCCESS
)){
255 /* Check if we've transmitted all the kernel sections */
256 if (num_sects_txed
== mach_section_count
) {
261 * Get region information for next region.
264 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
265 if((kret
= vm_region_recurse_64(map
,
266 &vmoffset
, &size
, &nesting_depth
,
267 (vm_region_recurse_info_t
)&vbr
,
268 &vbrcount
)) != KERN_SUCCESS
) {
280 if(kret
!= KERN_SUCCESS
)
283 prot
= vbr
.protection
;
284 maxprot
= vbr
.max_protection
;
285 inherit
= vbr
.inheritance
;
289 switch (num_sects_txed
) {
291 /* Transmit the kernel text section */
292 vmoffset
= sectTEXTB
;
296 vmoffset
= sectDATAB
;
300 vmoffset
= sectPRELINKB
;
301 size
= sectSizePRELINK
;
304 vmoffset
= sectLINKB
;
311 * Fill in segment command structure.
314 if (hoffset
> max_header_size
)
316 sc
= (struct segment_command
*) (header
);
317 sc
->cmd
= LC_SEGMENT
;
318 sc
->cmdsize
= sizeof(struct segment_command
);
320 sc
->vmaddr
= vmoffset
;
322 sc
->fileoff
= foffset
;
324 sc
->maxprot
= maxprot
;
328 if ((panic_error
= kdp_send_crashdump_pkt (KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
329 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
334 if ((panic_error
= kdp_send_crashdump_data (KDP_DATA
, NULL
, sizeof(struct segment_command
) , (caddr_t
) sc
)) < 0) {
335 printf ("kdp_send_crashdump_data failed with error %d\n", panic_error
);
340 /* Do not transmit memory tagged VM_MEMORY_IOKIT - instead,
341 * seek past that region on the server - this creates a
345 if ((vbr
.user_tag
!= VM_MEMORY_IOKIT
)) {
347 if ((panic_error
= kdp_send_crashdump_pkt (KDP_SEEK
, NULL
, sizeof(foffset
) , &foffset
)) < 0) {
348 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
355 if ((panic_error
= kdp_send_crashdump_data (KDP_DATA
, NULL
, size
, (caddr_t
) txstart
)) < 0) {
356 printf ("kdp_send_crashdump_data failed with error %d\n", panic_error
);
362 hoffset
+= sizeof(struct segment_command
);
367 tir1
.header
= header
;
369 tir1
.flavors
= flavors
;
370 tir1
.tstate_size
= tstate_size
;
372 /* Now send out the LC_THREAD load command, with the thread information
373 * for the current activation.
374 * Note that the corefile can contain LC_SEGMENT commands with file
375 * offsets that point past the edge of the corefile, in the event that
376 * the last N VM regions were all I/O mapped or otherwise
377 * non-transferable memory, not followed by a normal VM region;
378 * i.e. there will be no hole that reaches to the end of the core file.
380 kern_collectth_state (current_thread(), &tir1
);
382 if ((panic_error
= kdp_send_crashdump_pkt (KDP_SEEK
, NULL
, sizeof(hoffset
) , &hoffset
)) < 0) {
383 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
388 if ((panic_error
= kdp_send_crashdump_data (KDP_DATA
, NULL
, tir1
.hoffset
, (caddr_t
) header
)) < 0) {
389 printf ("kdp_send_crashdump_data failed with error %d\n", panic_error
);
395 if ((panic_error
= kdp_send_crashdump_pkt (KDP_EOF
, NULL
, 0, ((void *) 0))) < 0)
397 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);