xref: /freebsd/sys/cddl/dev/kinst/kinst.h (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1 /*
2  * SPDX-License-Identifier: CDDL 1.0
3  *
4  * Copyright 2022 Christos Margiolis <christos@FreeBSD.org>
5  */
6 
7 #ifndef _KINST_H_
8 #define _KINST_H_
9 
10 #include <sys/dtrace.h>
11 
12 typedef struct {
13 	char	kpd_func[DTRACE_FUNCNAMELEN];
14 	char	kpd_mod[DTRACE_MODNAMELEN];
15 	int	kpd_off;
16 } dtrace_kinst_probedesc_t;
17 
18 #define KINSTIOC_MAKEPROBE	_IOW('k', 1, dtrace_kinst_probedesc_t)
19 
20 #ifdef _KERNEL
21 
22 #include <sys/queue.h>
23 
24 #include "kinst_isa.h"
25 
26 struct kinst_probe {
27 	LIST_ENTRY(kinst_probe)	kp_hashnext;
28 	const char		*kp_func;
29 	char			kp_name[16];
30 	dtrace_id_t		kp_id;
31 	kinst_patchval_t	kp_patchval;
32 	kinst_patchval_t	kp_savedval;
33 	kinst_patchval_t	*kp_patchpoint;
34 
35 	struct kinst_probe_md	kp_md;
36 };
37 
38 LIST_HEAD(kinst_probe_list, kinst_probe);
39 
40 extern struct kinst_probe_list	*kinst_probetab;
41 
42 #define KINST_PROBETAB_MAX	0x8000	/* 32k */
43 #define KINST_ADDR2NDX(addr)	(((uintptr_t)(addr)) & (KINST_PROBETAB_MAX - 1))
44 #define KINST_GETPROBE(i) 	(&kinst_probetab[KINST_ADDR2NDX(i)])
45 
46 struct linker_file;
47 struct linker_symval;
48 
49 volatile void	*kinst_memcpy(volatile void *, volatile const void *, size_t);
50 bool	kinst_excluded(const char *);
51 bool	kinst_md_excluded(const char *);
52 int	kinst_invop(uintptr_t, struct trapframe *, uintptr_t);
53 int	kinst_make_probe(struct linker_file *, int, struct linker_symval *,
54 	    void *);
55 void	kinst_patch_tracepoint(struct kinst_probe *, kinst_patchval_t);
56 void	kinst_probe_create(struct kinst_probe *, struct linker_file *);
57 
58 int	kinst_trampoline_init(void);
59 int	kinst_trampoline_deinit(void);
60 uint8_t	*kinst_trampoline_alloc(int);
61 void	kinst_trampoline_dealloc(uint8_t *);
62 
63 int	kinst_md_init(void);
64 void	kinst_md_deinit(void);
65 
66 #ifdef MALLOC_DECLARE
67 MALLOC_DECLARE(M_KINST);
68 #endif /* MALLOC_DECLARE */
69 
70 #define KINST_LOG_HELPER(fmt, ...)	\
71 	printf("%s:%d: " fmt "%s\n", __func__, __LINE__, __VA_ARGS__)
72 #define KINST_LOG(...)			\
73 	KINST_LOG_HELPER(__VA_ARGS__, "")
74 
75 #endif /* _KERNEL */
76 
77 #endif /* _KINST_H_ */
78