1/*
2 * Copyright (c) 2007 Apple 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
29/*
30 * CDDL HEADER START
31 *
32 * The contents of this file are subject to the terms of the
33 * Common Development and Distribution License, Version 1.0 only
34 * (the "License").  You may not use this file except in compliance
35 * with the License.
36 *
37 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
38 * or http://www.opensolaris.org/os/licensing.
39 * See the License for the specific language governing permissions
40 * and limitations under the License.
41 *
42 * When distributing Covered Code, include this CDDL HEADER in each
43 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
44 * If applicable, add the following below this CDDL HEADER, with the
45 * fields enclosed by brackets "[]" replaced with your own identifying
46 * information: Portions Copyright [yyyy] [name of copyright owner]
47 *
48 * CDDL HEADER END
49 */
50/*
51 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
52 * Use is subject to license terms.
53 */
54
55/*
56 * #pragma ident	"@(#)dtrace_subr.c	1.12	05/06/08 SMI"
57 */
58
59#define MACH__POSIX_C_SOURCE_PRIVATE 1 /* pulls in suitable savearea from mach/ppc/thread_status.h */
60#include <sys/dtrace.h>
61#include <sys/dtrace_glue.h>
62#include <sys/dtrace_impl.h>
63#include <sys/fasttrap.h>
64#include <sys/vm.h>
65#include <sys/user.h>
66#include <sys/kauth.h>
67#include <kern/debug.h>
68
69int (*dtrace_pid_probe_ptr)(ppc_saved_state_t *);
70int (*dtrace_return_probe_ptr)(ppc_saved_state_t *);
71kern_return_t dtrace_user_probe(ppc_saved_state_t *sv);
72
73kern_return_t
74dtrace_user_probe(ppc_saved_state_t *sv)
75{
76
77	lck_rw_t *rwp;
78	struct proc *p = current_proc();
79
80	uthread_t uthread = (uthread_t)get_bsdthread_info(current_thread());
81	/*
82	 * DTrace accesses t_cred in probe context.	 t_cred
83	 * must always be either NULL, or point to a valid,
84	 * allocated cred structure.
85	 */
86	kauth_cred_uthread_update(uthread, p);
87
88	if (sv->save_exception == T_DTRACE_RET) {
89
90/*
91 *		T_DTRACE_RET is generated by the kernel when an emulation sequence
92 *		ends.  Unlike the x86 implementation, this can not be caused by
93 *		a user state trap instruction.  It is a system error if it occurs
94 *		when not stepping and is, therefore, a panickable offence.
95 */
96
97		if(uthread->t_dtrace_step == 0) {	/* Are we supposed to be tracing? */
98			panic("dtrace_user_probe: T_DTRACE_RET when not stepping\n");
99		}
100
101		if (uthread->t_dtrace_ast) {
102			printf("dtrace_user_probe() should be calling aston()\n");
103			// aston(uthread);
104			// uthread->t_sig_check = 1;
105		}
106
107		/*
108		 * Clear all user tracing flags.
109		 */
110		uthread->t_dtrace_ft = 0;
111
112		/*
113		 * We need to wait until after we've called the
114		 * dtrace_return_probe_ptr function pointer to step the pc.
115		 */
116		rwp = &CPU->cpu_ft_lock;
117		lck_rw_lock_shared(rwp);
118
119		if (dtrace_return_probe_ptr != NULL) (void)(*dtrace_return_probe_ptr)(sv);
120		lck_rw_unlock_shared(rwp);
121
122		sv->save_srr0 = sv->save_srr0 + 4;	/* Step to next instruction */
123		if(!(sv->save_srr1 & 0x8000000000000000ULL)) sv->save_srr0 &= 0x00000000FFFFFFFF;	/* Trim if in 32-bit mode */
124
125		return KERN_SUCCESS;
126
127	} else {
128
129/*
130 *	We have taken our normal trap to get here.  Make sure we expect it
131 */
132		uint32_t instr;
133		rwp = &CPU->cpu_ft_lock;
134
135		/*
136		 * The DTrace fasttrap provider uses a trap, "twi 31,r31,0xDDDD".
137		 * We will only be here if dtrace (or someone pretending to be us)
138		 * sets the trap.
139		 * We let DTrace take the first crack at handling
140		 * this trap; if it's not a probe that DTrace knowns about,
141		 * we call into the trap() routine to handle it like a
142		 * breakpoint placed by a conventional debugger.
143		 */
144
145		/*
146		 * APPLE NOTE: I believe the purpose of the reader/writers lock
147		 * is thus: There are times which dtrace needs to prevent calling
148		 * dtrace_pid_probe_ptr(). Sun's original impl grabbed a plain
149		 * mutex here. However, that serialized all probe calls, and
150		 * destroyed MP behavior. So now they use a RW lock, with probes
151		 * as readers, and the top level synchronization as a writer.
152		 */
153		lck_rw_lock_shared(rwp);
154		if (dtrace_pid_probe_ptr != NULL &&
155			(*dtrace_pid_probe_ptr)(sv) == 0) {
156			lck_rw_unlock_shared(rwp);
157			return KERN_SUCCESS;
158		}
159		lck_rw_unlock_shared(rwp);
160
161		/*
162		 * If the instruction that caused the breakpoint trap doesn't
163		 * look like our trap anymore, it may be that this tracepoint
164		 * was removed just after the user thread executed it. In
165		 * that case, return to user land to retry the instuction.
166		 *
167		 * Note that the PC is correct because we do not advance it until after emulation.
168		 */
169		if (fuword32(sv->save_srr0, &instr) == 0 && instr != FASTTRAP_INSTR) {
170			return KERN_SUCCESS;
171		}
172
173	}
174
175/*
176 *	If we get here, we go back to throw an exception
177 */
178
179	return KERN_FAILURE;
180}
181
182void
183dtrace_safe_synchronous_signal(void)
184{
185// This is commented out of the x86 code and is never called.
186}
187
188int
189dtrace_safe_defer_signal(void)
190{
191// This is commented out of the x86 code and is never called.
192	return 0;
193}
194