xref: /titanic_52/usr/src/uts/sparc/krtld/kobj_isa.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
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 /*
23  * Copyright 1994-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Miscellaneous ISA-specific code.
31  */
32 #include <sys/types.h>
33 #include <sys/elf.h>
34 #include <sys/kobj.h>
35 #include <sys/kobj_impl.h>
36 #include <sys/archsystm.h>
37 
38 extern	int	use_iflush;
39 
40 /*
41  * Check that an ELF header corresponds to this machine's
42  * instruction set architecture.  Used by kobj_load_module()
43  * to not get confused by a misplaced driver or kernel module
44  * built for a different ISA.
45  */
46 int
47 elf_mach_ok(Ehdr *h)
48 {
49 	/*
50 	 * XX64	This should eventually be folded into a v9-isa specific file
51 	 */
52 	return (h->e_ident[EI_DATA] == ELFDATA2MSB &&
53 	    (h->e_machine == EM_SPARCV9));
54 }
55 
56 
57 /*
58  * return non-zero for a bad-address
59  */
60 int
61 kobj_addrcheck(void *xmp, caddr_t adr)
62 {
63 	struct module *mp;
64 
65 	mp = (struct module *)xmp;
66 
67 	if ((adr >= mp->text && adr < mp->text + mp->text_size) ||
68 	    (adr >= mp->data && adr < mp->data + mp->data_size))
69 		return (0); /* ok */
70 	if (mp->bss && adr >= (caddr_t)mp->bss &&
71 	    adr < (caddr_t)mp->bss + mp->bss_size)
72 		return (0);
73 	return (1);
74 }
75 
76 /*
77  * Flush instruction cache after updating text
78  */
79 void
80 kobj_sync_instruction_memory(caddr_t addr, size_t len)
81 {
82 	caddr_t	eaddr = addr + len;
83 
84 	if (!use_iflush)
85 		return;
86 
87 	addr = (caddr_t)((uintptr_t)addr & -8l); /* sparc needs 8-byte align */
88 
89 	while (addr < eaddr) {
90 		doflush(addr);
91 		addr += 8;
92 	}
93 }
94 
95 /*
96  * Calculate memory image required for relocatable object.
97  */
98 /* ARGSUSED3 */
99 int
100 get_progbits_size(struct module *mp, struct proginfo *tp, struct proginfo *dp,
101 	struct proginfo *sdp)
102 {
103 	struct proginfo *pp;
104 	uint_t shn;
105 	Shdr *shp;
106 
107 	/*
108 	 * loop through sections to find out how much space we need
109 	 * for text, data, (also bss that is already assigned)
110 	 */
111 	for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
112 		shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
113 		if (!(shp->sh_flags & SHF_ALLOC))
114 			continue;
115 		if (shp->sh_addr != 0) {
116 			_kobj_printf(ops,
117 			    "%s non-zero sect addr in input file\n",
118 			    mp->filename);
119 			return (-1);
120 		}
121 		pp = (shp->sh_flags & SHF_WRITE)? dp : tp;
122 
123 		if (shp->sh_addralign > pp->align)
124 			pp->align = shp->sh_addralign;
125 		pp->size = ALIGN(pp->size, shp->sh_addralign);
126 		pp->size += ALIGN(shp->sh_size, 8);
127 	}
128 	tp->size = ALIGN(tp->size, 8);
129 	dp->size = ALIGN(dp->size, 8);
130 	return (0);
131 }
132