2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998 Michael Smith
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/linker.h>
37 #include <vm/vm_extern.h>
40 * Preloaded module support
43 vm_offset_t preload_addr_relocate = 0;
44 caddr_t preload_metadata;
47 * Search for the preloaded module (name)
50 preload_search_by_name(const char *name)
56 if (preload_metadata != NULL) {
57 curp = preload_metadata;
59 hdr = (uint32_t *)curp;
60 if (hdr[0] == 0 && hdr[1] == 0)
63 /* Search for a MODINFO_NAME field */
64 if ((hdr[0] == MODINFO_NAME) &&
65 !strcmp(name, curp + sizeof(uint32_t) * 2))
68 /* skip to next field */
69 next = sizeof(uint32_t) * 2 + hdr[1];
70 next = roundup(next, sizeof(u_long));
78 * Search for the first preloaded module of (type)
81 preload_search_by_type(const char *type)
87 if (preload_metadata != NULL) {
88 curp = preload_metadata;
91 hdr = (uint32_t *)curp;
92 if (hdr[0] == 0 && hdr[1] == 0)
95 /* remember the start of each record */
96 if (hdr[0] == MODINFO_NAME)
99 /* Search for a MODINFO_TYPE field */
100 if ((hdr[0] == MODINFO_TYPE) &&
101 !strcmp(type, curp + sizeof(uint32_t) * 2))
104 /* skip to next field */
105 next = sizeof(uint32_t) * 2 + hdr[1];
106 next = roundup(next, sizeof(u_long));
114 * Walk through the preloaded module list
117 preload_search_next_name(caddr_t base)
123 if (preload_metadata != NULL) {
124 /* Pick up where we left off last time */
126 /* skip to next field */
128 hdr = (uint32_t *)curp;
129 next = sizeof(uint32_t) * 2 + hdr[1];
130 next = roundup(next, sizeof(u_long));
133 curp = preload_metadata;
136 hdr = (uint32_t *)curp;
137 if (hdr[0] == 0 && hdr[1] == 0)
140 /* Found a new record? */
141 if (hdr[0] == MODINFO_NAME)
144 /* skip to next field */
145 next = sizeof(uint32_t) * 2 + hdr[1];
146 next = roundup(next, sizeof(u_long));
154 * Given a preloaded module handle (mod), return a pointer
155 * to the data for the attribute (inf).
158 preload_search_info(caddr_t mod, int inf)
170 hdr = (uint32_t *)curp;
171 /* end of module data? */
172 if (hdr[0] == 0 && hdr[1] == 0)
175 * We give up once we've looped back to what we were looking at
176 * first - this should normally be a MODINFO_NAME field.
186 * Attribute match? Return pointer to data.
187 * Consumer may safely assume that size value precedes
191 return(curp + (sizeof(uint32_t) * 2));
193 /* skip to next field */
194 next = sizeof(uint32_t) * 2 + hdr[1];
195 next = roundup(next, sizeof(u_long));
202 * Delete a preload record by name.
205 preload_delete_name(const char *name)
215 if (preload_metadata != NULL) {
217 curp = preload_metadata;
219 hdr = (uint32_t *)curp;
220 if (hdr[0] == MODINFO_NAME || (hdr[0] == 0 && hdr[1] == 0)) {
221 /* Free memory used to store the file. */
222 if (addr != 0 && sz != 0)
223 kmem_bootstrap_free((vm_offset_t)addr, sz);
229 if (!strcmp(name, curp + sizeof(uint32_t) * 2))
230 clearing = 1; /* got it, start clearing */
232 clearing = 0; /* at next one now.. better stop */
236 if (hdr[0] == MODINFO_ADDR)
237 addr = *(caddr_t *)(curp + sizeof(uint32_t) * 2);
238 else if (hdr[0] == MODINFO_SIZE)
239 sz = *(uint32_t *)(curp + sizeof(uint32_t) * 2);
240 hdr[0] = MODINFO_EMPTY;
243 /* skip to next field */
244 next = sizeof(uint32_t) * 2 + hdr[1];
245 next = roundup(next, sizeof(u_long));
252 preload_fetch_addr(caddr_t mod)
256 mdp = (caddr_t *)preload_search_info(mod, MODINFO_ADDR);
259 return (*mdp + preload_addr_relocate);
263 preload_fetch_size(caddr_t mod)
267 mdp = (size_t *)preload_search_info(mod, MODINFO_SIZE);
273 /* Called from locore. Convert physical pointers to kvm. Sigh. */
275 preload_bootstrap_relocate(vm_offset_t offset)
282 if (preload_metadata != NULL) {
283 curp = preload_metadata;
285 hdr = (uint32_t *)curp;
286 if (hdr[0] == 0 && hdr[1] == 0)
289 /* Deal with the ones that we know we have to fix */
292 case MODINFO_METADATA|MODINFOMD_SSYM:
293 case MODINFO_METADATA|MODINFOMD_ESYM:
294 ptr = (vm_offset_t *)(curp + (sizeof(uint32_t) * 2));
298 /* The rest is beyond us for now */
300 /* skip to next field */
301 next = sizeof(uint32_t) * 2 + hdr[1];
302 next = roundup(next, sizeof(u_long));