void MemoryModuleInit() { int i; int curpage; dbprintf('m',"MemoryGetsize() is %d\n",MemoryGetSize()); dbprintf('m',"MEM_PAGESIZE is %d\n",MEM_PAGESIZE); SetPara(); //All memory is in use for (curpage=0;curpage<(MemoryGetSize()/(MEM_PAGESIZE));curpage++) { // printf("curpage is %d\n",curpage); MemorySetFreemap(curpage,0); } //all the other memory space is unused for (curpage = ((lastosaddress+(MEM_PAGESIZE))/(MEM_PAGESIZE));curpage <(MemoryGetSize()/(MEM_PAGESIZE)); curpage++) { // printf("curpage is %d\n",curpage); MemorySetFreemap (curpage, 1); } //print for debug for(i=0;i<16;i++){ dbprintf('m',"freemap is 0x%x\n",freemap[i]); } }
//---------------------------------------------------------------------- // // MemoryModuleInit // // Initialize the memory module of the operating system. // Basically just need to setup the freemap for pages, and mark // the ones in use by the operating system as "VALID", and mark // all the rest as not in use. // //---------------------------------------------------------------------- void SetPara(){ int i; pagestart = (lastosaddress+(MEM_PAGESIZE))/(MEM_PAGESIZE); freemapmax=((MemoryGetSize()/(MEM_PAGESIZE))+31)/32; nfreepage=(MemoryGetSize()/(MEM_PAGESIZE))-pagestart+1; printf("nfreepage is %d\n",nfreepage); }
static int64 MemoryRead(Stream* stream, void* buffer, int64 size) { MemoryStream* ms = (MemoryStream*) stream; if(size < 0) return -1; if( !ms->useRawBuffer ) { int64 left = MemoryGetSize(stream) - ms->position; if(size > left) size = left; } uint8* cur = ms->buffer + ms->position; memcpy(buffer, cur, (size_t) size); ms->position += size; return size; }
static int64 MemorySeek(Stream* stream, int64 offset, int8 mode) { MemoryStream* ms = (MemoryStream*) stream; switch(mode) { case StreamSeekMode::Absolute: ms->position = offset; break; case StreamSeekMode::Relative: ms->position += offset; break; case StreamSeekMode::RelativeEnd: ms->position = MemoryGetSize(stream) - offset; break; } return true; }
uint32 MemoryAllocPage(void) { int mapnum; int bitnum; int i; int page; uint32 v; dbprintf('m',"There are still %d free pages.\n",nfreepage); page=(lastosaddress+(MEM_PAGESIZE))/(MEM_PAGESIZE); mapnum=page/32; if (nfreepage == 0) { return MEM_FAIL; } dbprintf('m',"Allocating memory, starting with entry %d\n",mapnum); while (freemap[mapnum] == 0) { mapnum +=1; if (mapnum >= ((MemoryGetSize()/(MEM_PAGESIZE))+31)/32) { mapnum = page/32; } } v=freemap[mapnum]; for (bitnum = 0; (v & (1 << bitnum)) == 0; bitnum++) { } freemap[mapnum] &= invert(1 << bitnum); v = (mapnum *32)+bitnum; dbprintf('m',"Allocated memory, from map %d, page 0x%x, map=0x%x.\n",mapnum,v,freemap[mapnum]); nfreepage--; // for(i=0;i<16;i++){ // printf("freemap is 0x%x\n",freemap[i]); // } return (v); }
//---------------------------------------------------------------------- // // main // // This routine is called when the OS starts up. It allocates a // PCB for the first process - the one corresponding to the initial // thread of execution. Note that the stack pointer is already // set correctly by _osinit (assembly language code) to point // to the stack for the 0th process. This stack isn't very big, // though, so it should be replaced by the system stack of the // currently running process. // //---------------------------------------------------------------------- void main (int argc, char *argv[]) { int i,j; int n; char buf[120]; char *userprog = (char *)0; int base=0; int numargs=0; char allargs[SIZE_ARG_BUFF]; int allargs_offset = 0; debugstr[0] = '\0'; printf ("Got %d arguments.\n", argc); printf ("Available memory: 0x%x -> 0x%x.\n", (int)lastosaddress, MemoryGetSize ()); printf ("Argument count is %d.\n", argc); for (i = 0; i < argc; i++) { printf ("Argument %d is %s.\n", i, argv[i]); } FsModuleInit (); for (i = 0; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'D': dstrcpy (debugstr, argv[++i]); break; case 'i': n = dstrtol (argv[++i], (void *)0, 0); ditoa (n, buf); printf ("Converted %s to %d=%s\n", argv[i], n, buf); break; case 'f': { int start, codeS, codeL, dataS, dataL, fd, j; int addr = 0; static unsigned char buf[200]; fd = ProcessGetCodeInfo (argv[++i], &start, &codeS, &codeL, &dataS, &dataL); printf ("File %s -> start=0x%08x\n", argv[i], start); printf ("File %s -> code @ 0x%08x (size=0x%08x)\n", argv[i], codeS, codeL); printf ("File %s -> data @ 0x%08x (size=0x%08x)\n", argv[i], dataS, dataL); while ((n = ProcessGetFromFile (fd, buf, &addr, sizeof (buf))) > 0) { for (j = 0; j < n; j += 4) { printf ("%08x: %02x%02x%02x%02x\n", addr + j - n, buf[j], buf[j+1], buf[j+2], buf[j+3]); } } close (fd); break; } case 'u': userprog = argv[++i]; base = i; // Save the location of the user program's name break; default: printf ("Option %s not recognized.\n", argv[i]); break; } if(userprog) break; } } dbprintf ('i', "About to initialize queues.\n"); AQueueModuleInit (); dbprintf ('i', "After initializing queues.\n"); MemoryModuleInit (); dbprintf ('i', "After initializing memory.\n"); ProcessModuleInit (); dbprintf ('i', "After initializing processes.\n"); SynchModuleInit (); dbprintf ('i', "After initializing synchronization tools.\n"); KbdModuleInit (); dbprintf ('i', "After initializing keyboard.\n"); ClkModuleInit (); dbprintf ('i', "After initializing clock.\n"); for (i = 0; i < 100; i++) { buf[i] = 'a'; } i = FsOpen ("vm", FS_MODE_WRITE); dbprintf ('i', "VM Descriptor is %d\n", i); FsSeek (i, 0, FS_SEEK_SET); FsWrite (i, buf, 80); FsClose (i); // Setup command line arguments if (userprog != (char *)0) { numargs=0; allargs_offset = 0; // Move through each of the argv addresses for(i=0; i<argc-base; i++) { // At each argv address, copy the string into allargs, including the '\0' for(j=0; allargs_offset < SIZE_ARG_BUFF; j++) { allargs[allargs_offset++] = argv[i+base][j]; if (argv[i+base][j] == '\0') break; // end of this string } numargs++; } allargs[SIZE_ARG_BUFF-1] = '\0'; // set last char to NULL for safety ProcessFork(0, (uint32)allargs, userprog, 1); } else { dbprintf('i', "No user program passed!\n"); } ClkStart(); dbprintf ('i', "Set timer quantum to %d, about to run first process.\n", processQuantum); intrreturn (); // Should never be called because the scheduler exits when there // are no runnable processes left. exitsim(); // NEVER RETURNS! }
//---------------------------------------------------------------------- // // main // // This routine is called when the OS starts up. It allocates a // PCB for the first process - the one corresponding to the initial // thread of execution. Note that the stack pointer is already // set correctly by _osinit (assembly language code) to point // to the stack for the 0th process. This stack isn't very big, // though, so it should be replaced by the system stack of the // currently running process. // //---------------------------------------------------------------------- main (int argc, char *argv[]) { int i, j; int n; char buf[120]; char *userprog = (char *)0; static PCB temppcb; uint32 addr; extern void SysprocCreateProcesses (); char *param[12]={NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; int base; debugstr[0] = '\0'; MyFuncRetZero(); printf ("Got %d arguments.\n", argc); printf ("Available memory: 0x%x -> 0x%x.\n", lastosaddress, MemoryGetSize ()); printf ("Argument count is %d.\n", argc); for (i = 0; i < argc; i++) { printf ("Argument %d is %s.\n", i, argv[i]); } // *((int *)0xfff00100) = 't'; FsModuleInit (); for (i = 0; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'D': dstrcpy (debugstr, argv[++i]); break; case 'i': n = dstrtol (argv[++i], (void *)0, 0); ditoa (n, buf); printf ("Converted %s to %d=%s\n", argv[i], n, buf); break; case 'f': { int start, codeS, codeL, dataS, dataL, fd, j; int addr = 0; static unsigned char buf[200]; fd = ProcessGetCodeInfo (argv[++i], &start, &codeS, &codeL, &dataS, &dataL); printf ("File %s -> start=0x%08x\n", argv[i], start); printf ("File %s -> code @ 0x%08x (size=0x%08x)\n", argv[i], codeS, codeL); printf ("File %s -> data @ 0x%08x (size=0x%08x)\n", argv[i], dataS, dataL); while ((n = ProcessGetFromFile (fd, buf, &addr, sizeof (buf))) > 0) { for (j = 0; j < n; j += 4) { printf ("%08x: %02x%02x%02x%02x\n", addr + j - n, buf[j], buf[j+1], buf[j+2], buf[j+3]); } } close (fd); break; } case 'u': userprog = argv[++i]; base = i; break; default: printf ("Option %s not recognized.\n", argv[i]); break; } if(userprog) break; } } dbprintf ('i', "About to initialize queues.\n"); QueueModuleInit (); dbprintf ('i', "After initializing queues.\n"); MemoryModuleInit (); dbprintf ('i', "After initializing memory.\n"); ProcessModuleInit (); dbprintf ('i', "After initializing processes.\n"); ShareModuleInit (); dbprintf ('i', "After initializing shared memory.\n"); SynchModuleInit (); dbprintf ('i', "After initializing synchronization tools.\n"); KbdModuleInit (); dbprintf ('i', "After initializing keyboard.\n"); for (i = 0; i < 100; i++) { buf[i] = 'a'; } i = FsOpen ("vm", FS_MODE_WRITE); dbprintf ('i', "VM Descriptor is %d\n", i); FsSeek (i, 0, FS_SEEK_SET); FsWrite (i, buf, 80); FsClose (i); if (userprog != (char *)0) { for(i=base;i<argc&&i-base<11; i++) { param[i-base] = argv[i]; } process_create(0,0,param[0], param[1], param[2], param[3], param[4], param[5], param[6], param[7], param[8], param[9], param[10], param[11]); // ProcessFork (0, (uint32)"Help Me man!", userprog, 1); } SysprocCreateProcesses (); dbprintf ('i', "Created processes - about to set timer quantum.\n"); TimerSet (processQuantum); dbprintf ('i', "Set timer quantum to %d, about to run first process.\n", processQuantum); intrreturn (); // Should never be called because the scheduler exits when there // are no runnable processes left. exitsim(); // NEVER RETURNS! }