1 | /* $Id: kLdr.c 2855 2006-11-04 02:30:19Z bird $ */
|
---|
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 |
|
---|
27 | /** @mainpage kLdr - The Dynamic Loader
|
---|
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 |
|
---|
48 |
|
---|
49 |
|
---|
50 | /*******************************************************************************
|
---|
51 | * Header Files *
|
---|
52 | *******************************************************************************/
|
---|
53 | #include "kLdr.h"
|
---|
54 | #include "kLdrHlp.h"
|
---|
55 |
|
---|
56 |
|
---|
57 | /*******************************************************************************
|
---|
58 | * Global Variables *
|
---|
59 | *******************************************************************************/
|
---|
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;
|
---|
68 |
|
---|
69 |
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Initializes the loader.
|
---|
73 | * @returns 0 on success, non-zero OS status code on failure.
|
---|
74 | */
|
---|
75 | int kldrInit(void)
|
---|
76 | {
|
---|
77 | int rc;
|
---|
78 |
|
---|
79 | /* check we're already good. */
|
---|
80 | if (g_fInitialized == 1)
|
---|
81 | return 0;
|
---|
82 |
|
---|
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)
|
---|
92 | {
|
---|
93 | g_fInitialized = -1;
|
---|
94 | break;
|
---|
95 | }
|
---|
96 | kldrHlpSleep(1);
|
---|
97 | }
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * Do the initialization.
|
---|
101 | */
|
---|
102 | rc = kldrHlpHeapInit();
|
---|
103 | if (!rc)
|
---|
104 | {
|
---|
105 | rc = kldrHlpSemInit();
|
---|
106 | if (!rc)
|
---|
107 | {
|
---|
108 | g_fInitialized = 1;
|
---|
109 | return 0;
|
---|
110 | }
|
---|
111 | kldrHlpHeapTerm();
|
---|
112 | }
|
---|
113 | g_fInitialized = -2;
|
---|
114 | return rc;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Terminates the loader.
|
---|
120 | */
|
---|
121 | void kldrTerm(void)
|
---|
122 | {
|
---|
123 | /* can't terminate unless it's initialized. */
|
---|
124 | if (g_fInitialized != 1)
|
---|
125 | return;
|
---|
126 | g_fInitialized = -1;
|
---|
127 |
|
---|
128 | /*
|
---|
129 | * Do the termination.
|
---|
130 | */
|
---|
131 | kldrHlpSemTerm();
|
---|
132 | kldrHlpHeapTerm();
|
---|
133 |
|
---|
134 | /* done */
|
---|
135 | g_fInitialized = 0;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Compares arch+cpu some code was generated for with a arch+cpu for executing it
|
---|
141 | * to see if it'll work out fine or not.
|
---|
142 | *
|
---|
143 | * @returns 0 if the code is compatible with the cpu.
|
---|
144 | * @returns KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE if the arch+cpu isn't compatible with the code.
|
---|
145 | * @param enmCodeArch The architecture the code was generated for.
|
---|
146 | * @param enmCodeCpu The cpu the code was generated for.
|
---|
147 | * @param enmArch The architecture to run it on.
|
---|
148 | * @param enmCpu The cpu to run it on.
|
---|
149 | */
|
---|
150 | int kLdrCompareCpus(KLDRARCH enmCodeArch, KLDRCPU enmCodeCpu, KLDRARCH enmArch, KLDRCPU enmCpu)
|
---|
151 | {
|
---|
152 | /*
|
---|
153 | * Compare arch and cpu.
|
---|
154 | */
|
---|
155 | if (enmCodeArch != enmArch)
|
---|
156 | return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
157 |
|
---|
158 | /* exact match is nice. */
|
---|
159 | if (enmCodeCpu == enmCpu)
|
---|
160 | return 0;
|
---|
161 | switch (enmArch)
|
---|
162 | {
|
---|
163 | case KLDRARCH_X86_16:
|
---|
164 | if (enmCpu < KLDRCPU_FIRST_X86_16 || enmCpu > KLDRCPU_LAST_X86_16)
|
---|
165 | return KLDR_ERR_INVALID_PARAMETER;
|
---|
166 |
|
---|
167 | /* intel? */
|
---|
168 | if (enmCodeCpu <= KLDRCPU_CORE2_16)
|
---|
169 | {
|
---|
170 | /* also intel? */
|
---|
171 | if (enmCpu <= KLDRCPU_CORE2_16)
|
---|
172 | return enmCodeCpu <= enmCpu ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
173 | switch (enmCpu)
|
---|
174 | {
|
---|
175 | case KLDRCPU_K6_16:
|
---|
176 | return enmCodeCpu <= KLDRCPU_I586 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
177 | case KLDRCPU_K7_16:
|
---|
178 | case KLDRCPU_K8_16:
|
---|
179 | default:
|
---|
180 | return enmCodeCpu <= KLDRCPU_I686 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
181 | }
|
---|
182 | }
|
---|
183 | /* amd */
|
---|
184 | return enmCpu >= KLDRCPU_K6_16 && enmCpu <= KLDRCPU_K8_16
|
---|
185 | ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
186 |
|
---|
187 | case KLDRARCH_X86_32:
|
---|
188 | if (enmCpu < KLDRCPU_FIRST_X86_32 || enmCpu > KLDRCPU_LAST_X86_32)
|
---|
189 | return KLDR_ERR_INVALID_PARAMETER;
|
---|
190 |
|
---|
191 | /* blend? */
|
---|
192 | if (enmCodeCpu == KLDRCPU_X86_32_BLEND)
|
---|
193 | return 0;
|
---|
194 |
|
---|
195 | /* intel? */
|
---|
196 | if (enmCodeCpu <= KLDRCPU_CORE2_32)
|
---|
197 | {
|
---|
198 | /* also intel? */
|
---|
199 | if (enmCpu <= KLDRCPU_CORE2_32)
|
---|
200 | return enmCodeCpu <= enmCpu ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
201 | switch (enmCpu)
|
---|
202 | {
|
---|
203 | case KLDRCPU_K6:
|
---|
204 | return enmCodeCpu <= KLDRCPU_I586 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
205 | case KLDRCPU_K7:
|
---|
206 | case KLDRCPU_K8_32:
|
---|
207 | default:
|
---|
208 | return enmCodeCpu <= KLDRCPU_I686 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | /* amd */
|
---|
212 | return enmCpu >= KLDRCPU_K6 && enmCpu <= KLDRCPU_K8_32
|
---|
213 | ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
214 |
|
---|
215 | case KLDRARCH_AMD64:
|
---|
216 | if (enmCpu < KLDRCPU_FIRST_AMD64 || enmCpu > KLDRCPU_LAST_AMD64)
|
---|
217 | return KLDR_ERR_INVALID_PARAMETER;
|
---|
218 |
|
---|
219 | /* blend? */
|
---|
220 | if (enmCodeCpu == KLDRCPU_AMD64_BLEND)
|
---|
221 | return 0;
|
---|
222 | /* this is simple for now. */
|
---|
223 | return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
224 |
|
---|
225 | default:
|
---|
226 | break;
|
---|
227 | }
|
---|
228 | return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Gets the arch+cpu of the calling cpu.
|
---|
234 | *
|
---|
235 | * @param penmArch Where to store the cpu architecture.
|
---|
236 | * @param penmCpu Where to store the cpu brand/model.
|
---|
237 | */
|
---|
238 | void kLdrGetArchCpu(PKLDRARCH penmArch, PKLDRCPU penmCpu)
|
---|
239 | {
|
---|
240 | #if defined(__x86_64__) || defined(_M_X64) || defined(__AMD64__) || defined(_M_AMD64)
|
---|
241 | *penmArch = KLDRARCH_AMD64;
|
---|
242 | *penmCpu = KLDRCPU_AMD64_BLEND; ///@todo check it using cpu.
|
---|
243 |
|
---|
244 | #elif defined(__i386__) || defined(_M_IX86)
|
---|
245 | *penmArch = KLDRARCH_X86_32;
|
---|
246 | *penmCpu = KLDRCPU_X86_32_BLEND; ///@todo check it using cpu.
|
---|
247 |
|
---|
248 | #else
|
---|
249 | # error "Port me"
|
---|
250 | #endif
|
---|
251 | }
|
---|