]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kdp/ml/i386/kdp_vm.c
58f614d067772633b75121045d14c10d7330a2e0
[apple/xnu.git] / osfmk / kdp / ml / i386 / kdp_vm.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #include <mach/mach_types.h>
29 #include <mach/vm_attributes.h>
30 #include <mach/vm_param.h>
31 #include <libsa/types.h>
32
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>
39
40 int kdp_dump_trap(int type, x86_saved_state32_t *regs);
41
42 static const x86_state_hdr_t thread_flavor_array [] = {
43 {x86_THREAD_STATE32, x86_THREAD_STATE32_COUNT}
44 };
45
46 size_t
47 kern_collectth_state_size(void)
48 {
49 unsigned int i;
50 size_t tstate_size = 0;
51
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));
55
56 return tstate_size;
57 }
58
59 void
60 kern_collectth_state(thread_t thread, void *buffer, size_t size)
61 {
62 size_t hoffset;
63 unsigned int i;
64 struct thread_command *tc;
65
66 /*
67 * Fill in thread command structure.
68 */
69 hoffset = 0;
70
71 if (hoffset + sizeof(struct thread_command) > size)
72 return;
73
74 tc = (struct thread_command *) ((uintptr_t)buffer + hoffset);
75 tc->cmd = LC_THREAD;
76 tc->cmdsize = sizeof(struct thread_command) + kern_collectth_state_size();
77 hoffset += sizeof(struct thread_command);
78 /*
79 * Follow with a struct thread_state_flavor and
80 * the appropriate thread state struct for each
81 * thread state flavor.
82 */
83 for (i = 0; i < sizeof(thread_flavor_array)/sizeof(thread_flavor_array[0]); i++) {
84
85 if (hoffset + sizeof(x86_state_hdr_t) > size)
86 return;
87
88 *(x86_state_hdr_t *)((uintptr_t)buffer + hoffset) =
89 thread_flavor_array[i];
90 hoffset += sizeof(x86_state_hdr_t);
91
92
93 if (hoffset + thread_flavor_array[i].count*sizeof(int) > size)
94 return;
95
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.
101 */
102 if (thread_flavor_array[i].flavor == x86_THREAD_STATE32) {
103 x86_thread_state32_t *tstate = (x86_thread_state32_t *) ((uintptr_t)buffer + hoffset);
104 vm_offset_t kstack;
105
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;
115 }
116 } else {
117 void *tstate = (void *)((uintptr_t)buffer + hoffset);
118
119 bzero(tstate, thread_flavor_array[i].count*sizeof(int));
120 }
121
122 hoffset += thread_flavor_array[i].count*sizeof(int);
123 }
124 }
125
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.
129 */
130 int
131 kdp_dump_trap(
132 int type,
133 __unused x86_saved_state32_t *saved_state)
134 {
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;
141
142 kdp_reset();
143
144 kdp_raise_exception(EXC_BAD_ACCESS, 0, 0, kdp.saved_state);
145 return( 0 );
146 }