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 <kdp/kdp_core.h>
34 #include <kdp/kdp_internal.h>
35 #include <kdp/ml/i386/kdp_x86_common.h>
36 #include <mach-o/loader.h>
37 #include <mach/thread_status.h>
38 #include <i386/thread.h>
40 int kdp_dump_trap(int type
, x86_saved_state32_t
*regs
);
42 static const x86_state_hdr_t thread_flavor_array
[] = {
43 {x86_THREAD_STATE32
, x86_THREAD_STATE32_COUNT
}
47 kern_collectth_state_size(void)
50 size_t tstate_size
= 0;
52 for (i
= 0; i
< sizeof(thread_flavor_array
)/sizeof(thread_flavor_array
[0]); i
++)
53 tstate_size
+= sizeof(x86_state_hdr_t
) +
54 (thread_flavor_array
[i
].count
* sizeof(int));
60 kern_collectth_state(thread_t thread
, void *buffer
, size_t size
)
64 struct thread_command
*tc
;
67 * Fill in thread command structure.
71 if (hoffset
+ sizeof(struct thread_command
) > size
)
74 tc
= (struct thread_command
*) ((uintptr_t)buffer
+ hoffset
);
76 tc
->cmdsize
= sizeof(struct thread_command
) + kern_collectth_state_size();
77 hoffset
+= sizeof(struct thread_command
);
79 * Follow with a struct thread_state_flavor and
80 * the appropriate thread state struct for each
81 * thread state flavor.
83 for (i
= 0; i
< sizeof(thread_flavor_array
)/sizeof(thread_flavor_array
[0]); i
++) {
85 if (hoffset
+ sizeof(x86_state_hdr_t
) > size
)
88 *(x86_state_hdr_t
*)((uintptr_t)buffer
+ hoffset
) =
89 thread_flavor_array
[i
];
90 hoffset
+= sizeof(x86_state_hdr_t
);
93 if (hoffset
+ thread_flavor_array
[i
].count
*sizeof(int) > size
)
96 /* Locate and obtain the non-volatile register context
97 * for this kernel thread. This should ideally be
98 * encapsulated in machine_thread_get_kern_state()
99 * but that routine appears to have been co-opted
100 * by CHUD to obtain pre-interrupt state.
102 if (thread_flavor_array
[i
].flavor
== x86_THREAD_STATE32
) {
103 x86_thread_state32_t
*tstate
= (x86_thread_state32_t
*) ((uintptr_t)buffer
+ hoffset
);
106 bzero(tstate
, x86_THREAD_STATE32_COUNT
* sizeof(int));
107 if ((kstack
= thread
->kernel_stack
) != 0){
108 struct x86_kernel_state
*iks
= STACK_IKS(kstack
);
109 tstate
->ebx
= iks
->k_ebx
;
110 tstate
->esp
= iks
->k_esp
;
111 tstate
->ebp
= iks
->k_ebp
;
112 tstate
->edi
= iks
->k_edi
;
113 tstate
->esi
= iks
->k_esi
;
114 tstate
->eip
= iks
->k_eip
;
117 void *tstate
= (void *)((uintptr_t)buffer
+ hoffset
);
119 bzero(tstate
, thread_flavor_array
[i
].count
*sizeof(int));
122 hoffset
+= thread_flavor_array
[i
].count
*sizeof(int);
126 /* Intended to be called from the kernel trap handler if an unrecoverable fault
127 * occurs during a crashdump (which shouldn't happen since we validate mappings
128 * and so on). This should be reworked to attempt some form of recovery.
133 __unused x86_saved_state32_t
*saved_state
)
135 printf ("An unexpected trap (type %d) occurred during the system dump, terminating.\n", type
);
136 kdp_send_crashdump_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
137 abort_panic_transfer();
138 kdp_flag
&= ~KDP_PANIC_DUMP_ENABLED
;
139 kdp_flag
&= ~PANIC_CORE_ON_NMI
;
140 kdp_flag
&= ~PANIC_LOG_DUMP
;
144 kdp_raise_exception(EXC_BAD_ACCESS
, 0, 0, kdp
.saved_state
);