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) {
58 curp = preload_metadata;
60 hdr = (uint32_t *)curp;
61 if (hdr[0] == 0 && hdr[1] == 0)
64 /* Search for a MODINFO_NAME field */
65 if ((hdr[0] == MODINFO_NAME) &&
66 !strcmp(name, curp + sizeof(uint32_t) * 2))
69 /* skip to next field */
70 next = sizeof(uint32_t) * 2 + hdr[1];
71 next = roundup(next, sizeof(u_long));
79 * Search for the first preloaded module of (type)
82 preload_search_by_type(const char *type)
88 if (preload_metadata != NULL) {
90 curp = preload_metadata;
93 hdr = (uint32_t *)curp;
94 if (hdr[0] == 0 && hdr[1] == 0)
97 /* remember the start of each record */
98 if (hdr[0] == MODINFO_NAME)
101 /* Search for a MODINFO_TYPE field */
102 if ((hdr[0] == MODINFO_TYPE) &&
103 !strcmp(type, curp + sizeof(uint32_t) * 2))
106 /* skip to next field */
107 next = sizeof(uint32_t) * 2 + hdr[1];
108 next = roundup(next, sizeof(u_long));
116 * Walk through the preloaded module list
119 preload_search_next_name(caddr_t base)
125 if (preload_metadata != NULL) {
127 /* Pick up where we left off last time */
129 /* skip to next field */
131 hdr = (uint32_t *)curp;
132 next = sizeof(uint32_t) * 2 + hdr[1];
133 next = roundup(next, sizeof(u_long));
136 curp = preload_metadata;
139 hdr = (uint32_t *)curp;
140 if (hdr[0] == 0 && hdr[1] == 0)
143 /* Found a new record? */
144 if (hdr[0] == MODINFO_NAME)
147 /* skip to next field */
148 next = sizeof(uint32_t) * 2 + hdr[1];
149 next = roundup(next, sizeof(u_long));
157 * Given a preloaded module handle (mod), return a pointer
158 * to the data for the attribute (inf).
161 preload_search_info(caddr_t mod, int inf)
173 hdr = (uint32_t *)curp;
174 /* end of module data? */
175 if (hdr[0] == 0 && hdr[1] == 0)
178 * We give up once we've looped back to what we were looking at
179 * first - this should normally be a MODINFO_NAME field.
189 * Attribute match? Return pointer to data.
190 * Consumer may safely assume that size value precedes
194 return(curp + (sizeof(uint32_t) * 2));
196 /* skip to next field */
197 next = sizeof(uint32_t) * 2 + hdr[1];
198 next = roundup(next, sizeof(u_long));
205 * Delete a preload record by name.
208 preload_delete_name(const char *name)
218 if (preload_metadata != NULL) {
221 curp = preload_metadata;
223 hdr = (uint32_t *)curp;
224 if (hdr[0] == MODINFO_NAME || (hdr[0] == 0 && hdr[1] == 0)) {
225 /* Free memory used to store the file. */
226 if (addr != 0 && sz != 0)
227 kmem_bootstrap_free((vm_offset_t)addr, sz);
233 if (!strcmp(name, curp + sizeof(uint32_t) * 2))
234 clearing = 1; /* got it, start clearing */
236 clearing = 0; /* at next one now.. better stop */
240 if (hdr[0] == MODINFO_ADDR)
241 addr = *(caddr_t *)(curp + sizeof(uint32_t) * 2);
242 else if (hdr[0] == MODINFO_SIZE)
243 sz = *(uint32_t *)(curp + sizeof(uint32_t) * 2);
244 hdr[0] = MODINFO_EMPTY;
247 /* skip to next field */
248 next = sizeof(uint32_t) * 2 + hdr[1];
249 next = roundup(next, sizeof(u_long));
256 preload_fetch_addr(caddr_t mod)
260 mdp = (caddr_t *)preload_search_info(mod, MODINFO_ADDR);
263 return (*mdp + preload_addr_relocate);
267 preload_fetch_size(caddr_t mod)
271 mdp = (size_t *)preload_search_info(mod, MODINFO_SIZE);
277 /* Called from locore. Convert physical pointers to kvm. Sigh. */
279 preload_bootstrap_relocate(vm_offset_t offset)
286 if (preload_metadata != NULL) {
288 curp = preload_metadata;
290 hdr = (uint32_t *)curp;
291 if (hdr[0] == 0 && hdr[1] == 0)
294 /* Deal with the ones that we know we have to fix */
297 case MODINFO_METADATA|MODINFOMD_SSYM:
298 case MODINFO_METADATA|MODINFOMD_ESYM:
299 ptr = (vm_offset_t *)(curp + (sizeof(uint32_t) * 2));
303 /* The rest is beyond us for now */
305 /* skip to next field */
306 next = sizeof(uint32_t) * 2 + hdr[1];
307 next = roundup(next, sizeof(u_long));