12acda721SJordan Gordeev /*
22acda721SJordan Gordeev * Copyright (c) 2005 The DragonFly Project. All rights reserved.
32acda721SJordan Gordeev *
42acda721SJordan Gordeev * This code is derived from software contributed to The DragonFly Project
52acda721SJordan Gordeev * by Matthew Dillon <dillon@backplane.com>
62acda721SJordan Gordeev *
72acda721SJordan Gordeev * Redistribution and use in source and binary forms, with or without
82acda721SJordan Gordeev * modification, are permitted provided that the following conditions
92acda721SJordan Gordeev * are met:
102acda721SJordan Gordeev *
112acda721SJordan Gordeev * 1. Redistributions of source code must retain the above copyright
122acda721SJordan Gordeev * notice, this list of conditions and the following disclaimer.
132acda721SJordan Gordeev * 2. Redistributions in binary form must reproduce the above copyright
142acda721SJordan Gordeev * notice, this list of conditions and the following disclaimer in
152acda721SJordan Gordeev * the documentation and/or other materials provided with the
162acda721SJordan Gordeev * distribution.
172acda721SJordan Gordeev * 3. Neither the name of The DragonFly Project nor the names of its
182acda721SJordan Gordeev * contributors may be used to endorse or promote products derived
192acda721SJordan Gordeev * from this software without specific, prior written permission.
202acda721SJordan Gordeev *
212acda721SJordan Gordeev * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
222acda721SJordan Gordeev * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
232acda721SJordan Gordeev * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
242acda721SJordan Gordeev * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
252acda721SJordan Gordeev * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
262acda721SJordan Gordeev * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
272acda721SJordan Gordeev * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
282acda721SJordan Gordeev * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
292acda721SJordan Gordeev * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
302acda721SJordan Gordeev * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
312acda721SJordan Gordeev * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322acda721SJordan Gordeev * SUCH DAMAGE.
332acda721SJordan Gordeev */
342acda721SJordan Gordeev /*
352acda721SJordan Gordeev * Kernel tracepoint facility.
362acda721SJordan Gordeev */
372acda721SJordan Gordeev
382acda721SJordan Gordeev #include "opt_ktr.h"
392acda721SJordan Gordeev
402acda721SJordan Gordeev #include <sys/param.h>
412acda721SJordan Gordeev #include <sys/cons.h>
422acda721SJordan Gordeev #include <sys/kernel.h>
432acda721SJordan Gordeev #include <sys/libkern.h>
442acda721SJordan Gordeev #include <sys/proc.h>
452acda721SJordan Gordeev #include <sys/sysctl.h>
462acda721SJordan Gordeev #include <sys/ktr.h>
472acda721SJordan Gordeev
482acda721SJordan Gordeev /*
492acda721SJordan Gordeev * This routine fills in the ktr_caller1 and ktr_caller2 fields by
502acda721SJordan Gordeev * tracing back through the kernel stack to locate the stack frames
512acda721SJordan Gordeev * and return addresses.
522acda721SJordan Gordeev *
532acda721SJordan Gordeev *
542acda721SJordan Gordeev * [first argument]
552acda721SJordan Gordeev * [retpc]
562acda721SJordan Gordeev * [frameptr] -> points to caller's frame pointer
572acda721SJordan Gordeev * sp ->[junk]
582acda721SJordan Gordeev */
592acda721SJordan Gordeev
602acda721SJordan Gordeev static __inline
612acda721SJordan Gordeev void **
FRAMEUP(void ** frameptr)622acda721SJordan Gordeev FRAMEUP(void **frameptr)
632acda721SJordan Gordeev {
642acda721SJordan Gordeev void **newframeptr;
652acda721SJordan Gordeev
662acda721SJordan Gordeev newframeptr = (void **)frameptr[0];
67*50e28c02SSepherosa Ziehau if (((uintptr_t)newframeptr ^ (uintptr_t)frameptr) & ~16383ULL)
682acda721SJordan Gordeev newframeptr = frameptr;
692acda721SJordan Gordeev return(newframeptr);
702acda721SJordan Gordeev }
712acda721SJordan Gordeev
72*50e28c02SSepherosa Ziehau struct x86_64_frame {
73*50e28c02SSepherosa Ziehau struct x86_64_frame *f_frame;
74*50e28c02SSepherosa Ziehau long f_retaddr;
75*50e28c02SSepherosa Ziehau long f_arg0;
76*50e28c02SSepherosa Ziehau };
77*50e28c02SSepherosa Ziehau
782acda721SJordan Gordeev void
cpu_ktr_caller(struct ktr_entry * _ktr)792acda721SJordan Gordeev cpu_ktr_caller(struct ktr_entry *_ktr)
802acda721SJordan Gordeev {
81*50e28c02SSepherosa Ziehau struct x86_64_frame *frame;
822acda721SJordan Gordeev struct ktr_entry *ktr;
832acda721SJordan Gordeev void **frameptr;
84*50e28c02SSepherosa Ziehau register_t rbp;
852acda721SJordan Gordeev
86*50e28c02SSepherosa Ziehau __asm __volatile("movq %%rbp, %0" : "=r" (rbp));
87*50e28c02SSepherosa Ziehau frame = (struct x86_64_frame *)rbp;
88*50e28c02SSepherosa Ziehau frameptr = (void **)frame->f_frame;
892acda721SJordan Gordeev ktr = _ktr;
902acda721SJordan Gordeev frameptr = FRAMEUP(frameptr); /* frame, retpc to traced function */
912acda721SJordan Gordeev frameptr = FRAMEUP(frameptr); /* frame, caller1 of traced function */
922acda721SJordan Gordeev ktr->ktr_caller1 = frameptr[1];
932acda721SJordan Gordeev frameptr = FRAMEUP(frameptr); /* frame, caller2 of caller1 */
942acda721SJordan Gordeev ktr->ktr_caller2 = frameptr[1];
952acda721SJordan Gordeev }
96