]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/dev/dtrace/amd64/dtrace_subr.c
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / sys / cddl / dev / dtrace / amd64 / dtrace_subr.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  *
22  * $FreeBSD$
23  *
24  */
25 /*
26  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29
30 /*
31  * Copyright (c) 2011, Joyent, Inc. All rights reserved.
32  */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/kmem.h>
40 #include <sys/smp.h>
41 #include <sys/dtrace_impl.h>
42 #include <sys/dtrace_bsd.h>
43 #include <machine/clock.h>
44 #include <machine/cpufunc.h>
45 #include <machine/frame.h>
46 #include <machine/md_var.h>
47 #include <machine/psl.h>
48 #include <machine/trap.h>
49 #include <vm/pmap.h>
50
51 extern void dtrace_getnanotime(struct timespec *tsp);
52 extern int (*dtrace_invop_jump_addr)(struct trapframe *);
53
54 int     dtrace_invop(uintptr_t, struct trapframe *, uintptr_t);
55 int     dtrace_invop_start(struct trapframe *frame);
56 void    dtrace_invop_init(void);
57 void    dtrace_invop_uninit(void);
58
59 typedef struct dtrace_invop_hdlr {
60         int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t);
61         struct dtrace_invop_hdlr *dtih_next;
62 } dtrace_invop_hdlr_t;
63
64 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
65
66 int
67 dtrace_invop(uintptr_t addr, struct trapframe *frame, uintptr_t eax)
68 {
69         dtrace_invop_hdlr_t *hdlr;
70         int rval;
71
72         for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
73                 if ((rval = hdlr->dtih_func(addr, frame, eax)) != 0)
74                         return (rval);
75
76         return (0);
77 }
78
79 void
80 dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
81 {
82         dtrace_invop_hdlr_t *hdlr;
83
84         hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
85         hdlr->dtih_func = func;
86         hdlr->dtih_next = dtrace_invop_hdlr;
87         dtrace_invop_hdlr = hdlr;
88 }
89
90 void
91 dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
92 {
93         dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL;
94
95         for (;;) {
96                 if (hdlr == NULL)
97                         panic("attempt to remove non-existent invop handler");
98
99                 if (hdlr->dtih_func == func)
100                         break;
101
102                 prev = hdlr;
103                 hdlr = hdlr->dtih_next;
104         }
105
106         if (prev == NULL) {
107                 ASSERT(dtrace_invop_hdlr == hdlr);
108                 dtrace_invop_hdlr = hdlr->dtih_next;
109         } else {
110                 ASSERT(dtrace_invop_hdlr != hdlr);
111                 prev->dtih_next = hdlr->dtih_next;
112         }
113
114         kmem_free(hdlr, 0);
115 }
116
117 void
118 dtrace_invop_init(void)
119 {
120
121         dtrace_invop_jump_addr = dtrace_invop_start;
122 }
123
124 void
125 dtrace_invop_uninit(void)
126 {
127
128         dtrace_invop_jump_addr = NULL;
129 }
130
131 /*ARGSUSED*/
132 void
133 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
134 {
135         (*func)(0, la57 ? (uintptr_t)addr_P5Tmap : (uintptr_t)addr_P4Tmap);
136 }
137
138 void
139 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
140 {
141         cpuset_t cpus;
142
143         if (cpu == DTRACE_CPUALL)
144                 cpus = all_cpus;
145         else
146                 CPU_SETOF(cpu, &cpus);
147
148         smp_rendezvous_cpus(cpus, smp_no_rendezvous_barrier, func,
149             smp_no_rendezvous_barrier, arg);
150 }
151
152 static void
153 dtrace_sync_func(void)
154 {
155 }
156
157 void
158 dtrace_sync(void)
159 {
160         dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
161 }
162
163 #ifdef notyet
164 void
165 dtrace_safe_synchronous_signal(void)
166 {
167         kthread_t *t = curthread;
168         struct regs *rp = lwptoregs(ttolwp(t));
169         size_t isz = t->t_dtrace_npc - t->t_dtrace_pc;
170
171         ASSERT(t->t_dtrace_on);
172
173         /*
174          * If we're not in the range of scratch addresses, we're not actually
175          * tracing user instructions so turn off the flags. If the instruction
176          * we copied out caused a synchonous trap, reset the pc back to its
177          * original value and turn off the flags.
178          */
179         if (rp->r_pc < t->t_dtrace_scrpc ||
180             rp->r_pc > t->t_dtrace_astpc + isz) {
181                 t->t_dtrace_ft = 0;
182         } else if (rp->r_pc == t->t_dtrace_scrpc ||
183             rp->r_pc == t->t_dtrace_astpc) {
184                 rp->r_pc = t->t_dtrace_pc;
185                 t->t_dtrace_ft = 0;
186         }
187 }
188
189 int
190 dtrace_safe_defer_signal(void)
191 {
192         kthread_t *t = curthread;
193         struct regs *rp = lwptoregs(ttolwp(t));
194         size_t isz = t->t_dtrace_npc - t->t_dtrace_pc;
195
196         ASSERT(t->t_dtrace_on);
197
198         /*
199          * If we're not in the range of scratch addresses, we're not actually
200          * tracing user instructions so turn off the flags.
201          */
202         if (rp->r_pc < t->t_dtrace_scrpc ||
203             rp->r_pc > t->t_dtrace_astpc + isz) {
204                 t->t_dtrace_ft = 0;
205                 return (0);
206         }
207
208         /*
209          * If we have executed the original instruction, but we have performed
210          * neither the jmp back to t->t_dtrace_npc nor the clean up of any
211          * registers used to emulate %rip-relative instructions in 64-bit mode,
212          * we'll save ourselves some effort by doing that here and taking the
213          * signal right away.  We detect this condition by seeing if the program
214          * counter is the range [scrpc + isz, astpc).
215          */
216         if (rp->r_pc >= t->t_dtrace_scrpc + isz &&
217             rp->r_pc < t->t_dtrace_astpc) {
218 #ifdef __amd64
219                 /*
220                  * If there is a scratch register and we're on the
221                  * instruction immediately after the modified instruction,
222                  * restore the value of that scratch register.
223                  */
224                 if (t->t_dtrace_reg != 0 &&
225                     rp->r_pc == t->t_dtrace_scrpc + isz) {
226                         switch (t->t_dtrace_reg) {
227                         case REG_RAX:
228                                 rp->r_rax = t->t_dtrace_regv;
229                                 break;
230                         case REG_RCX:
231                                 rp->r_rcx = t->t_dtrace_regv;
232                                 break;
233                         case REG_R8:
234                                 rp->r_r8 = t->t_dtrace_regv;
235                                 break;
236                         case REG_R9:
237                                 rp->r_r9 = t->t_dtrace_regv;
238                                 break;
239                         }
240                 }
241 #endif
242                 rp->r_pc = t->t_dtrace_npc;
243                 t->t_dtrace_ft = 0;
244                 return (0);
245         }
246
247         /*
248          * Otherwise, make sure we'll return to the kernel after executing
249          * the copied out instruction and defer the signal.
250          */
251         if (!t->t_dtrace_step) {
252                 ASSERT(rp->r_pc < t->t_dtrace_astpc);
253                 rp->r_pc += t->t_dtrace_astpc - t->t_dtrace_scrpc;
254                 t->t_dtrace_step = 1;
255         }
256
257         t->t_dtrace_ast = 1;
258
259         return (1);
260 }
261 #endif
262
263 static int64_t  tgt_cpu_tsc;
264 static int64_t  hst_cpu_tsc;
265 static int64_t  tsc_skew[MAXCPU];
266 static uint64_t nsec_scale;
267
268 /* See below for the explanation of this macro. */
269 #define SCALE_SHIFT     28
270
271 static void
272 dtrace_gethrtime_init_cpu(void *arg)
273 {
274         uintptr_t cpu = (uintptr_t) arg;
275
276         if (cpu == curcpu)
277                 tgt_cpu_tsc = rdtsc();
278         else
279                 hst_cpu_tsc = rdtsc();
280 }
281
282 #ifdef EARLY_AP_STARTUP
283 static void
284 dtrace_gethrtime_init(void *arg)
285 {
286         struct pcpu *pc;
287         uint64_t tsc_f;
288         cpuset_t map;
289         int i;
290 #else
291 /*
292  * Get the frequency and scale factor as early as possible so that they can be
293  * used for boot-time tracing.
294  */
295 static void
296 dtrace_gethrtime_init_early(void *arg)
297 {
298         uint64_t tsc_f;
299 #endif
300
301         /*
302          * Get TSC frequency known at this moment.
303          * This should be constant if TSC is invariant.
304          * Otherwise tick->time conversion will be inaccurate, but
305          * will preserve monotonic property of TSC.
306          */
307         tsc_f = atomic_load_acq_64(&tsc_freq);
308
309         /*
310          * The following line checks that nsec_scale calculated below
311          * doesn't overflow 32-bit unsigned integer, so that it can multiply
312          * another 32-bit integer without overflowing 64-bit.
313          * Thus minimum supported TSC frequency is 62.5MHz.
314          */
315         KASSERT(tsc_f > (NANOSEC >> (32 - SCALE_SHIFT)),
316             ("TSC frequency is too low"));
317
318         /*
319          * We scale up NANOSEC/tsc_f ratio to preserve as much precision
320          * as possible.
321          * 2^28 factor was chosen quite arbitrarily from practical
322          * considerations:
323          * - it supports TSC frequencies as low as 62.5MHz (see above);
324          * - it provides quite good precision (e < 0.01%) up to THz
325          *   (terahertz) values;
326          */
327         nsec_scale = ((uint64_t)NANOSEC << SCALE_SHIFT) / tsc_f;
328 #ifndef EARLY_AP_STARTUP
329 }
330 SYSINIT(dtrace_gethrtime_init_early, SI_SUB_CPU, SI_ORDER_ANY,
331     dtrace_gethrtime_init_early, NULL);
332
333 static void
334 dtrace_gethrtime_init(void *arg)
335 {
336         struct pcpu *pc;
337         cpuset_t map;
338         int i;
339 #endif
340
341         if (vm_guest != VM_GUEST_NO)
342                 return;
343
344         /* The current CPU is the reference one. */
345         sched_pin();
346         tsc_skew[curcpu] = 0;
347         CPU_FOREACH(i) {
348                 if (i == curcpu)
349                         continue;
350
351                 pc = pcpu_find(i);
352                 CPU_SETOF(PCPU_GET(cpuid), &map);
353                 CPU_SET(pc->pc_cpuid, &map);
354
355                 smp_rendezvous_cpus(map, NULL,
356                     dtrace_gethrtime_init_cpu,
357                     smp_no_rendezvous_barrier, (void *)(uintptr_t) i);
358
359                 tsc_skew[i] = tgt_cpu_tsc - hst_cpu_tsc;
360         }
361         sched_unpin();
362 }
363 #ifdef EARLY_AP_STARTUP
364 SYSINIT(dtrace_gethrtime_init, SI_SUB_DTRACE, SI_ORDER_ANY,
365     dtrace_gethrtime_init, NULL);
366 #else
367 SYSINIT(dtrace_gethrtime_init, SI_SUB_SMP, SI_ORDER_ANY, dtrace_gethrtime_init,
368     NULL);
369 #endif
370
371 /*
372  * DTrace needs a high resolution time function which can
373  * be called from a probe context and guaranteed not to have
374  * instrumented with probes itself.
375  *
376  * Returns nanoseconds since boot.
377  */
378 uint64_t
379 dtrace_gethrtime(void)
380 {
381         uint64_t tsc;
382         uint32_t lo, hi;
383         register_t rflags;
384
385         /*
386          * We split TSC value into lower and higher 32-bit halves and separately
387          * scale them with nsec_scale, then we scale them down by 2^28
388          * (see nsec_scale calculations) taking into account 32-bit shift of
389          * the higher half and finally add.
390          */
391         rflags = intr_disable();
392         tsc = rdtsc() - tsc_skew[curcpu];
393         intr_restore(rflags);
394
395         lo = tsc;
396         hi = tsc >> 32;
397         return (((lo * nsec_scale) >> SCALE_SHIFT) +
398             ((hi * nsec_scale) << (32 - SCALE_SHIFT)));
399 }
400
401 uint64_t
402 dtrace_gethrestime(void)
403 {
404         struct timespec current_time;
405
406         dtrace_getnanotime(&current_time);
407
408         return (current_time.tv_sec * 1000000000ULL + current_time.tv_nsec);
409 }
410
411 /* Function to handle DTrace traps during probes. See amd64/amd64/trap.c. */
412 int
413 dtrace_trap(struct trapframe *frame, u_int type)
414 {
415         uint16_t nofault;
416
417         /*
418          * A trap can occur while DTrace executes a probe. Before
419          * executing the probe, DTrace blocks re-scheduling and sets
420          * a flag in its per-cpu flags to indicate that it doesn't
421          * want to fault. On returning from the probe, the no-fault
422          * flag is cleared and finally re-scheduling is enabled.
423          *
424          * Check if DTrace has enabled 'no-fault' mode:
425          */
426         sched_pin();
427         nofault = cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT;
428         sched_unpin();
429         if (nofault) {
430                 KASSERT((read_rflags() & PSL_I) == 0, ("interrupts enabled"));
431
432                 /*
433                  * There are only a couple of trap types that are expected.
434                  * All the rest will be handled in the usual way.
435                  */
436                 switch (type) {
437                 /* General protection fault. */
438                 case T_PROTFLT:
439                         /* Flag an illegal operation. */
440                         cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
441
442                         /*
443                          * Offset the instruction pointer to the instruction
444                          * following the one causing the fault.
445                          */
446                         frame->tf_rip += dtrace_instr_size((u_char *) frame->tf_rip);
447                         return (1);
448                 /* Page fault. */
449                 case T_PAGEFLT:
450                         /* Flag a bad address. */
451                         cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
452                         cpu_core[curcpu].cpuc_dtrace_illval = frame->tf_addr;
453
454                         /*
455                          * Offset the instruction pointer to the instruction
456                          * following the one causing the fault.
457                          */
458                         frame->tf_rip += dtrace_instr_size((u_char *) frame->tf_rip);
459                         return (1);
460                 default:
461                         /* Handle all other traps in the usual way. */
462                         break;
463                 }
464         }
465
466         /* Handle the trap in the usual way. */
467         return (0);
468 }