xref: /illumos-gate/usr/src/uts/common/sys/kobj.h (revision 23690bf77a6b57ae5a2927ea6d4fdab770d0af97)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright 2017 RackTop Systems.
26  * Copyright (c) 2017 Joyent, Inc.
27  * Copyright 2025 MNX Cloud, Inc.
28  * Copyright 2025 Oxide Computer Company
29  */
30 
31 #ifndef _SYS_KOBJ_H
32 #define	_SYS_KOBJ_H
33 
34 #include <sys/modctl.h>
35 #include <sys/elf.h>
36 #include <sys/machelf.h>
37 #include <sys/vmem.h>
38 #include <sys/sdt.h>
39 #include <sys/bootstat.h>
40 #include <sys/types.h>
41 
42 #ifdef	__cplusplus
43 extern "C" {
44 #endif
45 
46 /*
47  * List of modules maintained by kobj.c
48  */
49 struct module_list {
50 	struct module_list *next;
51 	struct module *mp;
52 };
53 
54 typedef struct hotinline_desc {
55 	char	*hid_symname;		/* symbol name */
56 	uintptr_t hid_instr_offset;	/* offset of call in text */
57 	struct hotinline_desc *hid_next;	/* next hotinline */
58 } hotinline_desc_t;
59 
60 typedef uint32_t	symid_t;		/* symbol table index */
61 typedef unsigned char	*reloc_dest_t;
62 
63 typedef	void	module_mach;
64 
65 struct module {
66 	int total_allocated;
67 
68 	Ehdr hdr;
69 	Elf64_Word shnum;
70 	Elf64_Word phnum;
71 	Elf64_Word shstrndx;
72 	char *shdrs;
73 	Shdr *symhdr, *strhdr;
74 
75 	char *depends_on;
76 
77 	size_t symsize;
78 	char *symspace;	/* symbols + strings + hashtbl, or NULL */
79 	int flags;
80 
81 	size_t text_size;
82 	size_t data_size;
83 	char *text;
84 	char *data;
85 
86 	unsigned int symtbl_section;
87 	/* pointers into symspace, or NULL */
88 	char *symtbl;
89 	char *strings;
90 
91 	unsigned int hashsize;
92 	symid_t *buckets;
93 	symid_t *chains;
94 
95 	unsigned int nsyms;
96 
97 	unsigned int bss_align;
98 	size_t bss_size;
99 	uintptr_t bss;
100 
101 	char *filename;
102 
103 	struct module_list *head, *tail;
104 	reloc_dest_t destination;
105 	module_mach * machdata;
106 	char *ctfdata;
107 	size_t ctfsize;
108 
109 	char *fbt_tab;
110 	size_t fbt_size;
111 	size_t fbt_nentries;
112 	caddr_t textwin;
113 	caddr_t textwin_base;
114 
115 	hotinline_desc_t *hi_calls;
116 
117 	sdt_probedesc_t *sdt_probes;
118 	size_t sdt_nprobes;
119 	char *sdt_tab;
120 	size_t sdt_size;
121 
122 	char *sigdata;
123 	size_t sigsize;
124 };
125 
126 struct kobj_mem {
127 	struct kobj_mem	*km_next;
128 	struct kobj_mem *km_prev;
129 	uintptr_t	km_addr;
130 	size_t		km_size;
131 	uintptr_t	km_alloc_addr;
132 	size_t		km_alloc_size;
133 };
134 
135 struct _buf {
136 	intptr_t	 _fd;
137 	char		*_ptr;
138 	char		*_base;
139 	char		*_name;
140 	char		*_dbuf;
141 	int		 _size;
142 	int		_cnt;
143 	int		 _off;
144 	int		_ln;
145 	int		_bsize;
146 	int		_iscmp;
147 	int		_dsize;
148 };
149 
150 
151 /*
152  * Statistical info.
153  */
154 typedef struct {
155 	int nalloc;
156 	int nfree;
157 	int nalloc_calls;
158 	int nfree_calls;
159 } kobj_stat_t;
160 
161 #define	kobj_filename(p) ((p)->_name)
162 #define	kobj_linenum(p)  ((p)->_ln)
163 #define	kobj_newline(p)	 ((p)->_ln++)
164 #define	kobj_getc(p)	(--(p)->_cnt >= 0 ? ((int)*(p)->_ptr++):kobj_filbuf(p))
165 #define	kobj_ungetc(p)	 (++(p)->_cnt > (p)->_size ? -1 : ((int)*(--(p)->_ptr)))
166 #define	kobj_comphdr(p)	((struct comphdr *)(p)->_dbuf)
167 
168 /* Offset into buffer */
169 #define	B_OFFSET(file, off)	(off % (file)->_bsize)
170 
171 /* Start of page */
172 #define	F_PAGE(file, off)	(off - B_OFFSET(file, off))
173 
174 #define	F_BLKS(file, size)	((size / (file)->_bsize) * (file)->_bsize)
175 
176 #if defined(_KERNEL) || defined(_FAKE_KERNEL)
177 
178 extern int kobj_load_module(struct modctl *, int);
179 extern void kobj_unload_module(struct modctl *);
180 extern uintptr_t kobj_lookup(struct module *, const char *);
181 extern Sym *kobj_lookup_all(struct module *, char *, int);
182 extern int kobj_addrcheck(void *, caddr_t);
183 extern int kobj_module_to_id(void *);
184 extern void kobj_getmodinfo(void *, struct modinfo *);
185 extern int kobj_get_needed(void *, short *, int);
186 extern uintptr_t kobj_getsymvalue(char *, int);
187 extern char *kobj_getsymname(uintptr_t, ulong_t *);
188 extern char *kobj_searchsym(struct module *, uintptr_t, ulong_t *);
189 
190 extern int kobj_fstat(intptr_t, struct bootstat *);
191 extern intptr_t kobj_open(char *);
192 extern int kobj_path_exists(char *, int);
193 extern struct _buf *kobj_open_path(char *, int, int);
194 extern int kobj_read(intptr_t, char *, unsigned int, unsigned int);
195 extern void kobj_close(intptr_t);
196 extern void *kobj_alloc(size_t, int);
197 extern void *kobj_zalloc(size_t, int);
198 extern void kobj_free(void *, size_t);
199 extern struct _buf *kobj_open_file(char *);
200 extern void kobj_close_file(struct _buf *);
201 extern int kobj_read_file(struct _buf *, char *, unsigned, unsigned);
202 extern int kobj_get_filesize(struct _buf *, uint64_t *size);
203 extern uintptr_t kobj_getelfsym(char *, void *, int *);
204 extern void kobj_set_ctf(struct module *, caddr_t data, size_t size);
205 extern void do_hotinlines(struct module *);
206 
207 extern int kobj_filbuf(struct _buf *);
208 extern void kobj_sync(void);
209 #if defined(__i386) || defined(__sparc) || defined(__amd64)
210 extern void kobj_vmem_init(vmem_t **, vmem_t **);
211 #else
212 #error "ISA not supported"
213 #endif
214 extern caddr_t kobj_text_alloc(vmem_t *, size_t);
215 extern caddr_t kobj_texthole_alloc(caddr_t, size_t);
216 extern void kobj_texthole_free(caddr_t, size_t);
217 extern void kobj_stat_get(kobj_stat_t *);
218 extern void kobj_textwin_alloc(struct module *);
219 extern void kobj_textwin_free(struct module *);
220 extern void kobj_printf(char *, ...) __KPRINTFLIKE(1);
221 
222 #endif	/* defined(_KERNEL) || defined(_FAKE_KERNEL) */
223 
224 #ifdef	__cplusplus
225 }
226 #endif
227 
228 #endif /* !_SYS_KOBJ_H */
229