1998-10-09 00:31:29 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 Michael Smith
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1998-10-09 00:31:29 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/linker.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Preloaded module support
|
|
|
|
*/
|
|
|
|
|
Provide convenience function for obtaining MODINFO_ADDR and MODINFO_SIZE
attributes for preloaded modules/images. In particular, MODINFO_ADDR has
the added complexity of not always being relocated properly. Rather than
kluging this in the various components that are affected, we handle it
in a centralized place (preload_fetch_addr()). To that end, expose a new
variable, preload_addr_relocate, that MD initialization code can set and
that turns the address attribute into a valid kernel VA.
Architectures that need the relocation: arm & powerpc (at least).
Components that can utilize this: acpi(4), md(4), fb(4), pci(4), ZFS, geli.
Sponsored by: Juniper Networks
2011-02-09 19:08:21 +00:00
|
|
|
vm_offset_t preload_addr_relocate = 0;
|
|
|
|
caddr_t preload_metadata;
|
1998-10-09 00:31:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for the preloaded module (name)
|
|
|
|
*/
|
|
|
|
caddr_t
|
1998-10-09 23:59:01 +00:00
|
|
|
preload_search_by_name(const char *name)
|
1998-10-09 00:31:29 +00:00
|
|
|
{
|
|
|
|
caddr_t curp;
|
2010-06-21 09:55:56 +00:00
|
|
|
uint32_t *hdr;
|
1998-10-09 23:59:01 +00:00
|
|
|
int next;
|
1998-10-09 00:31:29 +00:00
|
|
|
|
1998-10-09 23:59:01 +00:00
|
|
|
if (preload_metadata != NULL) {
|
1998-10-09 00:31:29 +00:00
|
|
|
|
1998-10-09 23:59:01 +00:00
|
|
|
curp = preload_metadata;
|
1998-10-09 00:31:29 +00:00
|
|
|
for (;;) {
|
2010-06-21 09:55:56 +00:00
|
|
|
hdr = (uint32_t *)curp;
|
1998-10-09 23:59:01 +00:00
|
|
|
if (hdr[0] == 0 && hdr[1] == 0)
|
1998-10-09 00:31:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Search for a MODINFO_NAME field */
|
|
|
|
if ((hdr[0] == MODINFO_NAME) &&
|
2010-06-21 09:55:56 +00:00
|
|
|
!strcmp(name, curp + sizeof(uint32_t) * 2))
|
1998-10-09 00:31:29 +00:00
|
|
|
return(curp);
|
|
|
|
|
|
|
|
/* skip to next field */
|
2010-06-21 09:55:56 +00:00
|
|
|
next = sizeof(uint32_t) * 2 + hdr[1];
|
1998-10-14 05:07:23 +00:00
|
|
|
next = roundup(next, sizeof(u_long));
|
1998-10-09 23:59:01 +00:00
|
|
|
curp += next;
|
1998-10-09 00:31:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for the first preloaded module of (type)
|
|
|
|
*/
|
|
|
|
caddr_t
|
1998-10-09 23:59:01 +00:00
|
|
|
preload_search_by_type(const char *type)
|
1998-10-09 00:31:29 +00:00
|
|
|
{
|
|
|
|
caddr_t curp, lname;
|
2010-06-21 09:55:56 +00:00
|
|
|
uint32_t *hdr;
|
1998-10-09 23:59:01 +00:00
|
|
|
int next;
|
1998-10-09 00:31:29 +00:00
|
|
|
|
1998-10-09 23:59:01 +00:00
|
|
|
if (preload_metadata != NULL) {
|
1998-10-09 00:31:29 +00:00
|
|
|
|
1998-10-09 23:59:01 +00:00
|
|
|
curp = preload_metadata;
|
1998-10-09 00:31:29 +00:00
|
|
|
lname = NULL;
|
|
|
|
for (;;) {
|
2010-06-21 09:55:56 +00:00
|
|
|
hdr = (uint32_t *)curp;
|
1998-10-09 23:59:01 +00:00
|
|
|
if (hdr[0] == 0 && hdr[1] == 0)
|
1998-10-09 00:31:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* remember the start of each record */
|
|
|
|
if (hdr[0] == MODINFO_NAME)
|
|
|
|
lname = curp;
|
|
|
|
|
|
|
|
/* Search for a MODINFO_TYPE field */
|
|
|
|
if ((hdr[0] == MODINFO_TYPE) &&
|
2010-06-21 09:55:56 +00:00
|
|
|
!strcmp(type, curp + sizeof(uint32_t) * 2))
|
1998-10-09 00:31:29 +00:00
|
|
|
return(lname);
|
|
|
|
|
|
|
|
/* skip to next field */
|
2010-06-21 09:55:56 +00:00
|
|
|
next = sizeof(uint32_t) * 2 + hdr[1];
|
1998-10-14 05:07:23 +00:00
|
|
|
next = roundup(next, sizeof(u_long));
|
1998-10-09 23:59:01 +00:00
|
|
|
curp += next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk through the preloaded module list
|
|
|
|
*/
|
|
|
|
caddr_t
|
|
|
|
preload_search_next_name(caddr_t base)
|
|
|
|
{
|
|
|
|
caddr_t curp;
|
2010-06-21 09:55:56 +00:00
|
|
|
uint32_t *hdr;
|
1998-10-09 23:59:01 +00:00
|
|
|
int next;
|
|
|
|
|
|
|
|
if (preload_metadata != NULL) {
|
|
|
|
|
|
|
|
/* Pick up where we left off last time */
|
|
|
|
if (base) {
|
|
|
|
/* skip to next field */
|
|
|
|
curp = base;
|
2010-06-21 09:55:56 +00:00
|
|
|
hdr = (uint32_t *)curp;
|
|
|
|
next = sizeof(uint32_t) * 2 + hdr[1];
|
1998-10-14 05:07:23 +00:00
|
|
|
next = roundup(next, sizeof(u_long));
|
1998-10-09 23:59:01 +00:00
|
|
|
curp += next;
|
|
|
|
} else
|
|
|
|
curp = preload_metadata;
|
|
|
|
|
|
|
|
for (;;) {
|
2010-06-21 09:55:56 +00:00
|
|
|
hdr = (uint32_t *)curp;
|
1998-10-09 23:59:01 +00:00
|
|
|
if (hdr[0] == 0 && hdr[1] == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Found a new record? */
|
|
|
|
if (hdr[0] == MODINFO_NAME)
|
|
|
|
return curp;
|
|
|
|
|
|
|
|
/* skip to next field */
|
2010-06-21 09:55:56 +00:00
|
|
|
next = sizeof(uint32_t) * 2 + hdr[1];
|
1998-10-14 05:07:23 +00:00
|
|
|
next = roundup(next, sizeof(u_long));
|
1998-10-09 23:59:01 +00:00
|
|
|
curp += next;
|
1998-10-09 00:31:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a preloaded module handle (mod), return a pointer
|
|
|
|
* to the data for the attribute (inf).
|
|
|
|
*/
|
|
|
|
caddr_t
|
1998-10-09 23:59:01 +00:00
|
|
|
preload_search_info(caddr_t mod, int inf)
|
1998-10-09 00:31:29 +00:00
|
|
|
{
|
|
|
|
caddr_t curp;
|
2010-06-21 09:55:56 +00:00
|
|
|
uint32_t *hdr;
|
|
|
|
uint32_t type = 0;
|
1998-10-09 23:59:01 +00:00
|
|
|
int next;
|
1998-10-09 00:31:29 +00:00
|
|
|
|
2015-08-21 15:57:57 +00:00
|
|
|
if (mod == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
1998-10-09 00:31:29 +00:00
|
|
|
curp = mod;
|
|
|
|
for (;;) {
|
2010-06-21 09:55:56 +00:00
|
|
|
hdr = (uint32_t *)curp;
|
1998-10-09 00:31:29 +00:00
|
|
|
/* end of module data? */
|
1998-10-09 23:59:01 +00:00
|
|
|
if (hdr[0] == 0 && hdr[1] == 0)
|
1998-10-09 00:31:29 +00:00
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* We give up once we've looped back to what we were looking at
|
|
|
|
* first - this should normally be a MODINFO_NAME field.
|
|
|
|
*/
|
|
|
|
if (type == 0) {
|
|
|
|
type = hdr[0];
|
|
|
|
} else {
|
|
|
|
if (hdr[0] == type)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attribute match? Return pointer to data.
|
2001-02-18 10:43:53 +00:00
|
|
|
* Consumer may safely assume that size value precedes
|
1998-10-09 00:31:29 +00:00
|
|
|
* data.
|
|
|
|
*/
|
|
|
|
if (hdr[0] == inf)
|
2010-06-21 09:55:56 +00:00
|
|
|
return(curp + (sizeof(uint32_t) * 2));
|
1998-10-09 00:31:29 +00:00
|
|
|
|
|
|
|
/* skip to next field */
|
2010-06-21 09:55:56 +00:00
|
|
|
next = sizeof(uint32_t) * 2 + hdr[1];
|
1998-10-14 05:07:23 +00:00
|
|
|
next = roundup(next, sizeof(u_long));
|
1998-10-09 23:59:01 +00:00
|
|
|
curp += next;
|
1998-10-09 00:31:29 +00:00
|
|
|
}
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
1998-10-09 23:59:01 +00:00
|
|
|
/*
|
|
|
|
* Delete a preload record by name.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
preload_delete_name(const char *name)
|
|
|
|
{
|
|
|
|
caddr_t curp;
|
2010-06-21 09:55:56 +00:00
|
|
|
uint32_t *hdr;
|
1998-10-09 23:59:01 +00:00
|
|
|
int next;
|
|
|
|
int clearing;
|
|
|
|
|
|
|
|
if (preload_metadata != NULL) {
|
|
|
|
|
|
|
|
clearing = 0;
|
|
|
|
curp = preload_metadata;
|
|
|
|
for (;;) {
|
2010-06-21 09:55:56 +00:00
|
|
|
hdr = (uint32_t *)curp;
|
1998-10-09 23:59:01 +00:00
|
|
|
if (hdr[0] == 0 && hdr[1] == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Search for a MODINFO_NAME field */
|
|
|
|
if (hdr[0] == MODINFO_NAME) {
|
2010-06-21 09:55:56 +00:00
|
|
|
if (!strcmp(name, curp + sizeof(uint32_t) * 2))
|
1998-10-09 23:59:01 +00:00
|
|
|
clearing = 1; /* got it, start clearing */
|
|
|
|
else if (clearing)
|
|
|
|
clearing = 0; /* at next one now.. better stop */
|
|
|
|
}
|
|
|
|
if (clearing)
|
|
|
|
hdr[0] = MODINFO_EMPTY;
|
|
|
|
|
|
|
|
/* skip to next field */
|
2010-06-21 09:55:56 +00:00
|
|
|
next = sizeof(uint32_t) * 2 + hdr[1];
|
1998-10-14 05:07:23 +00:00
|
|
|
next = roundup(next, sizeof(u_long));
|
1998-10-09 23:59:01 +00:00
|
|
|
curp += next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Provide convenience function for obtaining MODINFO_ADDR and MODINFO_SIZE
attributes for preloaded modules/images. In particular, MODINFO_ADDR has
the added complexity of not always being relocated properly. Rather than
kluging this in the various components that are affected, we handle it
in a centralized place (preload_fetch_addr()). To that end, expose a new
variable, preload_addr_relocate, that MD initialization code can set and
that turns the address attribute into a valid kernel VA.
Architectures that need the relocation: arm & powerpc (at least).
Components that can utilize this: acpi(4), md(4), fb(4), pci(4), ZFS, geli.
Sponsored by: Juniper Networks
2011-02-09 19:08:21 +00:00
|
|
|
void *
|
|
|
|
preload_fetch_addr(caddr_t mod)
|
|
|
|
{
|
|
|
|
caddr_t *mdp;
|
|
|
|
|
|
|
|
mdp = (caddr_t *)preload_search_info(mod, MODINFO_ADDR);
|
|
|
|
if (mdp == NULL)
|
|
|
|
return (NULL);
|
|
|
|
return (*mdp + preload_addr_relocate);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
preload_fetch_size(caddr_t mod)
|
|
|
|
{
|
|
|
|
size_t *mdp;
|
|
|
|
|
|
|
|
mdp = (size_t *)preload_search_info(mod, MODINFO_SIZE);
|
|
|
|
if (mdp == NULL)
|
|
|
|
return (0);
|
|
|
|
return (*mdp);
|
|
|
|
}
|
|
|
|
|
2014-12-30 02:39:47 +00:00
|
|
|
/* Called from locore. Convert physical pointers to kvm. Sigh. */
|
1998-10-09 23:59:01 +00:00
|
|
|
void
|
|
|
|
preload_bootstrap_relocate(vm_offset_t offset)
|
|
|
|
{
|
|
|
|
caddr_t curp;
|
2010-06-21 09:55:56 +00:00
|
|
|
uint32_t *hdr;
|
1998-10-09 23:59:01 +00:00
|
|
|
vm_offset_t *ptr;
|
|
|
|
int next;
|
|
|
|
|
|
|
|
if (preload_metadata != NULL) {
|
|
|
|
|
|
|
|
curp = preload_metadata;
|
|
|
|
for (;;) {
|
2010-06-21 09:55:56 +00:00
|
|
|
hdr = (uint32_t *)curp;
|
1998-10-09 23:59:01 +00:00
|
|
|
if (hdr[0] == 0 && hdr[1] == 0)
|
|
|
|
break;
|
|
|
|
|
1998-10-12 09:03:48 +00:00
|
|
|
/* Deal with the ones that we know we have to fix */
|
|
|
|
switch (hdr[0]) {
|
|
|
|
case MODINFO_ADDR:
|
|
|
|
case MODINFO_METADATA|MODINFOMD_SSYM:
|
|
|
|
case MODINFO_METADATA|MODINFOMD_ESYM:
|
2010-06-21 09:55:56 +00:00
|
|
|
ptr = (vm_offset_t *)(curp + (sizeof(uint32_t) * 2));
|
1998-10-09 23:59:01 +00:00
|
|
|
*ptr += offset;
|
1998-10-12 09:03:48 +00:00
|
|
|
break;
|
1998-10-09 23:59:01 +00:00
|
|
|
}
|
|
|
|
/* The rest is beyond us for now */
|
|
|
|
|
|
|
|
/* skip to next field */
|
2010-06-21 09:55:56 +00:00
|
|
|
next = sizeof(uint32_t) * 2 + hdr[1];
|
1998-10-14 05:07:23 +00:00
|
|
|
next = roundup(next, sizeof(u_long));
|
1998-10-09 23:59:01 +00:00
|
|
|
curp += next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|