void main(int argc, char* argv[]) { while(1) { prints("Process 0: Trying to start process 1.\n"); /* Try to create and run process 1. */ if (0 != createprocess(1)) { prints("createprocess failed.\n"); } else { prints("Process 0: Process 1 terminated.\n"); } debugger(); } }
/*! This is the sensor process, it takes a simulated input ( a keyboard press ) and passes it to the Light control process via IPC */ void main(int argc, char* argv[]) { long send_port; if (ALL_OK != createprocess(1)) { printat(MAIN_DEBUG_LINE, 0, "Sensor control: createprocess of Light control failed.\n"); return; } // Locating the send port of the light control process send_port = findport(0, 1); if (send_port < 0) { printat(MAIN_DEBUG_LINE, 0, "Sensor control: findport() failed (Light control) "); return; } printat(0, 0, "Welcome to VRTTCS (very realtime traffic control system)"); printat(MAIN_DEBUG_LINE, 0, "Sensor control: "); while (1) { register long scan_code = getscancode(); if (0x1c == scan_code) { struct message msg; printat(MAIN_DEBUG_LINE, 0, "Sensor control: Got sensor input"); if (ALL_OK != send(send_port, &msg)) { printat(MAIN_DEBUG_LINE, 0, "Sensor control: send() failed"); return; } else { /* We let the debug message display some time*/ pause(500); printat(MAIN_DEBUG_LINE, 0, "Sensor control: "); } } } }
int coff_loadusermodule( char *module_name, //the name of the module char *coff_image, //the location of the pe image DWORD base, //the desired base address to load the image so //that dex can perform the necessary relocations int mode, //Determines what kind of executable to load char *p, //the parameters char *workdir, PCB386 *parent //the parent PCB for EXEs, the parent pagedirectory for DLLs ) //location of image to load { char temp[255],temp2[255]; FILHDR *fhdr = (FILHDR*) coff_image; //a linked list used to keep track of the virtual addresses used //by this process so that it could be freed when the process //terminates process_mem *tmpr,*memptr; //validate if this file is a coff format executable #ifdef DEBUG_COFF printf("dex32_coff_loader: started\n"); printf("dex32_coff_loader: magic number obtained [%s]\n",itoa(fhdr->f_magic,temp,16)); #endif if (fhdr->f_magic==I386MAGIC) { int i; AOUTHDR *aouthdr = (AOUTHDR*)(coff_image + sizeof(FILHDR)); SCNHDR *section = (SCNHDR*)((DWORD)coff_image + sizeof(FILHDR)+sizeof(AOUTHDR)); SYMENT *symbols = fhdr->f_symptr; int totalsymbols = fhdr->f_nsyms; DWORD *pagedir,*pg; DWORD heapres=0,stackres=0; DWORD *stackloc; DWORD flags,pages,ret; int totalsections = fhdr->f_nscns; void (*entrypoint)(int,char**)=0; #ifdef DEBUG_COFF printf("dex32_coff_loader: magic number 2 obtained [%s]\n",itoa(aouthdr->magic,temp,16)); #endif if (aouthdr->magic!=ZMAGIC) return 0; //check again printf("dex32_coff_loader: loading executable..\n"); #ifdef DEBUG_COFF printf("dex32_coff_loader: entry point at %sH\n",itoa(aouthdr->entry,temp,16)); printf("dex32_coff_loader: Total sections %d\n",totalsections); #endif entrypoint=(void*) aouthdr->entry; if (fhdr->f_flags&F_RELFLG) { printf("dex32_coff_loader: Relocations have been stripped of the file\n"); }; memptr=(process_mem*)malloc(sizeof(process_mem)); tmpr=memptr; pagedir=(DWORD*)mempop(); //obtain a physical address from the memory manager pg=(DWORD*)getvirtaddress((DWORD)pagedir); //convert to a virtual address so that //we could use it here without disabling //the paging mechanism of the 386 //initialize the new pagedirectory memset(pg,0,0x1000); stackloc=(DWORD*)dex32_commitblock((DWORD)userstackloc-1000, 1000,&pages, pagedir,PG_WR | PG_USER | PG_PRESENT); //take note of this so that we could free it later tmpr->vaddr=(DWORD)stackloc; tmpr->pages=pages; tmpr->next=(process_mem*)malloc(sizeof(process_mem)); tmpr=tmpr->next; tmpr->vaddr=0;tmpr->next=0; //allocate the "reserve" user stack //this pages don't get a physical memory until it actually gets //accessed stackres=dex32_reserveblock((DWORD)stackloc-DEFAULT_STACKSIZE, DEFAULT_STACKSIZE,&pages,pagedir,PG_WR); //store the allocation information so that it could be linked to //the process PCB and then be able to free up the memory when app terminates tmpr->vaddr=(DWORD)stackres; tmpr->pages=pages; //create another node in the linked list tmpr->next=(process_mem*)malloc(sizeof(process_mem)); tmpr=tmpr->next; tmpr->vaddr=0;tmpr->next=0; //calculate the position of the ESP pointer stackloc = userstackloc - 4 ; //allocate "commited" user heap //unlike the stack, this one goes up dex32_commitblock((DWORD)userheap,1000,&pages, pagedir,PG_WR | PG_USER | PG_PRESENT); //take note of this, so that it wouldn't cause a memory leak later tmpr->vaddr=(DWORD)userheap; tmpr->pages=pages; tmpr->next=(process_mem*)malloc(sizeof(process_mem)); tmpr=tmpr->next; tmpr->vaddr=0;tmpr->next=0; //calculate the position of the reserved heap heapres=userheap+(pages*0x1000); //allocate "reserved" user heap dex32_reserveblock((DWORD)heapres, DEFAULT_HEAPSIZE,&pages,pagedir,PG_WR ); tmpr->vaddr=(DWORD)heapres; tmpr->pages=pages; tmpr->next=(process_mem*)malloc(sizeof(process_mem)); tmpr=tmpr->next; tmpr->vaddr=0;tmpr->next=0; //examine the sections for (i=0;i<totalsections;i++) { int i2; char section_name[256]; DWORD rawdata, vaddr,size,pages,relnum; RELOC *relocation; /* copy the name of the section to a temporary vriable */ for (i2=0; section[i].s_name[i2]&&i2<8;i2++) { section_name[i2] = section[i].s_name[i2]; }; section_name[i2]=0; /*null terminator*/ rawdata = section[i].s_scnptr; //pointer to raw data in file vaddr = section[i].s_vaddr; //absolute address in memory size = section[i].s_size; //size of the section relocation = (RELOC*)section[i].s_relptr; //relocation pointer relnum = section[i].s_nreloc; //number of relocations #ifdef DEBUG_COFF printf("dex32_coff_loader: Section name [%s]\n",section_name); printf("dex32_coff_loader: ** file offset [%d]\n",rawdata); printf("dex32_coff_loader: ** load at [%sH]\n",itoa(vaddr,temp,16)); #endif /*perform relocations*/ for (i2=0;i2<relnum;i2++) { if (relocation[i2].r_type==RELOC_ADDR32) { DWORD *loc=symbols[relocation[i2].r_symndx].e_value; printf("dex32_coff_loader: ABSOLUTE RELOCATION Detected\n"); } else if (relocation[i2].r_type==RELOC_REL32) { printf("dex32_coff_loader: RELATIVE RELOCATION Detected\n"); } else printf("dex32_coff_loader: UNKNOWN RELOCATION TYPE\n"); }; /*allocate physical memory and assign a virtual address*/ #ifdef DEBUG_COFF printf("dex32_coff_loader: Commiting block..\n"); #endif tmpr->vaddr=(DWORD)dex32_commitblock((DWORD)base+vaddr,size,&pages, pagedir,PG_WR | PG_USER | PG_PRESENT); #ifdef DEBUG_COFF printf("dex32_coff_loader: Copying image at %s to %s..\n", itoa(coff_image+rawdata,temp2,16),itoa(tmpr->vaddr,temp,16)); #endif //copy the section to main memory memcpy(tmpr->vaddr,coff_image+rawdata,size); #ifdef DEBUG_COFF printf("dex32_coff_loader: ***Done***\n"); #endif //take note of the memory used so that we could free it up later tmpr->pages=pages; tmpr->next=(process_mem*)malloc(sizeof(process_mem)); tmpr=tmpr->next; tmpr->vaddr=0;tmpr->next=0; }; dex32_stopints(&flags); ret=createprocess(entrypoint,module_name,pagedir,memptr,stackloc, 1000,0x2000,signal,p,workdir,parent); dex32_restoreints(flags); dex32_freeuserpagetable(pagedir1); return ret; }; return 0; //not a coff file or error loading file };