Kernel/armv7 - Implemetned Hivecs, fixing bugs
[tpg/acess2.git] / Kernel / binary.c
1 /*
2  * Acess2
3  * Common Binary Loader
4  */
5 #define DEBUG   0
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'", stackPath);
80         
81         if(Proc_Clone(CLONE_VM) == 0)
82         {
83                 // CHILD
84                 const char      *args[2] = {stackPath, NULL};
85                 LOG("stackPath = '%s'", 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
173 //      MM_DumpTables(0, KERNEL_BASE);
174
175         LEAVE('-');
176         // --- And... Jump to it
177         Proc_StartUser(entry, bases, argc, argvSaved, envpSaved, argenvBytes);
178         for(;;);        // Tell GCC that we never return
179 }
180
181 /**
182  * \brief Load a binary into the current address space
183  * \param Path  Path to binary to load
184  * \param EntryPoint    Pointer for exectuable entry point
185  * \return Virtual address where the binary has been loaded
186  */
187 tVAddr Binary_Load(const char *Path, tVAddr *EntryPoint)
188 {
189         tMount  mount_id;
190         tInode  inode;
191         tBinary *pBinary;
192         tVAddr  base = -1;
193
194         ENTER("sPath pEntryPoint", Path, EntryPoint);
195         
196         // Sanity Check Argument
197         if(Path == NULL) {
198                 LEAVE('x', 0);
199                 return 0;
200         }
201
202         // Check if this path has been loaded before.
203         #if 0
204         // TODO: Implement a list of string/tBinary pairs for loaded bins
205         #endif
206
207         // Get Inode
208         {
209                 int fd;
210                 tFInfo  info;
211                 fd = VFS_Open(Path, VFS_OPENFLAG_READ|VFS_OPENFLAG_EXEC);
212                 if( fd == -1 ) {
213                         LOG("%s does not exist", Path);
214                         LEAVE_RET('x', 0);
215                 }
216                 VFS_FInfo(fd, &info, 0);
217                 VFS_Close(fd);
218                 mount_id = info.mount;
219                 inode = info.inode;
220                 LOG("mount_id = %i, inode = %i", mount_id, inode);
221         }
222
223         // TODO: Also get modifcation time?
224
225         // Check if the binary has already been loaded
226         if( !(pBinary = Binary_GetInfo(mount_id, inode)) )
227                 pBinary = Binary_DoLoad(mount_id, inode, Path); // Else load it
228         
229         // Error Check
230         if(pBinary == NULL) {
231                 LEAVE('x', 0);
232                 return 0;
233         }
234         
235         // Map into process space
236         base = Binary_MapIn(pBinary, Path, BIN_LOWEST, BIN_HIGHEST);
237         
238         // Check for errors
239         if(base == 0) {
240                 LEAVE('x', 0);
241                 return 0;
242         }
243         
244         // Interpret
245         if(pBinary->Interpreter) {
246                 tVAddr  start;
247                 if( Binary_Load(pBinary->Interpreter, &start) == 0 ) {
248                         LEAVE('x', 0);
249                         return 0;
250                 }
251                 *EntryPoint = start;
252         }
253         else
254                 *EntryPoint = pBinary->Entry - pBinary->Base + base;
255         
256         // Return
257         LOG("*EntryPoint = 0x%x", *EntryPoint);
258         LEAVE('x', base);
259         return base;    // Pass the base as an argument to the user if there is an interpreter
260 }
261
262 /**
263  * \brief Finds a matching binary entry
264  * \param TruePath      File Identifier (True path name)
265  */
266 tBinary *Binary_GetInfo(tMount MountID, tInode InodeID)
267 {
268         tBinary *pBinary;
269         pBinary = glLoadedBinaries;
270         while(pBinary)
271         {
272                 if(pBinary->MountID == MountID && pBinary->Inode == InodeID)
273                         return pBinary;
274                 pBinary = pBinary->Next;
275         }
276         return NULL;
277 }
278
279 /**
280  \fn Uint Binary_MapIn(tBinary *binary)
281  \brief Maps an already-loaded binary into an address space.
282  \param binary  Pointer to globally stored data.
283 */
284 tVAddr Binary_MapIn(tBinary *Binary, const char *Path, tVAddr LoadMin, tVAddr LoadMax)
285 {
286         tVAddr  base;
287          int    i, fd;
288
289         ENTER("pBinary sPath pLoadMin pLoadMax", Binary, Path, LoadMin, LoadMax);
290
291         // Reference Executable (Makes sure that it isn't unloaded)
292         Binary->ReferenceCount ++;
293         
294         // Get Binary Base
295         base = Binary->Base;
296         
297         // Check if base is free
298         if(base != 0)
299         {
300                 LOG("Checking base %p", base);
301                 for(i=0;i<Binary->NumSections;i++)
302                 {
303                         if( Binary_int_CheckMemFree( Binary->LoadSections[i].Virtual, Binary->LoadSections[i].MemSize ) )
304                         {
305                                 base = 0;
306                                 LOG("Address 0x%x is taken\n", Binary->LoadSections[i].Virtual);
307                                 break;
308                         }
309                 }
310         }
311         
312         // Check if the executable has no base or it is not free
313         if(base == 0)
314         {
315                 // If so, give it a base
316                 base = LoadMax;
317                 while(base >= LoadMin)
318                 {
319                         for( i = 0; i < Binary->NumSections; i ++ )
320                         {
321                                 tVAddr  addr = Binary->LoadSections[i].Virtual - Binary->Base + base;
322                                 if( Binary_int_CheckMemFree( addr, Binary->LoadSections[i].MemSize ) )
323                                         break;
324                         }
325                         // If space was found, break
326                         if(i == Binary->NumSections)            break;
327                         // Else decrement pointer and try again
328                         base -= BIN_GRANUALITY;
329                 }
330                 LOG("Allocated base %p", base);
331         }
332         
333         // Error Check
334         if(base < LoadMin) {
335                 Log_Warning("Binary", "Executable '%s' cannot be loaded, no space", Path);
336                 LEAVE('i', 0);
337                 return 0;
338         }
339         
340         // Map Executable In
341         fd = VFS_OpenInode(Binary->MountID, Binary->Inode, VFS_OPENFLAG_READ);
342         for( i = 0; i < Binary->NumSections; i ++ )
343         {
344                 tBinarySection  *sect = &Binary->LoadSections[i];
345                 Uint    protflags, mapflags;
346                 tVAddr  addr = sect->Virtual - Binary->Base + base;
347                 LOG("%i - %p to 0x%llx (%x)", i, addr, sect->Offset, sect->Flags);
348
349                 protflags = MMAP_PROT_READ;
350                 mapflags = MMAP_MAP_FIXED;
351
352                 if( sect->Flags & BIN_SECTFLAG_EXEC )
353                         protflags |= MMAP_PROT_EXEC;
354                 if( sect->Flags & BIN_SECTFLAG_RO  ) {
355                         VFS_MMap( (void*)addr, sect->FileSize, protflags, MMAP_MAP_SHARED|mapflags, fd, sect->Offset );
356                 }
357                 else {
358                         protflags |= MMAP_PROT_WRITE;
359                         VFS_MMap( (void*)addr, sect->FileSize, protflags, MMAP_MAP_PRIVATE|mapflags, fd, sect->Offset );
360                 }
361                 if( sect->FileSize < sect->MemSize ) {
362                         mapflags |= MMAP_MAP_ANONYMOUS;
363                         VFS_MMap(
364                                 (void*)(addr + sect->FileSize), sect->MemSize - sect->FileSize,
365                                 protflags, MMAP_MAP_PRIVATE|mapflags,
366                                 0, 0
367                                 );
368 //                      memset((void*)(addr + sect->FileSize), 0, sect->MemSize - sect->FileSize);
369                 }
370         }
371         
372         Log_Debug("Binary", "PID %i - Mapped '%s' to 0x%x", Threads_GetPID(), Path, base);
373         VFS_Close(fd);
374         
375         //LOG("*0x%x = 0x%x\n", binary->Pages[0].Virtual, *(Uint*)binary->Pages[0].Virtual);
376         
377         LEAVE('p', base);
378         return base;
379 }
380
381 #if 0
382 /**
383  * \fn Uint Binary_IsMapped(tBinary *binary)
384  * \brief Check if a binary is already mapped into the address space
385  * \param binary        Binary information to check
386  * \return Current Base or 0
387  */
388 Uint Binary_IsMapped(tBinary *binary)
389 {
390         Uint    iBase;
391         
392         // Check prefered base
393         iBase = binary->Base;
394         if(MM_GetPage( iBase ) == (binary->Pages[0].Physical & ~0xFFF))
395                 return iBase;
396         
397         for(iBase = BIN_HIGHEST;
398                 iBase >= BIN_LOWEST;
399                 iBase -= BIN_GRANUALITY)
400         {
401                 if(MM_GetPage( iBase ) == (binary->Pages[0].Physical & ~0xFFF))
402                         return iBase;
403         }
404         
405         return 0;
406 }
407 #endif
408
409 /**
410  * \fn tBinary *Binary_DoLoad(char *truePath)
411  * \brief Loads a binary file into memory
412  * \param truePath      Absolute filename of binary
413  */
414 tBinary *Binary_DoLoad(tMount MountID, tInode Inode, const char *Path)
415 {
416         tBinary *pBinary;
417          int    fp;
418         Uint32  ident;
419         tBinaryType     *bt = gRegBinTypes;
420         
421         ENTER("iMountID XInode sPath", MountID, Inode, Path);
422         
423         // Open File
424         fp = VFS_OpenInode(MountID, Inode, VFS_OPENFLAG_READ);
425         if(fp == -1) {
426                 LOG("Unable to load file, access denied");
427                 LEAVE('n');
428                 return NULL;
429         }
430
431         LOG("fp = 0x%x", fp);
432         
433         // Read File Type
434         VFS_Read(fp, 4, &ident);
435         VFS_Seek(fp, 0, SEEK_SET);
436
437         LOG("ident = 0x%x", ident);
438
439         // Determine the type   
440         for(; bt; bt = bt->Next)
441         {
442                 if( (ident & bt->Mask) != (Uint32)bt->Ident )
443                         continue;
444                 LOG("bt = %p (%s)", bt, bt->Name);
445                 pBinary = bt->Load(fp);
446                 break;
447         }
448
449         LOG("pBinary = %p", pBinary);
450         
451         // Close File
452         VFS_Close(fp);
453         
454         // Catch errors
455         if(!bt) {
456                 Log_Warning("Binary", "'%s' is an unknown file type. (%02x %02x %02x %02x)",
457                         Path, ident&0xFF, (ident>>8)&0xFF, (ident>>16)&0xFF, (ident>>24)&0xFF);
458                 LEAVE('n');
459                 return NULL;
460         }
461         
462         // Error Check
463         if(pBinary == NULL) {
464                 LEAVE('n');
465                 return NULL;
466         }
467         
468         // Initialise Structure
469         pBinary->ReferenceCount = 0;
470         pBinary->MountID = MountID;
471         pBinary->Inode = Inode;
472         
473         // Debug Information
474         LOG("Interpreter: '%s'", pBinary->Interpreter);
475         LOG("Base: 0x%x, Entry: 0x%x", pBinary->Base, pBinary->Entry);
476         LOG("NumSections: %i", pBinary->NumSections);
477         
478         // Add to the list
479         SHORTLOCK(&glBinListLock);
480         pBinary->Next = glLoadedBinaries;
481         glLoadedBinaries = pBinary;
482         SHORTREL(&glBinListLock);
483
484         // TODO: Register the path with the binary      
485
486         // Return
487         LEAVE('p', pBinary);
488         return pBinary;
489 }
490
491 /**
492  * \fn void Binary_Unload(void *Base)
493  * \brief Unload / Unmap a binary
494  * \param Base  Loaded Base
495  * \note Currently used only for kernel libaries
496  */
497 void Binary_Unload(void *Base)
498 {
499         tKernelBin      *pKBin;
500         tKernelBin      *prev = NULL;
501          int    i;
502         
503         if((Uint)Base < 0xC0000000)
504         {
505                 // TODO: User Binaries
506                 Log_Warning("BIN", "Unloading user binaries is currently unimplemented");
507                 return;
508         }
509         
510         // Kernel Libraries
511         for(pKBin = glLoadedKernelLibs;
512                 pKBin;
513                 prev = pKBin, pKBin = pKBin->Next)
514         {
515                 // Check the base
516                 if(pKBin->Base != Base) continue;
517                 // Deallocate Memory
518                 for(i = 0; i < pKBin->Info->NumSections; i++)
519                 {
520                         // TODO: VFS_MUnmap();
521                 }
522                 // Dereference Binary
523                 Binary_Dereference( pKBin->Info );
524                 // Remove from list
525                 if(prev)        prev->Next = pKBin->Next;
526                 else            glLoadedKernelLibs = pKBin->Next;
527                 // Free Kernel Lib
528                 free(pKBin);
529                 return;
530         }
531 }
532
533 /**
534  * \fn void Binary_Dereference(tBinary *Info)
535  * \brief Dereferences and if nessasary, deletes a binary
536  * \param Info  Binary information structure
537  */
538 void Binary_Dereference(tBinary *Info)
539 {
540         // Decrement reference count
541         Info->ReferenceCount --;
542         
543         // Check if it is still in use
544         if(Info->ReferenceCount)        return;
545         
546         /// \todo Implement binary freeing
547 }
548
549 /**
550  * \fn char *Binary_RegInterp(char *Path)
551  * \brief Registers an Interpreter
552  * \param Path  Path to interpreter provided by executable
553  */
554 char *Binary_RegInterp(char *Path)
555 {
556          int    i;
557         // NULL Check Argument
558         if(Path == NULL)        return NULL;
559         // NULL Check the array
560         if(gsaRegInterps == NULL)
561         {
562                 giRegInterps = 1;
563                 gsaRegInterps = malloc( sizeof(char*) );
564                 gsaRegInterps[0] = malloc( strlen(Path) );
565                 strcpy(gsaRegInterps[0], Path);
566                 return gsaRegInterps[0];
567         }
568         
569         // Scan Array
570         for( i = 0; i < giRegInterps; i++ )
571         {
572                 if(strcmp(gsaRegInterps[i], Path) == 0)
573                         return gsaRegInterps[i];
574         }
575         
576         // Interpreter is not in list
577         giRegInterps ++;
578         gsaRegInterps = malloc( sizeof(char*)*giRegInterps );
579         gsaRegInterps[i] = malloc( strlen(Path) );
580         strcpy(gsaRegInterps[i], Path);
581         return gsaRegInterps[i];
582 }
583
584 // ============
585 // Kernel Binary Handling
586 // ============
587 /**
588  * \fn void *Binary_LoadKernel(const char *File)
589  * \brief Load a binary into kernel space
590  * \note This function shares much with #Binary_Load, but does it's own mapping
591  * \param File  File to load into the kernel
592  */
593 void *Binary_LoadKernel(const char *File)
594 {
595         tBinary *pBinary;
596         tKernelBin      *pKBinary;
597         tVAddr  base = -1;
598         tMount  mount_id;
599         tInode  inode;
600
601         ENTER("sFile", File);
602         
603         // Sanity Check Argument
604         if(File == NULL) {
605                 LEAVE('n');
606                 return 0;
607         }
608
609         {
610                 int fd = VFS_Open(File, VFS_OPENFLAG_READ);
611                 tFInfo  info;
612                 if(fd == -1) {
613                         LEAVE('n');
614                         return NULL;
615                 }
616                 VFS_FInfo(fd, &info, 0);
617                 mount_id = info.mount;
618                 inode = info.inode;
619                 VFS_Close(fd);
620         }
621         
622         // Check if the binary has already been loaded
623         if( (pBinary = Binary_GetInfo(mount_id, inode)) )
624         {
625                 for(pKBinary = glLoadedKernelLibs;
626                         pKBinary;
627                         pKBinary = pKBinary->Next )
628                 {
629                         if(pKBinary->Info == pBinary) {
630                                 LEAVE('p', pKBinary->Base);
631                                 return pKBinary->Base;
632                         }
633                 }
634         }
635         else
636                 pBinary = Binary_DoLoad(mount_id, inode, File); // Else load it
637         
638         // Error Check
639         if(pBinary == NULL) {
640                 LEAVE('n');
641                 return NULL;
642         }
643         
644         // --------------
645         // Now pBinary is valid (either freshly loaded or only user mapped)
646         // So, map it into kernel space
647         // --------------
648         
649         // Reference Executable (Makes sure that it isn't unloaded)
650         pBinary->ReferenceCount ++;
651
652         Binary_MapIn(pBinary, File, KLIB_LOWEST, KLIB_HIGHEST);
653
654         // Relocate Library
655         if( !Binary_Relocate( (void*)base ) )
656         {
657                 Log_Warning("Binary", "Relocation of '%s' failed, unloading", File);
658                 Binary_Unload( (void*)base );
659                 Binary_Dereference( pBinary );
660                 LEAVE('n');
661                 return 0;
662         }
663         
664         // Add to list (relocator must look at itself manually, not via Binary_GetSymbol)
665         pKBinary = malloc(sizeof(*pKBinary));
666         pKBinary->Base = (void*)base;
667         pKBinary->Info = pBinary;
668         SHORTLOCK( &glKBinListLock );
669         pKBinary->Next = glLoadedKernelLibs;
670         glLoadedKernelLibs = pKBinary;
671         SHORTREL( &glKBinListLock );
672         
673         LEAVE('p', base);
674         return (void*)base;
675 }
676
677 /**
678  * \fn Uint Binary_Relocate(void *Base)
679  * \brief Relocates a loaded binary (used by kernel libraries)
680  * \param Base  Loaded base address of binary
681  * \return Boolean Success
682  */
683 Uint Binary_Relocate(void *Base)
684 {
685         Uint32  ident = *(Uint32*) Base;
686         tBinaryType     *bt = gRegBinTypes;
687         
688         for(; bt; bt = bt->Next)
689         {
690                 if( (ident & bt->Mask) == (Uint)bt->Ident )
691                         return bt->Relocate( (void*)Base);
692         }
693         
694         Log_Warning("BIN", "%p is an unknown file type. (%02x %02x %02x %02x)",
695                 Base, ident&0xFF, (ident>>8)&0xFF, (ident>>16)&0xFF, (ident>>24)&0xFF);
696         return 0;
697 }
698
699 /**
700  * \fn int Binary_GetSymbol(char *Name, Uint *Val)
701  * \brief Get a symbol value
702  * \return Value of symbol or -1 on error
703  * 
704  * Gets the value of a symbol from either the currently loaded
705  * libraries or the kernel's exports.
706  */
707 int Binary_GetSymbol(const char *Name, Uint *Val)
708 {
709         if( Binary_GetSymbolEx(Name, Val) )     return 1;
710         return 0;
711 }
712
713 /**
714  * \fn Uint Binary_GetSymbolEx(char *Name, Uint *Value)
715  * \brief Get a symbol value
716  * 
717  * Gets the value of a symbol from either the currently loaded
718  * libraries or the kernel's exports.
719  */
720 Uint Binary_GetSymbolEx(const char *Name, Uint *Value)
721 {
722          int    i;
723         tKernelBin      *pKBin;
724          int    numKSyms = ((Uint)&gKernelSymbolsEnd-(Uint)&gKernelSymbols)/sizeof(tKernelSymbol);
725         
726         // Scan Kernel
727         for( i = 0; i < numKSyms; i++ )
728         {
729                 if(strcmp(Name, gKernelSymbols[i].Name) == 0) {
730                         *Value = gKernelSymbols[i].Value;
731                         return 1;
732                 }
733         }
734         
735         // Scan Loaded Libraries
736         for(pKBin = glLoadedKernelLibs;
737                 pKBin;
738                 pKBin = pKBin->Next )
739         {
740                 if( Binary_FindSymbol(pKBin->Base, Name, Value) ) {
741                         return 1;
742                 }
743         }
744         
745         Log_Warning("BIN", "Unable to find symbol '%s'", Name);
746         return 0;
747 }
748
749 /**
750  * \fn Uint Binary_FindSymbol(void *Base, char *Name, Uint *Val)
751  * \brief Get a symbol from the specified library
752  * \param Base  Base address
753  * \param Name  Name of symbol to find
754  * \param Val   Pointer to place final value
755  */
756 Uint Binary_FindSymbol(void *Base, const char *Name, Uint *Val)
757 {
758         Uint32  ident = *(Uint32*) Base;
759         tBinaryType     *bt = gRegBinTypes;
760         
761         for(; bt; bt = bt->Next)
762         {
763                 if( (ident & bt->Mask) == (Uint)bt->Ident )
764                         return bt->GetSymbol(Base, Name, Val);
765         }
766         
767         Log_Warning("BIN", "Binary_FindSymbol - %p is an unknown file type. (%02x %02x %02x %02x)",
768                 Base, ident&0xFF, ident>>8, ident>>16, ident>>24);
769         return 0;
770 }
771
772 /**
773  * \brief Check if a range of memory is fully free
774  * \return Inverse boolean free (0 if all pages are unmapped)
775  */
776 int Binary_int_CheckMemFree( tVAddr _start, size_t _len )
777 {
778         _len += _start & (PAGE_SIZE-1);
779         _len = (_len + PAGE_SIZE - 1) & ~(PAGE_SIZE-1);
780         _start &= ~(PAGE_SIZE-1);
781         for( ; _len > PAGE_SIZE; _len -= PAGE_SIZE, _start += PAGE_SIZE ) {
782                 if( MM_GetPhysAddr(_start) != 0 )
783                         return 1;
784         }
785         if( _len == PAGE_SIZE && MM_GetPhysAddr(_start) != 0 )
786                 return 1;
787         return 0;
788 }
789
790
791 // === EXPORTS ===
792 EXPORT(Binary_FindSymbol);
793 EXPORT(Binary_Unload);

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