Kernel/vfs - Fixing OpenInode support
[tpg/acess2.git] / Kernel / binary.c
1 /*
2  * Acess2
3  * Common Binary Loader
4  */
5 #define DEBUG   1
6 #include <acess.h>
7 #include <binary.h>
8 #include <mm_virt.h>
9 #include <hal_proc.h>
10
11 // === CONSTANTS ===
12 #define BIN_LOWEST      MM_USER_MIN             // 1MiB
13 #define BIN_GRANUALITY  0x10000         // 64KiB
14 #define BIN_HIGHEST     (USER_LIB_MAX-BIN_GRANUALITY)           // Just below the kernel
15 #define KLIB_LOWEST     MM_MODULE_MIN
16 #define KLIB_GRANUALITY 0x10000         // 32KiB
17 #define KLIB_HIGHEST    (MM_MODULE_MAX-KLIB_GRANUALITY)
18
19 // === TYPES ===
20 typedef struct sKernelBin {
21         struct sKernelBin       *Next;
22         void    *Base;
23         tBinary *Info;
24 } tKernelBin;
25
26 // === IMPORTS ===
27 extern char     *Threads_GetName(int ID);
28 extern tKernelSymbol    gKernelSymbols[];
29 extern tKernelSymbol    gKernelSymbolsEnd[];
30 extern tBinaryType      gELF_Info;
31
32 // === PROTOTYPES ===
33  int    Proc_Execve(const char *File, const char **ArgV, const char **EnvP);
34 tVAddr  Binary_Load(const char *Path, tVAddr *EntryPoint);
35 tBinary *Binary_GetInfo(tMount MountID, tInode InodeID);
36 tVAddr  Binary_MapIn(tBinary *Binary, const char *Path, tVAddr LoadMin, tVAddr LoadMax);
37 tVAddr  Binary_IsMapped(tBinary *Binary);
38 tBinary *Binary_DoLoad(tMount MountID, tInode Inode, const char *Path);
39 void    Binary_Dereference(tBinary *Info);
40 #if 0
41 Uint    Binary_Relocate(void *Base);
42 #endif
43 Uint    Binary_GetSymbolEx(const char *Name, Uint *Value);
44 #if 0
45 Uint    Binary_FindSymbol(void *Base, const char *Name, Uint *Val);
46 #endif
47  int    Binary_int_CheckMemFree( tVAddr _start, size_t _len );
48
49 // === GLOBALS ===
50 tShortSpinlock  glBinListLock;
51 tBinary *glLoadedBinaries = NULL;
52 char    **gsaRegInterps = NULL;
53  int    giRegInterps = 0;
54 tShortSpinlock  glKBinListLock;
55 tKernelBin      *glLoadedKernelLibs;
56 tBinaryType     *gRegBinTypes = &gELF_Info;
57  
58 // === FUNCTIONS ===
59 /**
60  * \brief Registers a binary type
61  */
62 int Binary_RegisterType(tBinaryType *Type)
63 {
64         Type->Next = gRegBinTypes;
65         gRegBinTypes = Type;
66         return 1;
67 }
68
69 /**
70  * \fn int Proc_Spawn(const char *Path)
71  */
72 int Proc_Spawn(const char *Path)
73 {
74         char    stackPath[strlen(Path)+1];
75         ENTER("sPath", Path);
76         
77         strcpy(stackPath, Path);
78         
79         LOG("stackPath = '%s'\n", stackPath);
80         
81         if(Proc_Clone(CLONE_VM) == 0)
82         {
83                 // CHILD
84                 const char      *args[2] = {stackPath, NULL};
85                 LOG("stackPath = '%s'\n", stackPath);
86                 Proc_Execve(stackPath, args, &args[1]);
87                 for(;;);
88         }
89         LEAVE('i', 0);
90         return 0;
91 }
92
93 /**
94  * \fn int Proc_Execve(char *File, char **ArgV, char **EnvP)
95  * \brief Replace the current user image with another
96  * \param File  File to load as the next image
97  * \param ArgV  Arguments to pass to user
98  * \param EnvP  User's environment
99  * \note Called Proc_ for historical reasons
100  */
101 int Proc_Execve(const char *File, const char **ArgV, const char **EnvP)
102 {
103          int    argc, envc, i;
104          int    argenvBytes;
105         char    **argenvBuf, *strBuf;
106         char    **argvSaved, **envpSaved;
107         char    *savedFile;
108         tVAddr  entry;
109         Uint    bases[2] = {0}; // Uint because Proc_StartUser wants it
110         
111         ENTER("sFile pArgV pEnvP", File, ArgV, EnvP);
112         
113         // --- Save File, ArgV and EnvP (also get argc)
114         
115         // Count Arguments, Environment Variables and total string sizes
116         argenvBytes = 0;
117         for( argc = 0; ArgV && ArgV[argc]; argc++ )
118                 argenvBytes += strlen(ArgV[argc])+1;
119         for( envc = 0; EnvP && EnvP[envc]; envc++ )
120                 argenvBytes += strlen(EnvP[envc])+1;
121         argenvBytes = (argenvBytes + sizeof(void*)-1) & ~(sizeof(void*)-1);
122         argenvBytes += (argc+1)*sizeof(void*) + (envc+1)*sizeof(void*);
123         
124         // Allocate
125         argenvBuf = malloc(argenvBytes);
126         if(argenvBuf == NULL) {
127                 Log_Error("Binary", "Proc_Execve - What the hell? The kernel is out of heap space");
128                 LEAVE('i', 0);
129                 return 0;
130         }
131         strBuf = (char*)argenvBuf + (argc+1)*sizeof(void*) + (envc+1)*sizeof(void*);
132         
133         // Populate
134         argvSaved = argenvBuf;
135         for( i = 0; i < argc; i++ )
136         {
137                 argvSaved[i] = strBuf;
138                 strcpy(argvSaved[i], ArgV[i]);
139                 strBuf += strlen(ArgV[i])+1;
140         }
141         argvSaved[i] = NULL;
142         envpSaved = &argvSaved[i+1];
143         for( i = 0; i < envc; i++ )
144         {
145                 envpSaved[i] = strBuf;
146                 strcpy(envpSaved[i], EnvP[i]);
147                 strBuf += strlen(EnvP[i])+1;
148         }
149         envpSaved[i] = NULL;
150         
151         savedFile = malloc(strlen(File)+1);
152         strcpy(savedFile, File);
153         
154         // --- Set Process Name
155         Threads_SetName(File);
156         
157         // --- Clear User Address space
158         MM_ClearUser();
159         
160         // --- Load new binary
161         bases[0] = Binary_Load(savedFile, &entry);
162         free(savedFile);
163         if(bases[0] == 0)
164         {
165                 Log_Warning("Binary", "Proc_Execve - Unable to load '%s'", Threads_GetName(-1));
166                 LEAVE('-');
167                 Threads_Exit(0, -10);
168                 for(;;);
169         }
170         
171         LOG("entry = 0x%x, bases[0] = 0x%x", entry, bases[0]);
172         LEAVE('-');
173         // --- And... Jump to it
174         Proc_StartUser(entry, bases, argc, argvSaved, envpSaved, argenvBytes);
175         for(;;);        // Tell GCC that we never return
176 }
177
178 /**
179  * \fn tVAddr Binary_Load(char *Path, tVAddr *EntryPoint)
180  * \brief Load a binary into the current address space
181  * \param Path  Path to binary to load
182  * \param EntryPoint    Pointer for exectuable entry point
183  */
184 tVAddr Binary_Load(const char *Path, tVAddr *EntryPoint)
185 {
186         tMount  mount_id;
187         tInode  inode;
188         tBinary *pBinary;
189         tVAddr  base = -1;
190
191         ENTER("sPath pEntryPoint", Path, EntryPoint);
192         
193         // Sanity Check Argument
194         if(Path == NULL) {
195                 LEAVE('x', 0);
196                 return 0;
197         }
198
199         // Check if this path has been loaded before.
200         #if 0
201         // TODO: Implement a list of string/tBinary pairs for loaded bins
202         #endif
203
204         // Get Inode
205         {
206                 int fd;
207                 tFInfo  info;
208                 fd = VFS_Open(Path, VFS_OPENFLAG_READ|VFS_OPENFLAG_EXEC);
209                 if( fd == -1 ) {
210                         LOG("%s does not exist", Path);
211                         LEAVE_RET('x', 0);
212                 }
213                 VFS_FInfo(fd, &info, 0);
214                 VFS_Close(fd);
215                 mount_id = info.mount;
216                 inode = info.inode;
217         }
218
219         // TODO: Also get modifcation time?
220
221         // Check if the binary has already been loaded
222         if( !(pBinary = Binary_GetInfo(mount_id, inode)) )
223                 pBinary = Binary_DoLoad(mount_id, inode, Path); // Else load it
224         
225         // Error Check
226         if(pBinary == NULL) {
227                 LEAVE('x', 0);
228                 return 0;
229         }
230         
231         // Map into process space
232         base = Binary_MapIn(pBinary, Path, BIN_LOWEST, BIN_HIGHEST);
233         
234         // Check for errors
235         if(base == 0) {
236                 LEAVE('x', 0);
237                 return 0;
238         }
239         
240         // Interpret
241         if(pBinary->Interpreter) {
242                 tVAddr  start;
243                 if( Binary_Load(pBinary->Interpreter, &start) == 0 ) {
244                         LEAVE('x', 0);
245                         return 0;
246                 }
247                 *EntryPoint = start;
248         }
249         else
250                 *EntryPoint = pBinary->Entry - pBinary->Base + base;
251         
252         // Return
253         LOG("*EntryPoint = 0x%x", *EntryPoint);
254         LEAVE('x', base);
255         return base;    // Pass the base as an argument to the user if there is an interpreter
256 }
257
258 /**
259  * \brief Finds a matching binary entry
260  * \param TruePath      File Identifier (True path name)
261  */
262 tBinary *Binary_GetInfo(tMount MountID, tInode InodeID)
263 {
264         tBinary *pBinary;
265         pBinary = glLoadedBinaries;
266         while(pBinary)
267         {
268                 if(pBinary->MountID == MountID && pBinary->Inode == InodeID)
269                         return pBinary;
270                 pBinary = pBinary->Next;
271         }
272         return NULL;
273 }
274
275 /**
276  \fn Uint Binary_MapIn(tBinary *binary)
277  \brief Maps an already-loaded binary into an address space.
278  \param binary  Pointer to globally stored data.
279 */
280 tVAddr Binary_MapIn(tBinary *Binary, const char *Path, tVAddr LoadMin, tVAddr LoadMax)
281 {
282         tVAddr  base;
283          int    i, fd;
284         // Reference Executable (Makes sure that it isn't unloaded)
285         Binary->ReferenceCount ++;
286         
287         // Get Binary Base
288         base = Binary->Base;
289         
290         // Check if base is free
291         if(base != 0)
292         {
293                 for(i=0;i<Binary->NumSections;i++)
294                 {
295                         if( Binary_int_CheckMemFree( Binary->LoadSections[i].Virtual, Binary->LoadSections[i].MemSize ) )
296                         {
297                                 base = 0;
298                                 LOG("Address 0x%x is taken\n", Binary->LoadSections[i].Virtual);
299                                 break;
300                         }
301                 }
302         }
303         
304         // Check if the executable has no base or it is not free
305         if(base == 0)
306         {
307                 // If so, give it a base
308                 base = LoadMax;
309                 while(base >= LoadMin)
310                 {
311                         for( i = 0; i < Binary->NumSections; i ++ )
312                         {
313                                 tVAddr  addr = Binary->LoadSections[i].Virtual - Binary->Base + base;
314                                 if( Binary_int_CheckMemFree( addr, Binary->LoadSections[i].MemSize ) )
315                                         break;
316                         }
317                         // If space was found, break
318                         if(i == Binary->NumSections)            break;
319                         // Else decrement pointer and try again
320                         base -= BIN_GRANUALITY;
321                 }
322         }
323         
324         // Error Check
325         if(base < LoadMin) {
326                 Log_Warning("Binary", "Executable '%s' cannot be loaded, no space", Path);
327                 return 0;
328         }
329         
330         // Map Executable In
331         fd = VFS_OpenInode(Binary->MountID, Binary->Inode, VFS_OPENFLAG_READ);
332         for( i = 0; i < Binary->NumSections; i ++ )
333         {
334                 tBinarySection  *sect = &Binary->LoadSections[i];
335                 Uint    protflags, mapflags;
336                 tVAddr  addr = sect->Virtual - Binary->Base + base;
337                 LOG("%i - 0x%x to 0x%x", i, addr, sect->Offset);
338
339                 protflags = MMAP_PROT_READ;
340                 mapflags = MMAP_MAP_FIXED;
341
342                 if( sect->Flags & BIN_SECTFLAG_EXEC )
343                         protflags |= MMAP_PROT_EXEC;
344                 if( sect->Flags & BIN_SECTFLAG_RO  ) {
345                         VFS_MMap( (void*)addr, sect->FileSize, protflags, MMAP_MAP_SHARED|mapflags, fd, sect->Offset );
346                 }
347                 else {
348                         protflags |= MMAP_PROT_WRITE;
349                         VFS_MMap( (void*)addr, sect->FileSize, protflags, MMAP_MAP_PRIVATE|mapflags, fd, sect->Offset );
350                 }
351                 if( sect->FileSize < sect->MemSize ) {
352                         mapflags |= MMAP_MAP_ANONYMOUS;
353                         VFS_MMap(
354                                 (void*)(addr + sect->FileSize), sect->MemSize - sect->FileSize,
355                                 protflags, MMAP_MAP_PRIVATE|mapflags,
356                                 0, 0
357                                 );
358                 }
359         }
360         
361         Log_Debug("Binary", "PID %i - Mapped '%s' to 0x%x", Threads_GetPID(), Path, base);
362         
363         //LOG("*0x%x = 0x%x\n", binary->Pages[0].Virtual, *(Uint*)binary->Pages[0].Virtual);
364         
365         return base;
366 }
367
368 #if 0
369 /**
370  * \fn Uint Binary_IsMapped(tBinary *binary)
371  * \brief Check if a binary is already mapped into the address space
372  * \param binary        Binary information to check
373  * \return Current Base or 0
374  */
375 Uint Binary_IsMapped(tBinary *binary)
376 {
377         Uint    iBase;
378         
379         // Check prefered base
380         iBase = binary->Base;
381         if(MM_GetPage( iBase ) == (binary->Pages[0].Physical & ~0xFFF))
382                 return iBase;
383         
384         for(iBase = BIN_HIGHEST;
385                 iBase >= BIN_LOWEST;
386                 iBase -= BIN_GRANUALITY)
387         {
388                 if(MM_GetPage( iBase ) == (binary->Pages[0].Physical & ~0xFFF))
389                         return iBase;
390         }
391         
392         return 0;
393 }
394 #endif
395
396 /**
397  * \fn tBinary *Binary_DoLoad(char *truePath)
398  * \brief Loads a binary file into memory
399  * \param truePath      Absolute filename of binary
400  */
401 tBinary *Binary_DoLoad(tMount MountID, tInode Inode, const char *Path)
402 {
403         tBinary *pBinary;
404          int    fp;
405         Uint    ident;
406         tBinaryType     *bt = gRegBinTypes;
407         
408         ENTER("iMountID XInode sPath", Path);
409         
410         // Open File
411         fp = VFS_OpenInode(MountID, Inode, VFS_OPENFLAG_READ);
412         if(fp == -1) {
413                 LOG("Unable to load file, access denied");
414                 LEAVE('n');
415                 return NULL;
416         }
417         
418         // Read File Type
419         VFS_Read(fp, 4, &ident);
420         VFS_Seek(fp, 0, SEEK_SET);
421
422         // Determine the type   
423         for(; bt; bt = bt->Next)
424         {
425                 if( (ident & bt->Mask) != (Uint)bt->Ident )
426                         continue;
427                 pBinary = bt->Load(fp);
428                 break;
429         }
430         
431         // Close File
432         VFS_Close(fp);
433         
434         // Catch errors
435         if(!bt) {
436                 Log_Warning("Binary", "'%s' is an unknown file type. (%02x %02x %02x %02x)",
437                         Path, ident&0xFF, (ident>>8)&0xFF, (ident>>16)&0xFF, (ident>>24)&0xFF);
438                 LEAVE('n');
439                 return NULL;
440         }
441         
442         // Error Check
443         if(pBinary == NULL) {
444                 LEAVE('n');
445                 return NULL;
446         }
447         
448         // Initialise Structure
449         pBinary->ReferenceCount = 0;
450         pBinary->MountID = MountID;
451         pBinary->Inode = Inode;
452         
453         // Debug Information
454         LOG("Interpreter: '%s'", pBinary->Interpreter);
455         LOG("Base: 0x%x, Entry: 0x%x", pBinary->Base, pBinary->Entry);
456         LOG("NumSections: %i", pBinary->NumSections);
457         
458         // Add to the list
459         SHORTLOCK(&glBinListLock);
460         pBinary->Next = glLoadedBinaries;
461         glLoadedBinaries = pBinary;
462         SHORTREL(&glBinListLock);
463
464         // TODO: Register the path with the binary      
465
466         // Return
467         LEAVE('p', pBinary);
468         return pBinary;
469 }
470
471 /**
472  * \fn void Binary_Unload(void *Base)
473  * \brief Unload / Unmap a binary
474  * \param Base  Loaded Base
475  * \note Currently used only for kernel libaries
476  */
477 void Binary_Unload(void *Base)
478 {
479         tKernelBin      *pKBin;
480         tKernelBin      *prev = NULL;
481          int    i;
482         
483         if((Uint)Base < 0xC0000000)
484         {
485                 // TODO: User Binaries
486                 Log_Warning("BIN", "Unloading user binaries is currently unimplemented");
487                 return;
488         }
489         
490         // Kernel Libraries
491         for(pKBin = glLoadedKernelLibs;
492                 pKBin;
493                 prev = pKBin, pKBin = pKBin->Next)
494         {
495                 // Check the base
496                 if(pKBin->Base != Base) continue;
497                 // Deallocate Memory
498                 for(i = 0; i < pKBin->Info->NumSections; i++)
499                 {
500                         // TODO: VFS_MUnmap();
501                 }
502                 // Dereference Binary
503                 Binary_Dereference( pKBin->Info );
504                 // Remove from list
505                 if(prev)        prev->Next = pKBin->Next;
506                 else            glLoadedKernelLibs = pKBin->Next;
507                 // Free Kernel Lib
508                 free(pKBin);
509                 return;
510         }
511 }
512
513 /**
514  * \fn void Binary_Dereference(tBinary *Info)
515  * \brief Dereferences and if nessasary, deletes a binary
516  * \param Info  Binary information structure
517  */
518 void Binary_Dereference(tBinary *Info)
519 {
520         // Decrement reference count
521         Info->ReferenceCount --;
522         
523         // Check if it is still in use
524         if(Info->ReferenceCount)        return;
525         
526         /// \todo Implement binary freeing
527 }
528
529 /**
530  * \fn char *Binary_RegInterp(char *Path)
531  * \brief Registers an Interpreter
532  * \param Path  Path to interpreter provided by executable
533  */
534 char *Binary_RegInterp(char *Path)
535 {
536          int    i;
537         // NULL Check Argument
538         if(Path == NULL)        return NULL;
539         // NULL Check the array
540         if(gsaRegInterps == NULL)
541         {
542                 giRegInterps = 1;
543                 gsaRegInterps = malloc( sizeof(char*) );
544                 gsaRegInterps[0] = malloc( strlen(Path) );
545                 strcpy(gsaRegInterps[0], Path);
546                 return gsaRegInterps[0];
547         }
548         
549         // Scan Array
550         for( i = 0; i < giRegInterps; i++ )
551         {
552                 if(strcmp(gsaRegInterps[i], Path) == 0)
553                         return gsaRegInterps[i];
554         }
555         
556         // Interpreter is not in list
557         giRegInterps ++;
558         gsaRegInterps = malloc( sizeof(char*)*giRegInterps );
559         gsaRegInterps[i] = malloc( strlen(Path) );
560         strcpy(gsaRegInterps[i], Path);
561         return gsaRegInterps[i];
562 }
563
564 // ============
565 // Kernel Binary Handling
566 // ============
567 /**
568  * \fn void *Binary_LoadKernel(const char *File)
569  * \brief Load a binary into kernel space
570  * \note This function shares much with #Binary_Load, but does it's own mapping
571  * \param File  File to load into the kernel
572  */
573 void *Binary_LoadKernel(const char *File)
574 {
575         tBinary *pBinary;
576         tKernelBin      *pKBinary;
577         tVAddr  base = -1;
578         tMount  mount_id;
579         tInode  inode;
580
581         ENTER("sFile", File);
582         
583         // Sanity Check Argument
584         if(File == NULL) {
585                 LEAVE('n');
586                 return 0;
587         }
588
589         {
590                 int fd = VFS_Open(File, VFS_OPENFLAG_READ);
591                 tFInfo  info;
592                 if(fd == -1) {
593                         LEAVE('n');
594                         return NULL;
595                 }
596                 VFS_FInfo(fd, &info, 0);
597                 mount_id = info.mount;
598                 inode = info.inode;
599                 VFS_Close(fd);
600         }
601         
602         // Check if the binary has already been loaded
603         if( (pBinary = Binary_GetInfo(mount_id, inode)) )
604         {
605                 for(pKBinary = glLoadedKernelLibs;
606                         pKBinary;
607                         pKBinary = pKBinary->Next )
608                 {
609                         if(pKBinary->Info == pBinary) {
610                                 LEAVE('p', pKBinary->Base);
611                                 return pKBinary->Base;
612                         }
613                 }
614         }
615         else
616                 pBinary = Binary_DoLoad(mount_id, inode, File); // Else load it
617         
618         // Error Check
619         if(pBinary == NULL) {
620                 LEAVE('n');
621                 return NULL;
622         }
623         
624         // --------------
625         // Now pBinary is valid (either freshly loaded or only user mapped)
626         // So, map it into kernel space
627         // --------------
628         
629         // Reference Executable (Makes sure that it isn't unloaded)
630         pBinary->ReferenceCount ++;
631
632         Binary_MapIn(pBinary, File, KLIB_LOWEST, KLIB_HIGHEST);
633
634         // Relocate Library
635         if( !Binary_Relocate( (void*)base ) )
636         {
637                 Log_Warning("Binary", "Relocation of '%s' failed, unloading", File);
638                 Binary_Unload( (void*)base );
639                 Binary_Dereference( pBinary );
640                 LEAVE('n');
641                 return 0;
642         }
643         
644         // Add to list (relocator must look at itself manually, not via Binary_GetSymbol)
645         pKBinary = malloc(sizeof(*pKBinary));
646         pKBinary->Base = (void*)base;
647         pKBinary->Info = pBinary;
648         SHORTLOCK( &glKBinListLock );
649         pKBinary->Next = glLoadedKernelLibs;
650         glLoadedKernelLibs = pKBinary;
651         SHORTREL( &glKBinListLock );
652         
653         LEAVE('p', base);
654         return (void*)base;
655 }
656
657 /**
658  * \fn Uint Binary_Relocate(void *Base)
659  * \brief Relocates a loaded binary (used by kernel libraries)
660  * \param Base  Loaded base address of binary
661  * \return Boolean Success
662  */
663 Uint Binary_Relocate(void *Base)
664 {
665         Uint32  ident = *(Uint32*) Base;
666         tBinaryType     *bt = gRegBinTypes;
667         
668         for(; bt; bt = bt->Next)
669         {
670                 if( (ident & bt->Mask) == (Uint)bt->Ident )
671                         return bt->Relocate( (void*)Base);
672         }
673         
674         Log_Warning("BIN", "%p is an unknown file type. (%02x %02x %02x %02x)",
675                 Base, ident&0xFF, (ident>>8)&0xFF, (ident>>16)&0xFF, (ident>>24)&0xFF);
676         return 0;
677 }
678
679 /**
680  * \fn int Binary_GetSymbol(char *Name, Uint *Val)
681  * \brief Get a symbol value
682  * \return Value of symbol or -1 on error
683  * 
684  * Gets the value of a symbol from either the currently loaded
685  * libraries or the kernel's exports.
686  */
687 int Binary_GetSymbol(const char *Name, Uint *Val)
688 {
689         if( Binary_GetSymbolEx(Name, Val) )     return 1;
690         return 0;
691 }
692
693 /**
694  * \fn Uint Binary_GetSymbolEx(char *Name, Uint *Value)
695  * \brief Get a symbol value
696  * 
697  * Gets the value of a symbol from either the currently loaded
698  * libraries or the kernel's exports.
699  */
700 Uint Binary_GetSymbolEx(const char *Name, Uint *Value)
701 {
702          int    i;
703         tKernelBin      *pKBin;
704          int    numKSyms = ((Uint)&gKernelSymbolsEnd-(Uint)&gKernelSymbols)/sizeof(tKernelSymbol);
705         
706         // Scan Kernel
707         for( i = 0; i < numKSyms; i++ )
708         {
709                 if(strcmp(Name, gKernelSymbols[i].Name) == 0) {
710                         *Value = gKernelSymbols[i].Value;
711                         return 1;
712                 }
713         }
714         
715         // Scan Loaded Libraries
716         for(pKBin = glLoadedKernelLibs;
717                 pKBin;
718                 pKBin = pKBin->Next )
719         {
720                 if( Binary_FindSymbol(pKBin->Base, Name, Value) ) {
721                         return 1;
722                 }
723         }
724         
725         Log_Warning("BIN", "Unable to find symbol '%s'", Name);
726         return 0;
727 }
728
729 /**
730  * \fn Uint Binary_FindSymbol(void *Base, char *Name, Uint *Val)
731  * \brief Get a symbol from the specified library
732  * \param Base  Base address
733  * \param Name  Name of symbol to find
734  * \param Val   Pointer to place final value
735  */
736 Uint Binary_FindSymbol(void *Base, const char *Name, Uint *Val)
737 {
738         Uint32  ident = *(Uint32*) Base;
739         tBinaryType     *bt = gRegBinTypes;
740         
741         for(; bt; bt = bt->Next)
742         {
743                 if( (ident & bt->Mask) == (Uint)bt->Ident )
744                         return bt->GetSymbol(Base, Name, Val);
745         }
746         
747         Log_Warning("BIN", "Binary_FindSymbol - %p is an unknown file type. (%02x %02x %02x %02x)",
748                 Base, ident&0xFF, ident>>8, ident>>16, ident>>24);
749         return 0;
750 }
751
752 /**
753  * \brief Check if a range of memory is fully free
754  * \return Inverse boolean free (0 if all pages are unmapped)
755  */
756 int Binary_int_CheckMemFree( tVAddr _start, size_t _len )
757 {
758         _len += _start & (PAGE_SIZE-1);
759         _start &= ~(PAGE_SIZE-1);
760         for( ; _len > PAGE_SIZE; _len -= PAGE_SIZE, _start += PAGE_SIZE ) {
761                 if( MM_GetPhysAddr(_start) != 0 )
762                         return 1;
763         }
764         if( _len == PAGE_SIZE && MM_GetPhysAddr(_start) != 0 )
765                 return 1;
766         return 0;
767 }
768
769
770 // === EXPORTS ===
771 EXPORT(Binary_FindSymbol);
772 EXPORT(Binary_Unload);

UCC git Repository :: git.ucc.asn.au