Show
Ignore:
Timestamp:
05/24/08 09:54:46 (6 months ago)
Author:
mwhitworth
Message:

Create startup module.

Location:
Whitix/branches/hybrid/kernel
Files:
1 added
4 modified

Legend:

Unmodified
Added
Removed
  • Whitix/branches/hybrid/kernel/main.c

    r489 r538  
    1717 */ 
    1818 
    19 /* TODO: Fix cli(); hlt(); */ 
     19/* FIXME: Trim includes. */ 
    2020 
    2121#include <console.h> 
     
    3535#include <time.h> 
    3636 
    37 /* Implement proper ramdisk support and remove. */ 
    38 #include "../devices/storage/ata/ata.h" 
    39  
    40 extern WORD rootDevMajor,rootDevMinor; 
    41 extern BYTE biosDriveNo; 
    42  
    43 static void CallInitCalls() 
    44 { 
    45         InitCall* currCall; 
    46  
    47         for (currCall=(InitCall*)initcall_start; currCall<(InitCall*)initcall_end; currCall++) 
    48                 (*currCall)(); 
    49 } 
    50  
    51 struct StorageDevice* StartMountCd() 
    52 { 
    53         int i; 
    54         struct StorageDevice* rootDev=NULL; 
    55  
    56         /* Temporary hack for live cd */ 
    57         for (i=0; i<256; i+=64) 
    58         { 
    59                 struct AtaDrive* drive; 
    60                 rootDev=DevFindRootDev(4,i); 
    61                 if (!rootDev) 
    62                         continue; 
    63  
    64                 drive=(struct AtaDrive*)(rootDev->priv); 
    65  
    66                 if (drive->type & ATA_REMOVABLE) 
    67                         break; 
    68         } 
    69  
    70         /* Didn't find it */ 
    71         if (i == 256) 
    72                 rootDev=NULL; 
    73  
    74         return rootDev; 
    75 } 
    76  
    77 /* 
    78  * Start 
    79  * ----- 
    80  * 
    81  * This is where system things will be inited 
    82  * This still runs at kernel level and does all the things that main 
    83  * just couldn't do, like fs code  
    84  */ 
    85  
    86 #define RANGE_KBS(start,end) ((DWORD)end-(DWORD)start)/1024 
    87  
    88 static void Start() 
    89 { 
    90         struct StorageDevice* rootDev=NULL; 
    91         int fds[]={ 
    92                 0,0,0, 
    93         }; 
    94  
    95         CallInitCalls(); 
    96  
    97         /* For ISO filesystems, have a ramdisk - makes life a lot easier */ 
    98         if (biosDriveNo > 0x80 && rootDevMajor == 4) 
    99                 rootDev=StartMountCd(); 
    100         else 
    101                 /* Decode root filesystem here */ 
    102                 rootDev=DevFindRootDev(rootDevMajor,rootDevMinor); 
    103  
    104         if (!rootDev) 
    105         { 
    106                 printf("Device major:%d, minor: %d not valid root device.\n",(int)rootDevMajor,(int)rootDevMinor); 
    107                 cli(); 
    108                 hlt(); 
    109         } 
    110  
    111         VfsMountRoot(rootDev); 
    112  
    113         /* Print a few stats about the size of the kernel */ 
    114         printf("Kernel code size: %uk, kernel data size: %uk, kernel BSS size: %uk\n",RANGE_KBS(code,endCode),RANGE_KBS(data,endData),RANGE_KBS(bss,endbss)); 
    115  
    116         /* This mounting will be moved to an init-like program. */ 
    117          
    118         printf("DEVFS: Mounting device filesystem at %s\n", DEVICES_PATH); 
    119  
    120         if (VfsMount(NULL,DEVICES_PATH,"DevFs",NULL)) 
    121         { 
    122                 KernelPanic("Failed to mount the device filesystem"); 
    123         } 
    124  
    125         printf("Mounted the filesystems. Starting the shell\n"); 
    126  
    127         if (DoOpenFile(&current->files[0], DEVICES_PATH "/Consoles/Console0",FILE_READ | FILE_FORCE_OPEN,0)) 
    128                 KernelPanic("Failed to open the first console. Halting"); 
    129  
    130         extern int Exec(char* pathName,int* fds,char** argv); 
    131         if (Exec("/System/Startup/startup", fds, NULL) < 0) 
    132                 KernelPanic("Could not launch the startup program. Halting"); 
    133  
    134         ThrProcessExit(0); 
    135         ThrSchedule(); 
    136  
    137         while (1) hlt(); 
    138 } 
    139  
    14037extern int ArchInit(); 
    14138 
    14239void KernelMain() 
    14340{ 
    144         struct Process* start; 
    145         struct Thread* firstThread; 
    146  
     41        EarlyConsoleInit(); 
    14742        ArchInit(); 
    148         ThrInit(); 
    149         VfsInit(); 
     43        ThrEarlyInit(); 
    15044        TimeInit(); 
    15145 
    152         start=ThrCreateProcess("KernelLoading"); 
    153         firstThread=ThrCreateThread(start,(DWORD)Start,false,0, NULL); 
     46        ModulesBootLoad(); 
    15447 
    155         ThrStartThread(firstThread); 
    156  
    157         /* This is where the idle thread idles */ 
     48        /* This is where the idle thread idles, after returning from StartInit in startup.c */ 
    15849        ThrIdleFunc(); 
    15950} 
  • Whitix/branches/hybrid/kernel/module.c

    r490 r538  
    2929#include <sys.h> 
    3030#include <task.h> 
     31#include <imports.h> 
    3132 
    3233LIST_HEAD(moduleList); 
     34 
     35static struct Module bootModules[20]; 
     36int bootIndex=0; 
    3337 
    3438#define MODULE_START    0xD8000000 
     
    8387 
    8488                if (name) 
    85                         printf("%s+%#X/%#X\n", name, offset, size); 
     89                        KePrint("%s+%#X/%#X\n", name, offset, size); 
    8690                else 
    87                         printf("%#X\n", address); 
     91                        KePrint("%#X\n", address); 
    8892        }else 
    89                 printf("%#X\n", address); 
     93                KePrint("%#X\n", address); 
    9094} 
    9195 
     
    106110void ModuleSymbolPrint(DWORD address) 
    107111{ 
    108         printf("%#X\n", address); 
     112        KePrint("%#X\n", address); 
    109113} 
    110114 
     
    148152        } 
    149153 
    150         printf("Could not resolve %s\n", name); 
     154        KePrint("Could not resolve %s\n", name); 
    151155 
    152156        return 0; 
     
    191195                                        break; 
    192196 
    193                                 printf("Could not resolve %s\n", symName); 
     197                                KePrint("Could not resolve %s\n", symName); 
    194198                                return -ENOENT; 
    195199 
     
    200204                        /* Common symbols aren't supported by the module loading code. */ 
    201205                        case STN_COMMON: 
    202                                 printf("Please recompile the module with -fno-common.\n"); 
     206                                KePrint("Please recompile the module with -fno-common.\n"); 
    203207                                return -ENOENT; 
    204208 
     
    240244 
    241245                        default: 
    242                                 printf("ModuleSymbolResolve: type = %u, %u\n", ELF_R_TYPE(reloc->info), symbol->symIndex); 
     246                                KePrint("ModuleSymbolResolve: type = %u, %u\n", ELF_R_TYPE(reloc->info), symbol->symIndex); 
    243247                } 
    244248        } 
     
    286290 
    287291        /* Get basic information about the module. */ 
    288         module=(struct Module*)malloc(sizeof(struct Module)); 
     292        if (MemAlloc) 
     293                module=(struct Module*)MemAlloc(sizeof(struct Module)); 
     294        else 
     295                module=&bootModules[bootIndex++]; 
     296 
    289297        module->loadAddr=loadAddr; 
    290298        module->sectionHeaders=sectionHeaders; 
     
    362370} 
    363371 
     372#define SYS_MODULE_BASE         51 
     373 
     374int SysModuleAdd(void* data, unsigned long length); 
     375int SysModuleRemove(const char* name); 
     376 
     377struct SysCall moduleSysCalls[]={ 
     378        { SysModuleAdd, 8 }, 
     379        { SysModuleRemove, 4}, 
     380        { 0, 0 } 
     381}; 
     382 
     383void ModulesBootLoad() 
     384{ 
     385        /* The modules are located above the kernel in memory. */ 
     386        BYTE* numModules=(BYTE*)0x2FFFF; 
     387        int i=0; 
     388        DWORD* length=(DWORD*)0x40000; 
     389        char* names=(char*)0x30000; 
     390 
     391        for (i=0; i<*numModules; i++) 
     392        { 
     393                /* Load each module, one at a time. */ 
     394                BYTE* data=(BYTE*)(length+1); 
     395                void* kData=(void*)VirtMapPhysRange(MODULE_START, MODULE_END, PAGE_ALIGN_UP(*length) >> PAGE_SHIFT, 3); 
     396                char buf[32]; 
     397 
     398                int j=0; 
     399 
     400                while (names[j] != '\n') 
     401                        j++; 
     402 
     403                names[j]='\0'; 
     404 
     405                strcpy(buf, names); 
     406                strcat(buf, ".sys"); 
     407 
     408                if (ModuleAdd(data, kData, *length, 0)) 
     409                        KernelPanic("Could not initialize the kernel"); 
     410 
     411                KePrint("MODULES: Loaded %s, %dkb\n", buf, (*length)/1024); 
     412 
     413                names+=strlen(names)+1; 
     414 
     415                length+=(*length/4)+1; 
     416        } 
     417 
     418        /* Free the low pages. */ 
     419 
     420        /* Add the calls to the system table. */ 
     421        SysRegisterRange(SYS_MODULE_BASE, moduleSysCalls); 
     422} 
     423 
    364424int SysModuleAdd(void* data, unsigned long length) 
    365425{ 
     
    374434int SysModuleRemove(const char* name) 
    375435{ 
    376         printf("SysModuleRemove\n"); 
     436        KePrint("SysModuleRemove\n"); 
    377437        return 0; 
    378438} 
  • Whitix/branches/hybrid/kernel/sched.c

    r491 r538  
    1717 */ 
    1818 
     19/* TODO: Clean up these includes. */ 
    1920#include <sched.h> 
    2021#include <error.h> 
     
    2526#include <malloc.h> 
    2627#include <fs/vfs.h> 
     28#include <init.h> 
    2729#include <task.h> 
    2830#include <panic.h> 
     
    3133#include <net/socket.h> 
    3234#include <module.h> 
     35#include <imports.h> 
     36#include <sys.h> 
    3337 
    3438/* List heads */ 
     
    5559 
    5660/* Used when a thread is exiting */ 
    57 DWORD exitStack[PAGE_SIZE]; 
     61BYTE exitStack[PAGE_SIZE/4]; 
    5862 
    5963/*********************************************************************** 
     
    141145        SpinLockIrq(&procListLock); 
    142146 
    143         process=(struct Process*)MemCacheAlloc(processCache); 
     147        process=(struct Process*)memCacheAlloc(processCache); 
    144148        if (!process) 
    145149                return NULL; 
     
    147151        if (VirtMemManagerSetup(process)) 
    148152        { 
    149                 MemCacheFree(processCache,process); 
     153                memCacheFree(processCache,process); 
    150154                SpinUnlockIrq(&procListLock); 
    151155                return NULL; 
     
    153157 
    154158        /* Allocate the process's name - used when listing processes or when faulting. */ 
    155         process->name=(char*)malloc(strlen(name)+1); 
    156         strcpy(process->name,name); 
     159        process->name=(char*)MemAlloc(strlen(name)+1); 
     160        strcpy(process->name, name); 
    157161         
    158162        /* Add to parent's children list */ 
     
    168172        process->state=THR_RUNNING; 
    169173 
    170         process->files=NULL; 
     174        process->files=NULL; /* FIXME */ 
    171175 
    172176        ListAddTail(&process->next,&processList); 
     
    176180        return process; 
    177181} 
     182 
     183SYMBOL_EXPORT(ThrCreateProcess); 
    178184 
    179185/*********************************************************************** 
     
    200206        ListRemove(&process->next); 
    201207         
    202         free(process->name); 
    203         MemCacheFree(processCache,process); 
     208        MemFree(process->name); 
     209        memCacheFree(processCache,process); 
    204210 
    205211        IrqRestoreFlags(flags); 
    206212} 
     213 
     214SYMBOL_EXPORT(ThrFreeProcess); 
    207215 
    208216/*********************************************************************** 
     
    251259        SpinLockIrq(&thrListLock); 
    252260 
    253         ret=(struct Thread*)MemCacheAlloc(threadCache); 
     261        ret=(struct Thread*)memCacheAlloc(threadCache); 
    254262        if (!ret) 
    255263                return NULL; 
     
    272280} 
    273281 
     282SYMBOL_EXPORT(ThrCreateThread); 
     283 
    274284/*********************************************************************** 
    275285 * 
     
    295305} 
    296306 
     307SYMBOL_EXPORT(ThrSetPriority); 
     308 
    297309/*********************************************************************** 
    298310 * 
     
    310322void _ThrFreeThread(struct Thread* thread) 
    311323{ 
    312         free((void*)thread->currStack); 
     324        MemFree((void*)thread->currStack); 
    313325        currThread=NULL; 
    314         MemCacheFree(threadCache, thread); 
     326        memCacheFree(threadCache, thread); 
    315327        ThrSchedule(); 
    316328} 
     
    350362 
    351363        if (thread == currThread) 
    352                 ArchSwitchStackCall(exitStack+PAGE_SIZE,_ThrFreeThread,thread); 
     364                ArchSwitchStackCall(exitStack+PAGE_SIZE/4,_ThrFreeThread,thread); 
    353365        else{ 
    354                 free((void*)(thread->currStack)); 
    355                 MemCacheFree(threadCache,thread); 
     366                MemFree((void*)(thread->currStack)); 
     367                memCacheFree(threadCache,thread); 
    356368        } 
    357369         
     
    401413        } 
    402414} 
     415 
     416SYMBOL_EXPORT(ThrStartThread); 
    403417 
    404418/*********************************************************************** 
     
    467481 ***********************************************************************/ 
    468482 
     483/* TODO: Move to filesystem code. */ 
     484 
    469485static void ThrReleaseFsContext() 
    470486{ 
     
    473489        /* Now release the filesystem context */ 
    474490 
    475         if (current->files) 
     491//      if (current->files) 
    476492                /* Close all files associated with the process */ 
    477                 for (i=0; i<current->numFds; i++) 
    478                         DoCloseFile(&current->files[i]); 
     493//              for (i=0; i<current->numFds; i++) 
     494//                      DoCloseFile(&current->files[i]); 
    479495 
    480496        /* Does not apply to the KernelLoading process */ 
    481         if (current->exec) 
    482                 VNodeRelease(current->exec); 
    483  
    484         VNodeRelease(current->cwd); 
    485         VNodeRelease(current->root); 
    486  
    487         free(current->files); 
    488         free(current->sCwd); 
     497//      if (current->exec) 
     498//              VNodeRelease(current->exec); 
     499 
     500//      VNodeRelease(current->cwd); 
     501//      VNodeRelease(current->root); 
     502 
     503//      free(current->files); 
     504//      free(current->sCwd); 
    489505} 
    490506 
     
    511527 
    512528        /* Release all vmm areas */ 
    513         MmapProcessRemove(current); 
     529//      MmapProcessRemove(current); 
    514530        ThrReleaseFsContext(); 
    515531 
     
    548564} 
    549565 
     566SYMBOL_EXPORT(ThrProcessExit); 
     567 
    550568/*********************************************************************** 
    551569 * 
     
    599617        } 
    600618} 
     619 
     620SYMBOL_EXPORT(ThrResumeThread); 
    601621 
    602622/*********************************************************************** 
     
    751771int SysExitThread(int threadId) 
    752772{ 
    753 //      printf("SysExitThread(%d)\n", threadId); 
    754773        if (threadId == -1) /* Exit current thread. */ 
    755774                ThrDoExitThread(); 
     
    803822        waitEntry.thread=currThread; 
    804823         
    805         if (finishStatus) 
    806                 if (VirtCheckArea(finishStatus,sizeof(int),VER_WRITE)) 
    807                         return -EFAULT; 
     824//      if (finishStatus) 
     825//              if (VirtCheckArea(finishStatus,sizeof(int),VER_WRITE)) 
     826//                      return -EFAULT; 
    808827 
    809828        if (pid >= 0) 
     
    903922 ***********************************************************************/ 
    904923 
    905 int ThrInit() 
     924int ThrEarlyInit() 
    906925{ 
    907926        ThrArchInit(); /* Set up arch-specific data structures for multitasking here */ 
    908  
    909         /* Allocate the general scheduling caches */ 
    910         processCache=MemCacheCreate("Process Cache", sizeof(struct Process), ThrProcessConstructor, NULL, 0); 
    911         threadCache=MemCacheCreate("Thread Cache", sizeof(struct Thread), NULL, NULL, 0); 
    912927 
    913928        currThread=idleTask; 
     
    919934        return 0; 
    920935} 
     936 
     937#define SYS_SCHED_BASE          34 
     938 
     939struct SysCall schedSysCallTable[]= 
     940{ 
     941        { SysCreateThread,                      12 }, 
     942        { SysGetCurrentThreadId,        0  }, 
     943        { SysGetCurrentProcessId,       0  }, 
     944        { SysExitThread,                        4  }, 
     945        { SysExit,                                      4  }, 
     946        { SysWaitForProcessFinish,      8  }, 
     947        { SysYield,                                     0  }, 
     948        { 0,                                            0  }, 
     949}; 
     950 
     951int ThrInit() 
     952{ 
     953        /* Allocate the general scheduling caches */ 
     954        processCache=memCacheCreate("Process Cache", sizeof(struct Process), ThrProcessConstructor, NULL, 0); 
     955        threadCache=memCacheCreate("Thread Cache", sizeof(struct Thread), NULL, NULL, 0); 
     956 
     957        /* Register the threading system calls. */ 
     958        SysRegisterRange(SYS_SCHED_BASE, schedSysCallTable); 
     959 
     960        return 0; 
     961} 
     962 
     963CoreInit(ThrInit); 
  • Whitix/branches/hybrid/kernel/symbols.c

    r483 r538  
    6565        if (*offset > *size) 
    6666        { 
    67                 printf("%#X-%#X = %#X %#X\n", address, symbol->symValue, *offset, *size); 
     67                KePrint("%#X-%#X = %#X %#X\n", address, symbol->symValue, *offset, *size); 
    6868                while (1); 
    6969        } 
     
    7979 
    8080        if (name) 
    81                 printf("%s+%#X/%#X\n", name, offset, size); 
     81                KePrint("%s+%#X/%#X\n", name, offset, size); 
    8282        else 
    83                 printf("%#X\n", address); 
    84 } 
    85  
    86 int SymbolInit() 
    87 { 
    88         return 0; 
     83                KePrint("%#X\n", address); 
    8984} 
    9085 
     
    124119} 
    125120 
    126 CoreInit(SymbolInit); 
    127  
    128121#else 
    129122/* Stub functions. */ 
     
    131124void SymbolPrint(DWORD address) 
    132125{ 
    133         printf("%10X\n", address); 
     126        KePrint("%10X\n", address); 
     127} 
     128 
     129void SymbolsCopy() 
     130{ 
    134131} 
    135132