source: trunk/kLdr/kLdr.c@ 2883

Last change on this file since 2883 was 2883, checked in by bird, 19 years ago

cleanup.

  • Property svn:keywords set to Id
File size: 7.8 KB
RevLine 
[2826]1/* $Id: kLdr.c 2883 2006-11-18 11:21:33Z 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"
[2883]55#include "kLdrInternal.h"
[2821]56
[2830]57
[2821]58/*******************************************************************************
[2853]59* Global Variables *
[2821]60*******************************************************************************/
[2830]61/** Flag indicating whether we've initialized the loader or not.
62 *
63 * 0 if not initialized.
64 * -1 if we're initializing or terminating.
65 * 1 if we've successfully initialized it.
66 * -2 if initialization failed.
67 */
68static int volatile g_fInitialized;
[2821]69
70
71
72/**
[2830]73 * Initializes the loader.
74 * @returns 0 on success, non-zero OS status code on failure.
[2821]75 */
76int kldrInit(void)
77{
[2830]78 int rc;
79
80 /* check we're already good. */
81 if (g_fInitialized == 1)
[2821]82 return 0;
83
[2830]84 /* a tiny serialization effort. */
85 for (;;)
86 {
87 if (g_fInitialized == 1)
88 return 0;
89 if (g_fInitialized == -2)
90 return -1;
91 /** @todo atomic test and set if we care. */
92 if (g_fInitialized == 0)
[2821]93 {
[2830]94 g_fInitialized = -1;
95 break;
[2821]96 }
[2830]97 kldrHlpSleep(1);
[2821]98 }
99
100 /*
[2830]101 * Do the initialization.
[2821]102 */
[2830]103 rc = kldrHlpHeapInit();
104 if (!rc)
[2821]105 {
[2830]106 rc = kldrHlpSemInit();
107 if (!rc)
108 {
[2869]109 rc = kldrDyldInit();
110 if (!rc)
111 {
112 g_fInitialized = 1;
113 return 0;
114 }
115 kldrHlpSemTerm();
[2830]116 }
117 kldrHlpHeapTerm();
[2821]118 }
[2830]119 g_fInitialized = -2;
120 return rc;
121}
[2821]122
123
[2830]124/**
125 * Terminates the loader.
126 */
127void kldrTerm(void)
128{
129 /* can't terminate unless it's initialized. */
130 if (g_fInitialized != 1)
131 return;
132 g_fInitialized = -1;
[2821]133
134 /*
[2830]135 * Do the termination.
[2821]136 */
[2830]137 kldrHlpSemTerm();
138 kldrHlpHeapTerm();
[2821]139
[2830]140 /* done */
141 g_fInitialized = 0;
[2821]142}
143
[2855]144
145/**
146 * Compares arch+cpu some code was generated for with a arch+cpu for executing it
147 * to see if it'll work out fine or not.
148 *
149 * @returns 0 if the code is compatible with the cpu.
150 * @returns KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE if the arch+cpu isn't compatible with the code.
151 * @param enmCodeArch The architecture the code was generated for.
152 * @param enmCodeCpu The cpu the code was generated for.
153 * @param enmArch The architecture to run it on.
154 * @param enmCpu The cpu to run it on.
155 */
156int kLdrCompareCpus(KLDRARCH enmCodeArch, KLDRCPU enmCodeCpu, KLDRARCH enmArch, KLDRCPU enmCpu)
157{
158 /*
159 * Compare arch and cpu.
160 */
161 if (enmCodeArch != enmArch)
162 return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
163
164 /* exact match is nice. */
165 if (enmCodeCpu == enmCpu)
166 return 0;
167 switch (enmArch)
168 {
169 case KLDRARCH_X86_16:
170 if (enmCpu < KLDRCPU_FIRST_X86_16 || enmCpu > KLDRCPU_LAST_X86_16)
171 return KLDR_ERR_INVALID_PARAMETER;
172
173 /* intel? */
174 if (enmCodeCpu <= KLDRCPU_CORE2_16)
175 {
176 /* also intel? */
177 if (enmCpu <= KLDRCPU_CORE2_16)
178 return enmCodeCpu <= enmCpu ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
179 switch (enmCpu)
180 {
181 case KLDRCPU_K6_16:
182 return enmCodeCpu <= KLDRCPU_I586 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
183 case KLDRCPU_K7_16:
184 case KLDRCPU_K8_16:
185 default:
186 return enmCodeCpu <= KLDRCPU_I686 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
187 }
188 }
189 /* amd */
190 return enmCpu >= KLDRCPU_K6_16 && enmCpu <= KLDRCPU_K8_16
191 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
192
193 case KLDRARCH_X86_32:
194 if (enmCpu < KLDRCPU_FIRST_X86_32 || enmCpu > KLDRCPU_LAST_X86_32)
195 return KLDR_ERR_INVALID_PARAMETER;
196
197 /* blend? */
198 if (enmCodeCpu == KLDRCPU_X86_32_BLEND)
199 return 0;
200
201 /* intel? */
202 if (enmCodeCpu <= KLDRCPU_CORE2_32)
203 {
204 /* also intel? */
205 if (enmCpu <= KLDRCPU_CORE2_32)
206 return enmCodeCpu <= enmCpu ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
207 switch (enmCpu)
208 {
209 case KLDRCPU_K6:
210 return enmCodeCpu <= KLDRCPU_I586 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
211 case KLDRCPU_K7:
212 case KLDRCPU_K8_32:
213 default:
214 return enmCodeCpu <= KLDRCPU_I686 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
215 }
216 }
217 /* amd */
218 return enmCpu >= KLDRCPU_K6 && enmCpu <= KLDRCPU_K8_32
219 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
220
221 case KLDRARCH_AMD64:
222 if (enmCpu < KLDRCPU_FIRST_AMD64 || enmCpu > KLDRCPU_LAST_AMD64)
223 return KLDR_ERR_INVALID_PARAMETER;
224
225 /* blend? */
226 if (enmCodeCpu == KLDRCPU_AMD64_BLEND)
227 return 0;
228 /* this is simple for now. */
229 return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
230
231 default:
232 break;
233 }
234 return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
235}
236
237
238/**
239 * Gets the arch+cpu of the calling cpu.
240 *
241 * @param penmArch Where to store the cpu architecture.
242 * @param penmCpu Where to store the cpu brand/model.
243 */
244void kLdrGetArchCpu(PKLDRARCH penmArch, PKLDRCPU penmCpu)
245{
[2883]246#ifdef __AMD64__
[2855]247 *penmArch = KLDRARCH_AMD64;
[2883]248 *penmCpu = KLDRCPU_AMD64_BLEND; /** @todo check it using cpu. */
[2855]249
[2883]250#elif defined(__X86__)
[2855]251 *penmArch = KLDRARCH_X86_32;
[2883]252 *penmCpu = KLDRCPU_X86_32_BLEND; /** @todo check it using cpu. */
[2855]253
254#else
255# error "Port me"
256#endif
257}
Note: See TracBrowser for help on using the repository browser.