[2826] | 1 | /* $Id: kLdr.c 2869 2006-11-12 02:47:25Z bird $ */
|
---|
[2821] | 2 | /** @file
|
---|
| 3 | *
|
---|
| 4 | * kLdr - The Dynamic Loader.
|
---|
| 5 | *
|
---|
| 6 | * Copyright (c) 2006 knut st. osmundsen <bird@anduin.net>
|
---|
| 7 | *
|
---|
| 8 | *
|
---|
| 9 | * This file is part of kLdr.
|
---|
| 10 | *
|
---|
| 11 | * kLdr is free software; you can redistribute it and/or modify
|
---|
| 12 | * it under the terms of the GNU General Public License as published by
|
---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 14 | * (at your option) any later version.
|
---|
| 15 | *
|
---|
| 16 | * kLdr is distributed in the hope that it will be useful,
|
---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | * GNU General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU General Public License
|
---|
| 22 | * along with kLdr; if not, write to the Free Software
|
---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
| 24 | *
|
---|
| 25 | */
|
---|
| 26 |
|
---|
[2852] | 27 | /** @mainpage kLdr - The Dynamic Loader
|
---|
[2824] | 28 | *
|
---|
| 29 | * The purpose of kLdr is to provide a generic interface for querying
|
---|
| 30 | * information about and loading executable image modules.
|
---|
| 31 | *
|
---|
| 32 | * kLdr defines the term executable image to include all kinds of files that contains
|
---|
| 33 | * binary code that can be executed on a CPU - linker objects (OBJs/Os), shared
|
---|
| 34 | * objects (SOs), dynamic link libraries (DLLs), executables (EXEs), and all kinds
|
---|
| 35 | * of kernel modules / device drivers (SYSs).
|
---|
| 36 | *
|
---|
| 37 | * kLdr provides two types of services:
|
---|
| 38 | * -# Inspect or/and load individual modules (kLdrMod).
|
---|
| 39 | * -# Work as a dynamic loader - construct and maintain an address space (kLdrDy).
|
---|
| 40 | *
|
---|
| 41 | * The kLdrMod API works on KLDRMOD structures where all the internals are exposed, while
|
---|
| 42 | * the kLdrDy API works opque KLDRDY structures. KLDRDY are in reality simple wrappers
|
---|
| 43 | * around KLDRMOD with some extra linking and attributes.
|
---|
| 44 | *
|
---|
| 45 | */
|
---|
| 46 |
|
---|
| 47 |
|
---|
[2853] | 48 |
|
---|
| 49 |
|
---|
| 50 | /*******************************************************************************
|
---|
| 51 | * Header Files *
|
---|
| 52 | *******************************************************************************/
|
---|
[2821] | 53 | #include "kLdr.h"
|
---|
[2824] | 54 | #include "kLdrHlp.h"
|
---|
[2821] | 55 |
|
---|
[2830] | 56 |
|
---|
[2821] | 57 | /*******************************************************************************
|
---|
[2853] | 58 | * Global Variables *
|
---|
[2821] | 59 | *******************************************************************************/
|
---|
[2830] | 60 | /** Flag indicating whether we've initialized the loader or not.
|
---|
| 61 | *
|
---|
| 62 | * 0 if not initialized.
|
---|
| 63 | * -1 if we're initializing or terminating.
|
---|
| 64 | * 1 if we've successfully initialized it.
|
---|
| 65 | * -2 if initialization failed.
|
---|
| 66 | */
|
---|
| 67 | static int volatile g_fInitialized;
|
---|
[2821] | 68 |
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
[2830] | 72 | * Initializes the loader.
|
---|
| 73 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
[2821] | 74 | */
|
---|
| 75 | int kldrInit(void)
|
---|
| 76 | {
|
---|
[2830] | 77 | int rc;
|
---|
| 78 |
|
---|
| 79 | /* check we're already good. */
|
---|
| 80 | if (g_fInitialized == 1)
|
---|
[2821] | 81 | return 0;
|
---|
| 82 |
|
---|
[2830] | 83 | /* a tiny serialization effort. */
|
---|
| 84 | for (;;)
|
---|
| 85 | {
|
---|
| 86 | if (g_fInitialized == 1)
|
---|
| 87 | return 0;
|
---|
| 88 | if (g_fInitialized == -2)
|
---|
| 89 | return -1;
|
---|
| 90 | /** @todo atomic test and set if we care. */
|
---|
| 91 | if (g_fInitialized == 0)
|
---|
[2821] | 92 | {
|
---|
[2830] | 93 | g_fInitialized = -1;
|
---|
| 94 | break;
|
---|
[2821] | 95 | }
|
---|
[2830] | 96 | kldrHlpSleep(1);
|
---|
[2821] | 97 | }
|
---|
| 98 |
|
---|
| 99 | /*
|
---|
[2830] | 100 | * Do the initialization.
|
---|
[2821] | 101 | */
|
---|
[2830] | 102 | rc = kldrHlpHeapInit();
|
---|
| 103 | if (!rc)
|
---|
[2821] | 104 | {
|
---|
[2830] | 105 | rc = kldrHlpSemInit();
|
---|
| 106 | if (!rc)
|
---|
| 107 | {
|
---|
[2869] | 108 | rc = kldrDyldInit();
|
---|
| 109 | if (!rc)
|
---|
| 110 | {
|
---|
| 111 | g_fInitialized = 1;
|
---|
| 112 | return 0;
|
---|
| 113 | }
|
---|
| 114 | kldrHlpSemTerm();
|
---|
[2830] | 115 | }
|
---|
| 116 | kldrHlpHeapTerm();
|
---|
[2821] | 117 | }
|
---|
[2830] | 118 | g_fInitialized = -2;
|
---|
| 119 | return rc;
|
---|
| 120 | }
|
---|
[2821] | 121 |
|
---|
| 122 |
|
---|
[2830] | 123 | /**
|
---|
| 124 | * Terminates the loader.
|
---|
| 125 | */
|
---|
| 126 | void kldrTerm(void)
|
---|
| 127 | {
|
---|
| 128 | /* can't terminate unless it's initialized. */
|
---|
| 129 | if (g_fInitialized != 1)
|
---|
| 130 | return;
|
---|
| 131 | g_fInitialized = -1;
|
---|
[2821] | 132 |
|
---|
| 133 | /*
|
---|
[2830] | 134 | * Do the termination.
|
---|
[2821] | 135 | */
|
---|
[2830] | 136 | kldrHlpSemTerm();
|
---|
| 137 | kldrHlpHeapTerm();
|
---|
[2821] | 138 |
|
---|
[2830] | 139 | /* done */
|
---|
| 140 | g_fInitialized = 0;
|
---|
[2821] | 141 | }
|
---|
| 142 |
|
---|
[2855] | 143 |
|
---|
| 144 | /**
|
---|
| 145 | * Compares arch+cpu some code was generated for with a arch+cpu for executing it
|
---|
| 146 | * to see if it'll work out fine or not.
|
---|
| 147 | *
|
---|
| 148 | * @returns 0 if the code is compatible with the cpu.
|
---|
| 149 | * @returns KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE if the arch+cpu isn't compatible with the code.
|
---|
| 150 | * @param enmCodeArch The architecture the code was generated for.
|
---|
| 151 | * @param enmCodeCpu The cpu the code was generated for.
|
---|
| 152 | * @param enmArch The architecture to run it on.
|
---|
| 153 | * @param enmCpu The cpu to run it on.
|
---|
| 154 | */
|
---|
| 155 | int kLdrCompareCpus(KLDRARCH enmCodeArch, KLDRCPU enmCodeCpu, KLDRARCH enmArch, KLDRCPU enmCpu)
|
---|
| 156 | {
|
---|
| 157 | /*
|
---|
| 158 | * Compare arch and cpu.
|
---|
| 159 | */
|
---|
| 160 | if (enmCodeArch != enmArch)
|
---|
| 161 | return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
| 162 |
|
---|
| 163 | /* exact match is nice. */
|
---|
| 164 | if (enmCodeCpu == enmCpu)
|
---|
| 165 | return 0;
|
---|
| 166 | switch (enmArch)
|
---|
| 167 | {
|
---|
| 168 | case KLDRARCH_X86_16:
|
---|
| 169 | if (enmCpu < KLDRCPU_FIRST_X86_16 || enmCpu > KLDRCPU_LAST_X86_16)
|
---|
| 170 | return KLDR_ERR_INVALID_PARAMETER;
|
---|
| 171 |
|
---|
| 172 | /* intel? */
|
---|
| 173 | if (enmCodeCpu <= KLDRCPU_CORE2_16)
|
---|
| 174 | {
|
---|
| 175 | /* also intel? */
|
---|
| 176 | if (enmCpu <= KLDRCPU_CORE2_16)
|
---|
| 177 | return enmCodeCpu <= enmCpu ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
| 178 | switch (enmCpu)
|
---|
| 179 | {
|
---|
| 180 | case KLDRCPU_K6_16:
|
---|
| 181 | return enmCodeCpu <= KLDRCPU_I586 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
| 182 | case KLDRCPU_K7_16:
|
---|
| 183 | case KLDRCPU_K8_16:
|
---|
| 184 | default:
|
---|
| 185 | return enmCodeCpu <= KLDRCPU_I686 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | /* amd */
|
---|
| 189 | return enmCpu >= KLDRCPU_K6_16 && enmCpu <= KLDRCPU_K8_16
|
---|
| 190 | ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
| 191 |
|
---|
| 192 | case KLDRARCH_X86_32:
|
---|
| 193 | if (enmCpu < KLDRCPU_FIRST_X86_32 || enmCpu > KLDRCPU_LAST_X86_32)
|
---|
| 194 | return KLDR_ERR_INVALID_PARAMETER;
|
---|
| 195 |
|
---|
| 196 | /* blend? */
|
---|
| 197 | if (enmCodeCpu == KLDRCPU_X86_32_BLEND)
|
---|
| 198 | return 0;
|
---|
| 199 |
|
---|
| 200 | /* intel? */
|
---|
| 201 | if (enmCodeCpu <= KLDRCPU_CORE2_32)
|
---|
| 202 | {
|
---|
| 203 | /* also intel? */
|
---|
| 204 | if (enmCpu <= KLDRCPU_CORE2_32)
|
---|
| 205 | return enmCodeCpu <= enmCpu ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
| 206 | switch (enmCpu)
|
---|
| 207 | {
|
---|
| 208 | case KLDRCPU_K6:
|
---|
| 209 | return enmCodeCpu <= KLDRCPU_I586 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
| 210 | case KLDRCPU_K7:
|
---|
| 211 | case KLDRCPU_K8_32:
|
---|
| 212 | default:
|
---|
| 213 | return enmCodeCpu <= KLDRCPU_I686 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 | /* amd */
|
---|
| 217 | return enmCpu >= KLDRCPU_K6 && enmCpu <= KLDRCPU_K8_32
|
---|
| 218 | ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
| 219 |
|
---|
| 220 | case KLDRARCH_AMD64:
|
---|
| 221 | if (enmCpu < KLDRCPU_FIRST_AMD64 || enmCpu > KLDRCPU_LAST_AMD64)
|
---|
| 222 | return KLDR_ERR_INVALID_PARAMETER;
|
---|
| 223 |
|
---|
| 224 | /* blend? */
|
---|
| 225 | if (enmCodeCpu == KLDRCPU_AMD64_BLEND)
|
---|
| 226 | return 0;
|
---|
| 227 | /* this is simple for now. */
|
---|
| 228 | return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
| 229 |
|
---|
| 230 | default:
|
---|
| 231 | break;
|
---|
| 232 | }
|
---|
| 233 | return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 |
|
---|
| 237 | /**
|
---|
| 238 | * Gets the arch+cpu of the calling cpu.
|
---|
| 239 | *
|
---|
| 240 | * @param penmArch Where to store the cpu architecture.
|
---|
| 241 | * @param penmCpu Where to store the cpu brand/model.
|
---|
| 242 | */
|
---|
| 243 | void kLdrGetArchCpu(PKLDRARCH penmArch, PKLDRCPU penmCpu)
|
---|
| 244 | {
|
---|
| 245 | #if defined(__x86_64__) || defined(_M_X64) || defined(__AMD64__) || defined(_M_AMD64)
|
---|
| 246 | *penmArch = KLDRARCH_AMD64;
|
---|
| 247 | *penmCpu = KLDRCPU_AMD64_BLEND; ///@todo check it using cpu.
|
---|
| 248 |
|
---|
| 249 | #elif defined(__i386__) || defined(_M_IX86)
|
---|
| 250 | *penmArch = KLDRARCH_X86_32;
|
---|
| 251 | *penmCpu = KLDRCPU_X86_32_BLEND; ///@todo check it using cpu.
|
---|
| 252 |
|
---|
| 253 | #else
|
---|
| 254 | # error "Port me"
|
---|
| 255 | #endif
|
---|
| 256 | }
|
---|