xref: /illumos-gate/usr/src/uts/common/sys/kobj.h (revision 425251fd)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22b1b8ab34Slling  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24f06dce2cSAndrew Stormont  *
25f06dce2cSAndrew Stormont  * Copyright 2017 RackTop Systems.
267c478bd9Sstevel@tonic-gate  */
27*425251fdSSam Gwydir /*
28*425251fdSSam Gwydir  * Copyright (c) 2017 Joyent, Inc.
29*425251fdSSam Gwydir  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #ifndef _SYS_KOBJ_H
327c478bd9Sstevel@tonic-gate #define	_SYS_KOBJ_H
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
357c478bd9Sstevel@tonic-gate #include <sys/elf.h>
367c478bd9Sstevel@tonic-gate #include <sys/machelf.h>
377c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
39ea8dc4b6Seschrock #include <sys/bootstat.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
427c478bd9Sstevel@tonic-gate extern "C" {
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * List of modules maintained by kobj.c
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate struct module_list {
497c478bd9Sstevel@tonic-gate 	struct module_list *next;
507c478bd9Sstevel@tonic-gate 	struct module *mp;
517c478bd9Sstevel@tonic-gate };
527c478bd9Sstevel@tonic-gate 
53*425251fdSSam Gwydir typedef struct hotinline_desc {
54*425251fdSSam Gwydir 	char	*hid_symname;		/* symbol name */
55*425251fdSSam Gwydir 	uintptr_t hid_instr_offset;	/* offset of call in text */
56*425251fdSSam Gwydir 	struct hotinline_desc *hid_next;	/* next hotinline */
57*425251fdSSam Gwydir } hotinline_desc_t;
58*425251fdSSam Gwydir 
597c478bd9Sstevel@tonic-gate typedef unsigned short	symid_t;		/* symbol table index */
607c478bd9Sstevel@tonic-gate typedef unsigned char	*reloc_dest_t;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate typedef	void	module_mach;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate struct module {
657c478bd9Sstevel@tonic-gate 	int total_allocated;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	Ehdr hdr;
687c478bd9Sstevel@tonic-gate 	char *shdrs;
697c478bd9Sstevel@tonic-gate 	Shdr *symhdr, *strhdr;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	char *depends_on;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	size_t symsize;
747c478bd9Sstevel@tonic-gate 	char *symspace;	/* symbols + strings + hashtbl, or NULL */
757c478bd9Sstevel@tonic-gate 	int flags;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	size_t text_size;
787c478bd9Sstevel@tonic-gate 	size_t data_size;
797c478bd9Sstevel@tonic-gate 	char *text;
807c478bd9Sstevel@tonic-gate 	char *data;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	unsigned int symtbl_section;
837c478bd9Sstevel@tonic-gate 	/* pointers into symspace, or NULL */
847c478bd9Sstevel@tonic-gate 	char *symtbl;
857c478bd9Sstevel@tonic-gate 	char *strings;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	unsigned int hashsize;
887c478bd9Sstevel@tonic-gate 	symid_t *buckets;
897c478bd9Sstevel@tonic-gate 	symid_t *chains;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	unsigned int nsyms;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	unsigned int bss_align;
947c478bd9Sstevel@tonic-gate 	size_t bss_size;
957c478bd9Sstevel@tonic-gate 	uintptr_t bss;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	char *filename;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	struct module_list *head, *tail;
1007c478bd9Sstevel@tonic-gate 	reloc_dest_t destination;
1017c478bd9Sstevel@tonic-gate 	module_mach * machdata;
1027c478bd9Sstevel@tonic-gate 	char *ctfdata;
1037c478bd9Sstevel@tonic-gate 	size_t ctfsize;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	char *fbt_tab;
1067c478bd9Sstevel@tonic-gate 	size_t fbt_size;
1077c478bd9Sstevel@tonic-gate 	size_t fbt_nentries;
1087c478bd9Sstevel@tonic-gate 	caddr_t textwin;
1097c478bd9Sstevel@tonic-gate 	caddr_t textwin_base;
1107c478bd9Sstevel@tonic-gate 
111*425251fdSSam Gwydir 	hotinline_desc_t *hi_calls;
112*425251fdSSam Gwydir 
1137c478bd9Sstevel@tonic-gate 	sdt_probedesc_t *sdt_probes;
1147c478bd9Sstevel@tonic-gate 	size_t sdt_nprobes;
1157c478bd9Sstevel@tonic-gate 	char *sdt_tab;
1167c478bd9Sstevel@tonic-gate 	size_t sdt_size;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	char *sigdata;
1197c478bd9Sstevel@tonic-gate 	size_t sigsize;
1207c478bd9Sstevel@tonic-gate };
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate struct kobj_mem {
1237c478bd9Sstevel@tonic-gate 	struct kobj_mem	*km_next;
1247c478bd9Sstevel@tonic-gate 	struct kobj_mem *km_prev;
1257c478bd9Sstevel@tonic-gate 	uintptr_t	km_addr;
1267c478bd9Sstevel@tonic-gate 	size_t		km_size;
1277c478bd9Sstevel@tonic-gate 	uintptr_t	km_alloc_addr;
1287c478bd9Sstevel@tonic-gate 	size_t		km_alloc_size;
1297c478bd9Sstevel@tonic-gate };
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate struct _buf {
1327c478bd9Sstevel@tonic-gate 	intptr_t	 _fd;
1337c478bd9Sstevel@tonic-gate 	char		*_ptr;
1347c478bd9Sstevel@tonic-gate 	char		*_base;
1357c478bd9Sstevel@tonic-gate 	char 		*_name;
136986fd29aSsetje 	char		*_dbuf;
1377c478bd9Sstevel@tonic-gate 	int		 _size;
1387c478bd9Sstevel@tonic-gate 	int		_cnt;
1397c478bd9Sstevel@tonic-gate 	int		 _off;
1407c478bd9Sstevel@tonic-gate 	int		_ln;
141986fd29aSsetje 	int		_bsize;
142986fd29aSsetje 	int		_iscmp;
143986fd29aSsetje 	int		_dsize;
1447c478bd9Sstevel@tonic-gate };
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * Statistical info.
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate typedef struct {
1517c478bd9Sstevel@tonic-gate 	int nalloc;
1527c478bd9Sstevel@tonic-gate 	int nfree;
1537c478bd9Sstevel@tonic-gate 	int nalloc_calls;
1547c478bd9Sstevel@tonic-gate 	int nfree_calls;
1557c478bd9Sstevel@tonic-gate } kobj_stat_t;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate #define	kobj_filename(p) ((p)->_name)
1587c478bd9Sstevel@tonic-gate #define	kobj_linenum(p)  ((p)->_ln)
1597c478bd9Sstevel@tonic-gate #define	kobj_newline(p)	 ((p)->_ln++)
1607c478bd9Sstevel@tonic-gate #define	kobj_getc(p)	(--(p)->_cnt >= 0 ? ((int)*(p)->_ptr++):kobj_filbuf(p))
1617c478bd9Sstevel@tonic-gate #define	kobj_ungetc(p)	 (++(p)->_cnt > (p)->_size ? -1 : ((int)*(--(p)->_ptr)))
162986fd29aSsetje #define	kobj_comphdr(p)	((struct comphdr *)(p)->_dbuf)
1637c478bd9Sstevel@tonic-gate 
164986fd29aSsetje /* Offset into buffer */
165986fd29aSsetje #define	B_OFFSET(file, off)	(off % (file)->_bsize)
166986fd29aSsetje 
167986fd29aSsetje /* Start of page */
168986fd29aSsetje #define	F_PAGE(file, off)	(off - B_OFFSET(file, off))
169986fd29aSsetje 
170986fd29aSsetje #define	F_BLKS(file, size)	((size / (file)->_bsize) * (file)->_bsize)
1717c478bd9Sstevel@tonic-gate 
172f06dce2cSAndrew Stormont #if defined(_KERNEL) || defined(_FAKE_KERNEL)
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate extern int kobj_load_module(struct modctl *, int);
1757c478bd9Sstevel@tonic-gate extern void kobj_unload_module(struct modctl *);
1767aec1d6eScindi extern uintptr_t kobj_lookup(struct module *, const char *);
1777c478bd9Sstevel@tonic-gate extern Sym *kobj_lookup_all(struct module *, char *, int);
1787c478bd9Sstevel@tonic-gate extern int kobj_addrcheck(void *, caddr_t);
1797c478bd9Sstevel@tonic-gate extern int kobj_module_to_id(void *);
1807c478bd9Sstevel@tonic-gate extern void kobj_getmodinfo(void *, struct modinfo *);
1817c478bd9Sstevel@tonic-gate extern int kobj_get_needed(void *, short *, int);
1827c478bd9Sstevel@tonic-gate extern uintptr_t kobj_getsymvalue(char *, int);
1837c478bd9Sstevel@tonic-gate extern char *kobj_getsymname(uintptr_t, ulong_t *);
1847c478bd9Sstevel@tonic-gate extern char *kobj_searchsym(struct module *, uintptr_t, ulong_t *);
1857c478bd9Sstevel@tonic-gate 
186ea8dc4b6Seschrock extern int kobj_fstat(intptr_t, struct bootstat *);
1877c478bd9Sstevel@tonic-gate extern intptr_t kobj_open(char *);
1885c311300Scth extern int kobj_path_exists(char *, int);
1897c478bd9Sstevel@tonic-gate extern struct _buf *kobj_open_path(char *, int, int);
1907c478bd9Sstevel@tonic-gate extern int kobj_read(intptr_t, char *, unsigned int, unsigned int);
1917c478bd9Sstevel@tonic-gate extern void kobj_close(intptr_t);
1927c478bd9Sstevel@tonic-gate extern void *kobj_alloc(size_t, int);
1937c478bd9Sstevel@tonic-gate extern void *kobj_zalloc(size_t, int);
1947c478bd9Sstevel@tonic-gate extern void kobj_free(void *, size_t);
1957c478bd9Sstevel@tonic-gate extern struct _buf *kobj_open_file(char *);
1967c478bd9Sstevel@tonic-gate extern void kobj_close_file(struct _buf *);
1977c478bd9Sstevel@tonic-gate extern int kobj_read_file(struct _buf *, char *, unsigned, unsigned);
198b1b8ab34Slling extern int kobj_get_filesize(struct _buf *, uint64_t *size);
1997c478bd9Sstevel@tonic-gate extern uintptr_t kobj_getelfsym(char *, void *, int *);
2007c478bd9Sstevel@tonic-gate extern void kobj_set_ctf(struct module *, caddr_t data, size_t size);
201*425251fdSSam Gwydir extern void do_hotinlines(struct module *);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate extern int kobj_filbuf(struct _buf *);
2047c478bd9Sstevel@tonic-gate extern void kobj_sync(void);
2057c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__sparc) || defined(__amd64)
2067c478bd9Sstevel@tonic-gate extern void kobj_vmem_init(vmem_t **, vmem_t **);
2077c478bd9Sstevel@tonic-gate #else
2087c478bd9Sstevel@tonic-gate #error "ISA not supported"
2097c478bd9Sstevel@tonic-gate #endif
2107c478bd9Sstevel@tonic-gate extern caddr_t kobj_text_alloc(vmem_t *, size_t);
2117c478bd9Sstevel@tonic-gate extern caddr_t kobj_texthole_alloc(caddr_t, size_t);
2127c478bd9Sstevel@tonic-gate extern void kobj_texthole_free(caddr_t, size_t);
2137c478bd9Sstevel@tonic-gate extern void kobj_stat_get(kobj_stat_t *);
2147c478bd9Sstevel@tonic-gate extern void kobj_textwin_alloc(struct module *);
2157c478bd9Sstevel@tonic-gate extern void kobj_textwin_free(struct module *);
2167c478bd9Sstevel@tonic-gate 
217f06dce2cSAndrew Stormont #endif	/* defined(_KERNEL) || defined(_FAKE_KERNEL) */
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate #endif
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate #endif /* !_SYS_KOBJ_H */
224