The Pedigree Project  0.1
KernelElf.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef KERNEL_LINKER_KERNELELF_H
21 #define KERNEL_LINKER_KERNELELF_H
22 
23 #include "pedigree/kernel/compiler.h"
24 #include "pedigree/kernel/linker/Elf.h"
25 #include "pedigree/kernel/processor/MemoryRegion.h"
26 #include "pedigree/kernel/processor/types.h"
27 #include "pedigree/kernel/utilities/MemoryAllocator.h"
28 #include "pedigree/kernel/utilities/SharedPointer.h"
29 #include "pedigree/kernel/utilities/Vector.h"
30 #include "pedigree/kernel/utilities/utility.h"
31 
32 #ifdef THREADS
33 #include "pedigree/kernel/Spinlock.h"
34 #include "pedigree/kernel/process/Semaphore.h"
35 #endif
36 
37 #ifdef STATIC_DRIVERS
38 #include "modules/Module.h"
39 #endif
40 
41 class BootstrapStruct_t;
42 class String;
43 
47 class Module
48 {
49  public:
50  Module()
51  : elf(nullptr), name(0), entry(0), exit(0), depends(0), depends_opt(0),
52  buffer(0), buflen(0), status(Unknown)
53  {
54  }
55 
56  ~Module()
57  {
58  delete elf;
59  }
60 
61  Elf *elf;
62  String name;
63  bool (*entry)();
64  void (*exit)();
65  const char **depends;
66  const char **depends_opt;
67  uint8_t *buffer;
68  size_t buflen;
69  uintptr_t loadBase;
70  size_t loadSize;
71 
72  enum ModuleStatus
73  {
74  Unknown,
75  Preloaded,
76  Executing,
77  Active,
78  Failed,
79  Unloaded
80  } status;
81 
82  bool isPending() const
83  {
84  return status == Preloaded;
85  }
86 
87  bool isLoaded() const
88  {
89  return status == Preloaded || status == Active;
90  }
91 
92  bool isUnloaded() const
93  {
94  return status == Unloaded || status == Failed;
95  }
96 
97  bool isFailed() const
98  {
99  return status == Failed;
100  }
101 
102  bool isActive() const
103  {
104  return status == Active;
105  }
106 
107  bool isExecuting() const
108  {
109  return status == Executing;
110  }
111 
112  bool wasAttempted() const
113  {
114  return status == Executing || isActive() || isFailed() || isUnloaded();
115  }
116 
117  protected:
118  Module(const Module &);
119  Module &operator=(const Module &);
120 };
121 
123 {
124  friend void system_reset();
125 
126  public:
129  inline static KernelElf &instance()
130  {
131  return m_Instance;
132  }
133 
136  bool initialise(const BootstrapStruct_t &pBootstrap) INITIALISATION_ONLY;
137 
144  Module *loadModule(uint8_t *pModule, size_t len, bool silent = false);
145 #ifdef STATIC_DRIVERS
146  Module *loadModule(struct ModuleInfo *info, bool silent = false);
147 #endif
148 
150  void executeModules(bool silent = false, bool progress = true);
151 
153  void
154  unloadModule(const char *name, bool silent = false, bool progress = true);
155  void
156  unloadModule(Module *module, bool silent = false, bool progress = true);
157 
159  void unloadModules();
160 
162  bool moduleIsLoaded(char *name);
163 
166  char *getDependingModule(char *name);
167 
170  uintptr_t globalLookupSymbol(const char *pName);
171  const char *globalLookupSymbol(uintptr_t addr, uintptr_t *startAddr = 0);
172 
175  {
176  return m_ModuleAllocator;
177  }
178 
180  bool hasPendingModules() const;
181 
183  void updateModuleStatus(Module *module, bool status);
184 
186  void waitForModulesToLoad();
187 
188  private:
193  KernelElf(const KernelElf &);
195  virtual ~KernelElf();
198  KernelElf &operator=(const KernelElf &);
199 
200  bool moduleDependenciesSatisfied(Module *module);
201  bool executeModule(Module *module);
202 
204  template <class T>
205  static T *rebase(Module *module, T *ptr)
206  {
207  return adjust_pointer(ptr, module->loadBase);
208  }
209 
211  void lockModules();
212 
214  void unlockModules();
215 
216 #if defined(X86_COMMON)
217  MemoryRegion m_AdditionalSectionContents;
218  MemoryRegion *m_AdditionalSectionHeaders;
219 #endif
220 
223 
228 
230 #if defined(X86_COMMON)
231  Elf32SectionHeader_t *m_pSectionHeaders;
232  Elf32Symbol_t *m_pSymbolTable;
233 
234  typedef Elf32SectionHeader_t KernelElfSectionHeader_t;
236 #else
237  ElfSectionHeader_t *m_pSectionHeaders;
238  ElfSymbol_t *m_pSymbolTable;
239 
240  typedef ElfSectionHeader_t KernelElfSectionHeader_t;
241  typedef ElfSymbol_t KernelElfSymbol_t;
242 #endif
243 
245 #ifdef THREADS
247  Spinlock m_ModuleAdjustmentLock;
248 #endif
249 };
250 
253 #endif
Bootstrap structure passed to the kernel entry point.
Elf32SectionHeader_t * m_pSectionHeaders
Definition: KernelElf.h:231
Vector< Module * > m_Modules
Definition: KernelElf.h:225
static KernelElf m_Instance
Definition: KernelElf.h:222
MemoryAllocator m_ModuleAllocator
Definition: KernelElf.h:227
Definition: String.h:49
static KernelElf & instance()
Definition: KernelElf.h:129
static T * rebase(Module *module, T *ptr)
Definition: KernelElf.h:205
MemoryAllocator & getModuleAllocator()
Definition: KernelElf.h:174
Special memory entity in the kernel&#39;s virtual address space.
Definition: MemoryRegion.h:35
Semaphore m_ModuleProgress
Definition: KernelElf.h:246
Definition: Elf.h:201