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_state64_t *regs); |
41 | |
42 | static const x86_state_hdr_t thread_flavor_array [] = { |
43 | {x86_THREAD_STATE64, x86_THREAD_STATE64_COUNT} |
44 | }; |
45 | |
46 | void |
47 | kern_collectth_state_size(uint64_t * tstate_count, uint64_t * ptstate_size) |
48 | { |
49 | unsigned int i; |
50 | uint64_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 | *tstate_count = 1; |
57 | *ptstate_size = sizeof(struct thread_command) + tstate_size; |
58 | } |
59 | |
60 | void |
61 | kern_collectth_state(thread_t thread, void *buffer, uint64_t size, void ** iter) |
62 | { |
63 | size_t hoffset; |
64 | uint64_t tstate_size, tstate_count; |
65 | unsigned int i; |
66 | struct thread_command *tc; |
67 | |
68 | |
69 | *iter = NULL; |
70 | /* |
71 | * Fill in thread command structure. |
72 | */ |
73 | hoffset = 0; |
74 | |
75 | if (hoffset + sizeof(struct thread_command) > size) |
76 | return; |
77 | |
78 | kern_collectth_state_size(&tstate_count, &tstate_size); |
79 | tc = (struct thread_command *) ((uintptr_t)buffer + hoffset); |
80 | tc->cmd = LC_THREAD; |
81 | tc->cmdsize = (uint32_t) tstate_size; |
82 | hoffset += sizeof(struct thread_command); |
83 | /* |
84 | * Follow with a struct thread_state_flavor and |
85 | * the appropriate thread state struct for each |
86 | * thread state flavor. |
87 | */ |
88 | for (i = 0; i < sizeof(thread_flavor_array)/sizeof(thread_flavor_array[0]); i++) { |
89 | |
90 | if (hoffset + sizeof(x86_state_hdr_t) > size) |
91 | return; |
92 | |
93 | *(x86_state_hdr_t *)((uintptr_t)buffer + hoffset) = |
94 | thread_flavor_array[i]; |
95 | hoffset += sizeof(x86_state_hdr_t); |
96 | |
97 | |
98 | if (hoffset + thread_flavor_array[i].count*sizeof(int) > size) |
99 | return; |
100 | |
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 | */ |
105 | if (thread_flavor_array[i].flavor == x86_THREAD_STATE64) { |
106 | x86_thread_state64_t *tstate = (x86_thread_state64_t *) ((uintptr_t)buffer + hoffset); |
107 | vm_offset_t kstack; |
108 | x86_saved_state64_t *cpstate = current_cpu_datap()->cpu_fatal_trap_state; |
109 | |
110 | bzero(tstate, x86_THREAD_STATE64_COUNT * sizeof(int)); |
111 | if ((current_thread() == thread) && (cpstate != NULL)) { |
112 | tstate->rax = cpstate->rax; |
113 | tstate->rbx = cpstate->rbx; |
114 | tstate->rcx = cpstate->rcx; |
115 | tstate->rdx = cpstate->rdx; |
116 | tstate->rdi = cpstate->rdi; |
117 | tstate->rsi = cpstate->rsi; |
118 | tstate->rbp = cpstate->rbp; |
119 | tstate->r8 = cpstate->r8; |
120 | tstate->r9 = cpstate->r9; |
121 | tstate->r10 = cpstate->r10; |
122 | tstate->r11 = cpstate->r11; |
123 | tstate->r12 = cpstate->r12; |
124 | tstate->r13 = cpstate->r13; |
125 | tstate->r14 = cpstate->r14; |
126 | tstate->r15 = cpstate->r15; |
127 | tstate->rip = cpstate->isf.rip; |
128 | tstate->rsp = cpstate->isf.rsp; |
129 | tstate->rflags = cpstate->isf.rflags; |
130 | tstate->cs = cpstate->isf.cs; |
131 | tstate->fs = cpstate->fs; |
132 | tstate->gs = cpstate->gs; |
133 | } else if ((kstack = thread->kernel_stack) != 0){ |
134 | struct x86_kernel_state *iks = STACK_IKS(kstack); |
135 | tstate->rbx = iks->k_rbx; |
136 | tstate->rsp = iks->k_rsp; |
137 | tstate->rbp = iks->k_rbp; |
138 | tstate->r12 = iks->k_r12; |
139 | tstate->r13 = iks->k_r13; |
140 | tstate->r14 = iks->k_r14; |
141 | tstate->r15 = iks->k_r15; |
142 | tstate->rip = iks->k_rip; |
143 | } |
144 | } else { |
145 | void *tstate = (void *)((uintptr_t)buffer + hoffset); |
146 | |
147 | bzero(tstate, thread_flavor_array[i].count*sizeof(int)); |
148 | } |
149 | |
150 | hoffset += thread_flavor_array[i].count*sizeof(int); |
151 | } |
152 | } |
153 | |
154 | /* Intended to be called from the kernel trap handler if an unrecoverable fault |
155 | * occurs during a crashdump (which shouldn't happen since we validate mappings |
156 | * and so on). This should be reworked to attempt some form of recovery. |
157 | */ |
158 | int |
159 | kdp_dump_trap( |
160 | int type, |
161 | __unused x86_saved_state64_t *saved_state) |
162 | { |
163 | printf ("An unexpected trap (type %d) occurred during the system dump, terminating.\n" , type); |
164 | kdp_send_crashdump_pkt (KDP_EOF, NULL, 0, ((void *) 0)); |
165 | abort_panic_transfer(); |
166 | kdp_flag &= ~KDP_PANIC_DUMP_ENABLED; |
167 | kdp_flag &= ~PANIC_CORE_ON_NMI; |
168 | kdp_flag &= ~PANIC_LOG_DUMP; |
169 | |
170 | kdp_reset(); |
171 | |
172 | kdp_raise_exception(EXC_BAD_ACCESS, 0, 0, kdp.saved_state); |
173 | return( 0 ); |
174 | } |
175 | |