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