1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions * Copyright (c) 2015 Apple Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions *
4*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions *
6*2c2f96dcSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions *
15*2c2f96dcSApple OSS Distributions * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions *
18*2c2f96dcSApple OSS Distributions * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions * limitations under the License.
25*2c2f96dcSApple OSS Distributions *
26*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions */
28*2c2f96dcSApple OSS Distributions #include <mach/mach.h>
29*2c2f96dcSApple OSS Distributions
30*2c2f96dcSApple OSS Distributions kern_return_t
thread_get_register_pointer_values(thread_t thread,uintptr_t * sp,size_t * length,uintptr_t * values)31*2c2f96dcSApple OSS Distributions thread_get_register_pointer_values(thread_t thread, uintptr_t *sp, size_t *length, uintptr_t *values)
32*2c2f96dcSApple OSS Distributions {
33*2c2f96dcSApple OSS Distributions if (!length) {
34*2c2f96dcSApple OSS Distributions return KERN_INVALID_ARGUMENT;
35*2c2f96dcSApple OSS Distributions }
36*2c2f96dcSApple OSS Distributions if (*length > 0 && values == NULL) {
37*2c2f96dcSApple OSS Distributions return KERN_INVALID_ARGUMENT;
38*2c2f96dcSApple OSS Distributions }
39*2c2f96dcSApple OSS Distributions
40*2c2f96dcSApple OSS Distributions size_t in_length = *length;
41*2c2f96dcSApple OSS Distributions size_t out_length = 0;
42*2c2f96dcSApple OSS Distributions
43*2c2f96dcSApple OSS Distributions #if defined(__i386__)
44*2c2f96dcSApple OSS Distributions i386_thread_state_t state = {};
45*2c2f96dcSApple OSS Distributions thread_state_flavor_t flavor = x86_THREAD_STATE32;
46*2c2f96dcSApple OSS Distributions mach_msg_type_number_t count = i386_THREAD_STATE_COUNT;
47*2c2f96dcSApple OSS Distributions #elif defined(__x86_64__)
48*2c2f96dcSApple OSS Distributions x86_thread_state64_t state = {};
49*2c2f96dcSApple OSS Distributions thread_state_flavor_t flavor = x86_THREAD_STATE64;
50*2c2f96dcSApple OSS Distributions mach_msg_type_number_t count = x86_THREAD_STATE64_COUNT;
51*2c2f96dcSApple OSS Distributions #elif defined(__arm__)
52*2c2f96dcSApple OSS Distributions arm_thread_state_t state = {};
53*2c2f96dcSApple OSS Distributions thread_state_flavor_t flavor = ARM_THREAD_STATE;
54*2c2f96dcSApple OSS Distributions mach_msg_type_number_t count = ARM_THREAD_STATE_COUNT;
55*2c2f96dcSApple OSS Distributions #elif defined(__arm64__)
56*2c2f96dcSApple OSS Distributions arm_thread_state64_t state = {};
57*2c2f96dcSApple OSS Distributions thread_state_flavor_t flavor = ARM_THREAD_STATE64;
58*2c2f96dcSApple OSS Distributions mach_msg_type_number_t count = ARM_THREAD_STATE64_COUNT;
59*2c2f96dcSApple OSS Distributions #else
60*2c2f96dcSApple OSS Distributions #error thread_get_register_pointer_values not defined for this architecture
61*2c2f96dcSApple OSS Distributions #endif
62*2c2f96dcSApple OSS Distributions
63*2c2f96dcSApple OSS Distributions kern_return_t ret = thread_get_state(thread, flavor, (thread_state_t)&state, &count);
64*2c2f96dcSApple OSS Distributions if (ret != KERN_SUCCESS) {
65*2c2f96dcSApple OSS Distributions return ret;
66*2c2f96dcSApple OSS Distributions }
67*2c2f96dcSApple OSS Distributions
68*2c2f96dcSApple OSS Distributions // If the provided pointer value is > PAGE_SIZE, add it to the output array
69*2c2f96dcSApple OSS Distributions // if there's available space. (Values between 0 and PAGE_SIZE are the NULL page
70*2c2f96dcSApple OSS Distributions // and not valid pointers.)
71*2c2f96dcSApple OSS Distributions #define push_register_value(p) do { \
72*2c2f96dcSApple OSS Distributions if ((uintptr_t)p > PAGE_SIZE) { \
73*2c2f96dcSApple OSS Distributions if (out_length < in_length && values) \
74*2c2f96dcSApple OSS Distributions values[out_length] = p; \
75*2c2f96dcSApple OSS Distributions out_length++; \
76*2c2f96dcSApple OSS Distributions } } while (0)
77*2c2f96dcSApple OSS Distributions
78*2c2f96dcSApple OSS Distributions #if defined(__i386__)
79*2c2f96dcSApple OSS Distributions if (sp) {
80*2c2f96dcSApple OSS Distributions *sp = state.__esp;
81*2c2f96dcSApple OSS Distributions }
82*2c2f96dcSApple OSS Distributions
83*2c2f96dcSApple OSS Distributions push_register_value(state.__eax);
84*2c2f96dcSApple OSS Distributions push_register_value(state.__ebx);
85*2c2f96dcSApple OSS Distributions push_register_value(state.__ecx);
86*2c2f96dcSApple OSS Distributions push_register_value(state.__edx);
87*2c2f96dcSApple OSS Distributions push_register_value(state.__edi);
88*2c2f96dcSApple OSS Distributions push_register_value(state.__esi);
89*2c2f96dcSApple OSS Distributions push_register_value(state.__ebp);
90*2c2f96dcSApple OSS Distributions #elif defined(__x86_64__)
91*2c2f96dcSApple OSS Distributions if (sp) {
92*2c2f96dcSApple OSS Distributions if (state.__rsp > 128) {
93*2c2f96dcSApple OSS Distributions *sp = state.__rsp - 128 /* redzone */;
94*2c2f96dcSApple OSS Distributions } else {
95*2c2f96dcSApple OSS Distributions *sp = 0;
96*2c2f96dcSApple OSS Distributions }
97*2c2f96dcSApple OSS Distributions }
98*2c2f96dcSApple OSS Distributions
99*2c2f96dcSApple OSS Distributions push_register_value(state.__rax);
100*2c2f96dcSApple OSS Distributions push_register_value(state.__rbx);
101*2c2f96dcSApple OSS Distributions push_register_value(state.__rcx);
102*2c2f96dcSApple OSS Distributions push_register_value(state.__rdx);
103*2c2f96dcSApple OSS Distributions push_register_value(state.__rdi);
104*2c2f96dcSApple OSS Distributions push_register_value(state.__rbp);
105*2c2f96dcSApple OSS Distributions push_register_value(state.__r8);
106*2c2f96dcSApple OSS Distributions push_register_value(state.__r9);
107*2c2f96dcSApple OSS Distributions push_register_value(state.__r10);
108*2c2f96dcSApple OSS Distributions push_register_value(state.__r11);
109*2c2f96dcSApple OSS Distributions push_register_value(state.__r12);
110*2c2f96dcSApple OSS Distributions push_register_value(state.__r13);
111*2c2f96dcSApple OSS Distributions push_register_value(state.__r14);
112*2c2f96dcSApple OSS Distributions push_register_value(state.__r15);
113*2c2f96dcSApple OSS Distributions #elif defined(__arm__)
114*2c2f96dcSApple OSS Distributions if (sp) {
115*2c2f96dcSApple OSS Distributions *sp = state.__sp;
116*2c2f96dcSApple OSS Distributions }
117*2c2f96dcSApple OSS Distributions
118*2c2f96dcSApple OSS Distributions push_register_value(state.__lr);
119*2c2f96dcSApple OSS Distributions
120*2c2f96dcSApple OSS Distributions for (int i = 0; i < 13; i++) {
121*2c2f96dcSApple OSS Distributions push_register_value(state.__r[i]);
122*2c2f96dcSApple OSS Distributions }
123*2c2f96dcSApple OSS Distributions #elif defined(__arm64__)
124*2c2f96dcSApple OSS Distributions if (sp) {
125*2c2f96dcSApple OSS Distributions uintptr_t __sp = arm_thread_state64_get_sp(state);
126*2c2f96dcSApple OSS Distributions if (__sp > 128) {
127*2c2f96dcSApple OSS Distributions *sp = __sp - 128 /* redzone */;
128*2c2f96dcSApple OSS Distributions } else {
129*2c2f96dcSApple OSS Distributions *sp = 0;
130*2c2f96dcSApple OSS Distributions }
131*2c2f96dcSApple OSS Distributions }
132*2c2f96dcSApple OSS Distributions
133*2c2f96dcSApple OSS Distributions push_register_value(arm_thread_state64_get_lr(state));
134*2c2f96dcSApple OSS Distributions
135*2c2f96dcSApple OSS Distributions for (int i = 0; i < 29; i++) {
136*2c2f96dcSApple OSS Distributions push_register_value(state.__x[i]);
137*2c2f96dcSApple OSS Distributions }
138*2c2f96dcSApple OSS Distributions #else
139*2c2f96dcSApple OSS Distributions #error thread_get_register_pointer_values not defined for this architecture
140*2c2f96dcSApple OSS Distributions #endif
141*2c2f96dcSApple OSS Distributions
142*2c2f96dcSApple OSS Distributions *length = out_length;
143*2c2f96dcSApple OSS Distributions
144*2c2f96dcSApple OSS Distributions if (in_length == 0 || out_length > in_length) {
145*2c2f96dcSApple OSS Distributions return KERN_INSUFFICIENT_BUFFER_SIZE;
146*2c2f96dcSApple OSS Distributions }
147*2c2f96dcSApple OSS Distributions
148*2c2f96dcSApple OSS Distributions return KERN_SUCCESS;
149*2c2f96dcSApple OSS Distributions }
150