2 * Copyright (c) 1998 Michael Smith
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/kern/subr_module.c,v 1.6 1999/10/11 15:19:10 peter Exp $
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/linker.h>
34 * Preloaded module support
37 caddr_t preload_metadata;
40 * Search for the preloaded module (name)
43 preload_search_by_name(const char *name)
51 if (preload_metadata == NULL)
54 curp = preload_metadata;
56 hdr = (u_int32_t *)curp;
57 if (hdr[0] == 0 && hdr[1] == 0)
61 * Search for a MODINFO_NAME field. the boot loader really
62 * ought to strip the path names
64 if (hdr[0] == MODINFO_NAME) {
65 scanname = curp + sizeof(u_int32_t) * 2;
67 while (i > 0 && scanname[i-1] != '/')
69 if (strcmp(name, scanname) == 0)
71 if (strcmp(name, scanname + i) == 0)
74 /* skip to next field */
75 next = sizeof(u_int32_t) * 2 + hdr[1];
76 next = roundup(next, sizeof(u_long));
83 * Search for the first preloaded module of (type)
86 preload_search_by_type(const char *type)
92 if (preload_metadata != NULL) {
94 curp = preload_metadata;
97 hdr = (u_int32_t *)curp;
98 if (hdr[0] == 0 && hdr[1] == 0)
101 /* remember the start of each record */
102 if (hdr[0] == MODINFO_NAME)
105 /* Search for a MODINFO_TYPE field */
106 if ((hdr[0] == MODINFO_TYPE) &&
107 !strcmp(type, curp + sizeof(u_int32_t) * 2))
110 /* skip to next field */
111 next = sizeof(u_int32_t) * 2 + hdr[1];
112 next = roundup(next, sizeof(u_long));
120 * Walk through the preloaded module list
123 preload_search_next_name(caddr_t base)
129 if (preload_metadata != NULL) {
131 /* Pick up where we left off last time */
133 /* skip to next field */
135 hdr = (u_int32_t *)curp;
136 next = sizeof(u_int32_t) * 2 + hdr[1];
137 next = roundup(next, sizeof(u_long));
140 curp = preload_metadata;
143 hdr = (u_int32_t *)curp;
144 if (hdr[0] == 0 && hdr[1] == 0)
147 /* Found a new record? */
148 if (hdr[0] == MODINFO_NAME)
151 /* skip to next field */
152 next = sizeof(u_int32_t) * 2 + hdr[1];
153 next = roundup(next, sizeof(u_long));
161 * Given a preloaded module handle (mod), return a pointer
162 * to the data for the attribute (inf).
165 preload_search_info(caddr_t mod, int inf)
174 hdr = (u_int32_t *)curp;
175 /* end of module data? */
176 if (hdr[0] == 0 && hdr[1] == 0)
179 * We give up once we've looped back to what we were looking at
180 * first - this should normally be a MODINFO_NAME field.
190 * Attribute match? Return pointer to data.
191 * Consumer may safely assume that size value preceeds
195 return(curp + (sizeof(u_int32_t) * 2));
197 /* skip to next field */
198 next = sizeof(u_int32_t) * 2 + hdr[1];
199 next = roundup(next, sizeof(u_long));
206 * Delete a preload record by name.
208 * XXX we should really pass the base of the preloaded module here and not
209 * require rematching of the name. If the wrong module (or no module) is
210 * deleted, the original preloaded module might be loaded again, causing it's
211 * data to be relocated twice.
214 preload_delete_name(const char *name)
223 if (preload_metadata != NULL) {
225 curp = preload_metadata;
227 hdr = (u_int32_t *)curp;
228 if (hdr[0] == 0 && hdr[1] == 0)
231 /* Search for a MODINFO_NAME field */
232 if (hdr[0] == MODINFO_NAME) {
233 scanname = curp + sizeof(u_int32_t) * 2;
234 i = strlen(scanname);
235 while (i > 0 && scanname[i-1] != '/')
237 if (strcmp(name, scanname) == 0)
239 else if (strcmp(name, scanname + i) == 0)
242 clearing = 0; /* at next module now, stop clearing */
245 hdr[0] = MODINFO_EMPTY;
247 /* skip to next field */
248 next = sizeof(u_int32_t) * 2 + hdr[1];
249 next = roundup(next, sizeof(u_long));
255 /* Called from hammer_time() on pc64. Convert physical pointers to kvm. Sigh. */
257 preload_bootstrap_relocate(vm_offset_t offset)
264 if (preload_metadata != NULL) {
266 curp = preload_metadata;
268 hdr = (u_int32_t *)curp;
269 if (hdr[0] == 0 && hdr[1] == 0)
272 /* Deal with the ones that we know we have to fix */
275 case MODINFO_METADATA|MODINFOMD_SSYM:
276 case MODINFO_METADATA|MODINFOMD_ESYM:
277 ptr = (vm_offset_t *)(curp + (sizeof(u_int32_t) * 2));
281 /* The rest is beyond us for now */
283 /* skip to next field */
284 next = sizeof(u_int32_t) * 2 + hdr[1];
285 next = roundup(next, sizeof(u_long));