/***************************************************************************************** Copyright (c) 2002-2004 The UbixOS Project All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions, the following disclaimer and the list of authors. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, the following disclaimer and the list of authors in the documentation and/or other materials provided with the distribution. Neither the name of the UbixOS Project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. $Id$ *****************************************************************************************/ #include <ubixos/exec.h> #include <ubixos/sched.h> #include <ubixos/ld.h> #include <ubixos/kpanic.h> #include <ubixos/endtask.h> #include <vmm/vmm.h> #include <lib/kmalloc.h> #include <lib/kprintf.h> #include <lib/string.h> #include <assert.h> /***************************************************************************************** Function: execThread(void (*)(void),int,char *); Description: This function will create a thread from code in the current memory space Notes: 05/19/04 - This does not work the way I want it to it still makes a copy of kernel space so do not use out side of kernel space *****************************************************************************************/ uInt32 execThread(void (* tproc)(void),uInt32 stack,char *arg) { kTask_t * newProcess = 0x0; /* Find A New Thread */ newProcess = schedNewTask(); assert(newProcess); /* Set All The Correct Thread Attributes */ newProcess->cr3 = (uInt32)kernelPageDirectory; newProcess->iframe->eip = (uInt32)tproc; newProcess->iframe->flags = 0x206; newProcess->iframe->esp = (uInt32)&newProcess->kernelStack + 0x2000 - sizeof(struct i386_frame); newProcess->iframe->ebp = newProcess->iframe->esp;//stack; newProcess->iframe->user_esp = newProcess->iframe->esp;//stack; newProcess->iframe->user_ss = 0x10; newProcess->iframe->cs = 0x08; newProcess->iframe->es = 0x10; newProcess->iframe->ss = 0x10; newProcess->iframe->ds = 0x10; newProcess->iframe->fs = 0x10; newProcess->iframe->gs = 0x10; kprintf("ESP: [0x%X:0x%X]\n", newProcess->iframe->user_esp, newProcess->iframe); /* Set up default stack for thread here filled with arg list 3 times */ asm volatile( "pusha \n" "movl %%esp,%%ecx \n" "movl %1,%%eax \n" "movl %%eax,%%esp \n" "pushl %0 \n" "pushl %0 \n" "pushl %0 \n" "movl %%esp,%%eax \n" "movl %%eax,%1 \n" "movl %%ecx,%%esp \n" "popa \n" : : "b" (arg),"m" (newProcess->iframe->user_esp) : "eax" ); newProcess->iframe->esp = newProcess->iframe->user_esp; kprintf("ESP: [0x%X:0x%X]\n", newProcess->iframe->esp, newProcess->iframe); /* Put new thread into the READY state */ sched_setStatus(newProcess->id,READY); /* Return with the new process ID */ return((uInt32)newProcess); } /***************************************************************************************** Function: void execFile(char *file); Description: This Function Executes A Kile Into A New VM Space With Out Having To Fork Notes: 07/30/02 - I Have Made Some Heavy Changes To This As Well As Fixed A Few Memory Leaks The Memory Allocated To Load The Binary Into Is Now Unmapped So It Can Be Used Again And Not Held Onto Until The Program Exits 07/30/02 - Now I Have To Make A Better Memory Allocator So We Can Set Up The Freshly Allocated Pages With The Correct Permissions *****************************************************************************************/ void execFile(char *file,int argc,char **argv,int console) { int i = 0x0; int x = 0x0; kTask_t *tmpTask = 0x0; fileDescriptor *tmpFd = 0x0; elfHeader *binaryHeader = 0x0; elfProgramHeader *programHeader = 0x0; /* Get A New Task For This Proccess */ tmpTask = _current; _current = schedNewTask(); assert(_current); _current->gid = 0x0; _current->uid = 0x0; _current->term = tty_find(console); if (_current->term == 0x0) kpanic("Error: invalid console\n"); /* Set tty ownership */ _current->term->owner = _current->id; /* Now We Must Create A Virtual Space For This Proccess To Run In */ _current->cr3 = (uInt32)vmmCreateVirtualSpace(_current->id); /* To Better Load This Application We Will Switch Over To Its VM Space */ asm volatile( "movl %0,%%eax \n" "movl %%eax,%%cr3 \n" : : "d" ((uInt32 *)(_current->cr3)) ); /* Lets Find The File */ tmpFd = fopen(file,"r"); /* If We Dont Find the File Return */ if (tmpFd == 0x0) { // kprintf("Exec Format Error: Binary File Not Executable.\n"); fclose(tmpFd); return; } if (tmpFd->perms == 0x0) { //kprintf("Exec Format Error: Binary File Not Executable.\n"); fclose(tmpFd); return; } /* Load ELF Header */ binaryHeader = (elfHeader *)kmalloc(sizeof(elfHeader)); fread(binaryHeader,sizeof(elfHeader),1,tmpFd); /* Check If App Is A Real Application */ if ((binaryHeader->eIdent[1] != 'E') && (binaryHeader->eIdent[2] != 'L') && (binaryHeader->eIdent[3] != 'F')) { //kprintf("Exec Format Error: Binary File Not Executable.\n"); kfree(binaryHeader); fclose(tmpFd); return; } else if (binaryHeader->eType != 2) { //kprintf("Exec Format Error: Binary File Not Executable.\n"); kfree(binaryHeader); fclose(tmpFd); return; } else if (binaryHeader->eEntry == 0x300000) { //kprintf("Exec Format Error: Binary File Not Executable.\n"); kfree(binaryHeader); fclose(tmpFd); return; } /* Load The Program Header(s) */ programHeader = (elfProgramHeader *)kmalloc(sizeof(elfProgramHeader)*binaryHeader->ePhnum); fseek(tmpFd,binaryHeader->ePhoff,0); fread(programHeader,(sizeof(elfProgramHeader)*binaryHeader->ePhnum),1,tmpFd); /* Loop Through The Header And Load Sections Which Need To Be Loaded */ for (i=0;i<binaryHeader->ePhnum;i++) { if (programHeader[i].phType == 1) { /* Allocate Memory Im Going To Have To Make This Load Memory With Correct Settings so it helps us in the future */ for (x = 0x0;x < (programHeader[i].phMemsz+4095);x += 0x1000) { /* Make readonly and read/write !!! */ if (vmm_remapPage(vmmFindFreePage(_current->id),((programHeader[i].phVaddr & 0xFFFFF000) + x),PAGE_DEFAULT) == 0x0) kpanic("Error: vmmFindFreePage Failed\n"); memset((void *)((programHeader[i].phVaddr & 0xFFFFF000) + x),0x0,0x1000); } _current->oInfo.vmStart = ((programHeader[i].phVaddr & 0xFFFFF000) + 0x1900000); /* Now Load Section To Memory */ fseek(tmpFd,programHeader[i].phOffset,0); fread((void *)programHeader[i].phVaddr,programHeader[i].phFilesz,1,tmpFd); if ((programHeader[i].phFlags & 0x2) != 0x2) { for (x = 0x0;x < (programHeader[i].phMemsz+4095);x += 0x1000) { if ((vmm_setPageAttributes((programHeader[i].phVaddr & 0xFFFFF000) + x,PAGE_PRESENT | PAGE_USER)) != 0x0) kpanic("Error: vmm_setPageAttributes failed, File: %s, Line: %i\n",__FILE__,__LINE__); } } } } /* Set Virtual Memory Start */ _current->oInfo.vmStart = ((programHeader[i].phVaddr & 0xFFFFF000) + 0x1900000); /* Task Stack 0x2000 bytes long */ vmm_remapPage(vmmFindFreePage(_current->id),0x5DC000,PAGE_DEFAULT | PAGE_STACK); vmm_remapPage(vmmFindFreePage(_current->id),0x5DB000,PAGE_DEFAULT | PAGE_STACK); /* Kernel Stack 0x2000 bytes long */ vmm_remapPage(vmmFindFreePage(_current->id),0x5CC000,KERNEL_PAGE_DEFAULT | PAGE_STACK); vmm_remapPage(vmmFindFreePage(_current->id),0x5CB000,KERNEL_PAGE_DEFAULT | PAGE_STACK); /* Set All The Proper Information For The Task */ /* old _current->tss.back_link = 0x0; _current->tss.esp0 = 0x5CC000; _current->tss.ss0 = 0x10; _current->tss.esp1 = 0x0; _current->tss.ss1 = 0x0; _current->tss.esp2 = 0x0; _current->tss.ss2 = 0x0; _current->tss.eip = (long)binaryHeader->eEntry; _current->tss.eflags = 0x206; _current->tss.esp = 0x5DD000-12; _current->tss.ebp = 0x5DD000; _current->tss.esi = 0x0; _current->tss.edi = 0x0; */ /* Set these up to be ring 3 tasks */ /* old _current->tss.es = 0x30+3; _current->tss.cs = 0x28+3; _current->tss.ss = 0x30+3; _current->tss.ds = 0x30+3; _current->tss.fs = 0x30+3; _current->tss.gs = 0x30+3; _current->tss.ldt = 0x18; _current->tss.trace_bitmap = 0x0000; _current->tss.io_map = 0x8000; */ _current->iframe->eip = (uInt32)binaryHeader->eEntry; _current->iframe->flags = 0x206; _current->iframe->esp = (uInt32)&_current->kernelStack + 0x2000 - sizeof(struct i386_frame); _current->iframe->ebp = 0x5DD000; _current->iframe->user_esp = 0x5DD000 - 12; _current->iframe->user_ss = 0x30 + 3; _current->iframe->cs = 0x28 + 3; _current->iframe->es = 0x30 + 3; _current->iframe->ss = 0x10 + 0; _current->iframe->ds = 0x30 + 3; _current->iframe->fs = 0x30 + 3; _current->iframe->gs = 0x30 + 3; sched_setStatus(_current->id,READY); kfree(binaryHeader); kfree(programHeader); fclose(tmpFd); /* Switch Back To The Kernels VM Space */ asm volatile( "movl %0,%%eax \n" "movl %%eax,%%cr3 \n" : : "d" ((uInt32 *)(kernelPageDirectory)) ); _current = tmpTask; /* Finally Return */ return; } /***************************************************************************************** Function: void sysExec(); Description: This Is The System Call To Execute A New Task Notes: 04-22-03 - It Now Loads Sections Not The Full File *****************************************************************************************/ void sysExec(char *file,int argc,char **argv) { int i = 0x0; int x = 0x0; uInt32 *tmp = 0x0; uInt32 ldAddr = 0x0; fileDescriptor *tmpFd = 0x0; elfHeader *binaryHeader = 0x0; elfProgramHeader *programHeader = 0x0; elfSectionHeader *sectionHeader = 0x0; elfDynamic *elfDynamicS = 0x0; struct i386_frame *iframe = 0x0; tmpFd = fopen(file,"r"); _current->imageFd = tmpFd; /* If We Dont Find the File Return */ if (tmpFd == 0x0) { kprintf("Couldn't open file %s\n",file); return; } if (tmpFd->perms == 0) { //kprintf("Exec Format Error: Binary File Not Executable.\n"); fclose(tmpFd); return; } /* Load ELF Header */ if ((binaryHeader = (elfHeader *)kmalloc(sizeof(elfHeader))) == 0x0) endTask(_current->id); fread(binaryHeader,sizeof(elfHeader),1,tmpFd); /* Set sectionHeader To Point To Loaded Binary To We Can Gather Info */ /* Check If App Is A Real Application */ if ((binaryHeader->eIdent[1] != 'E') && (binaryHeader->eIdent[2] != 'L') && (binaryHeader->eIdent[3] != 'F')) { //kprintf("Exec Format Error: Binary File Not Executable.\n"); kfree(binaryHeader); fclose(tmpFd); return; } else if (binaryHeader->eType != 2) { //kprintf("Exec Format Error: Binary File Not Executable.\n"); kfree(binaryHeader); fclose(tmpFd); return; } else if (binaryHeader->eEntry == 0x300000) { //kprintf("Exec Format Error: Binary File Not Executable.\n"); kfree(binaryHeader); fclose(tmpFd); return; } /* Load The Program Header(s) */ if ((programHeader = (elfProgramHeader *)kmalloc(sizeof(elfProgramHeader)*binaryHeader->ePhnum)) == 0x0) endTask(_current->id); assert(programHeader); fseek(tmpFd,binaryHeader->ePhoff,0); fread(programHeader,(sizeof(elfProgramHeader)*binaryHeader->ePhnum),1,tmpFd); if ((sectionHeader = (elfSectionHeader *)kmalloc(sizeof(elfSectionHeader)*binaryHeader->eShnum)) == 0x0) endTask(_current->id); assert(sectionHeader); fseek(tmpFd,binaryHeader->eShoff,0); fread(sectionHeader,sizeof(elfSectionHeader)*binaryHeader->eShnum,1,tmpFd); /* Loop Through The Header And Load Sections Which Need To Be Loaded */ for (i=0;i<binaryHeader->ePhnum;i++) { if (programHeader[i].phType == 1) { /* Allocate Memory Im Going To Have To Make This Load Memory With Correct Settings so it helps us in the future */ for (x=0;x<(programHeader[i].phMemsz+4095);x += 0x1000) { /* Make readonly and read/write !!! */ if (vmm_remapPage(vmmFindFreePage(_current->id),((programHeader[i].phVaddr & 0xFFFFF000) + x),PAGE_DEFAULT) == 0x0) kpanic("Error: vmmFindFreePage Failed\n"); memset((void *)((programHeader[i].phVaddr & 0xFFFFF000) + x),0x0,0x1000); } _current->oInfo.vmStart = ((programHeader[i].phVaddr & 0xFFFFF000) + 0x1900000); /* Now Load Section To Memory */ fseek(tmpFd,programHeader[i].phOffset,0); fread((void *)programHeader[i].phVaddr,programHeader[i].phFilesz,1,tmpFd); if ((programHeader[i].phFlags & 0x2) != 0x2) { for (x = 0x0;x < (programHeader[i].phMemsz+4095);x += 0x1000) { if ((vmm_setPageAttributes((programHeader[i].phVaddr & 0xFFFFF000) + x,PAGE_PRESENT | PAGE_USER)) != 0x0) kpanic("Error: vmm_setPageAttributes failed, File: %s,Line: %i\n",__FILE__,__LINE__); } } } else if (programHeader[i].phType == 2) { //newLoc = (char *)programHeader[i].phVaddr; elfDynamicS = (elfDynamic *)programHeader[i].phVaddr; fseek(tmpFd,programHeader[i].phOffset,0); fread((void *)programHeader[i].phVaddr,programHeader[i].phFilesz,1,tmpFd); } else if (programHeader[i].phType == 3) { ldAddr = ldEnable(); } } if (elfDynamicS != 0x0) { for (i=0;i<12;i++) { if (elfDynamicS[i].dynVal == 0x3) { tmp = (uInt32 *)elfDynamicS[i].dynPtr; if (tmp == 0x0) kpanic("tmp: NULL\n"); tmp[2] = (uInt32)ldAddr; tmp[1] = (uInt32)tmpFd; break; } } } /* Adjust iframe */ /* old tmp = (uInt32 *)_current->tss.esp0 - 5; tmp[0] = binaryHeader->eEntry; tmp[3] = 0x5DD000-12; */ _current->iframe->eip = binaryHeader->eEntry; _current->iframe->user_esp = 0x5DD000-12; tmp = (uInt32 *)0x5DD000 - 2; tmp[0] = argc; tmp[1] = (uInt32)argv; /* Now That We Relocated The Binary We Can Unmap And Free Header Info */ iframe = (struct i386_frame *)_current->kernelStack;//(_current->kernelStack + 0x1000) - sizeof(struct i386_frame); memset(iframe,0x0,sizeof(struct i386_frame)); iframe->eip = (uInt32)binaryHeader->eEntry; iframe->flags = 0x206; iframe->esp = (uInt32)&_current->kernelStack + 0x2000 - sizeof(struct i386_frame); iframe->ebp = 0x5DD000; iframe->user_esp = 0x5DD000 - 12; iframe->user_ss = 0x30 + 3; iframe->cs = 0x28 + 3; iframe->es = 0x30 + 3; iframe->ss = 0x10 + 0; iframe->ds = 0x30 + 3; iframe->fs = 0x30 + 3; iframe->gs = 0x30 + 3; kfree(binaryHeader); kfree(programHeader); asm( "mov %0,%%esp\n" "pop %%gs \n" "pop %%fs \n" "pop %%es \n" "pop %%ds \n" "pop %%ss \n" "popa \n" "iret \n" : : "g" (iframe) ); return; } /*** $Log$ Revision 1.69 2004/08/26 22:51:18 reddawg TCA touched me :( i think he likes men.... sched.h: kTask_t added parentPid endtask.c: fixed term back to parentPid exec.c: cleaned warnings fork.c: fixed term to childPid sched.c: clean up for dead tasks systemtask.c: clean up dead tasks kmalloc.c: cleaned up warnings udp.c: cleaned up warnings bot.c: cleaned up warnings shell.c: cleaned up warnings tcpdump.c: took a dump hd.c: cleaned up warnings ubixfs.c: stopped prning debug info Revision 1.68 2004/08/25 22:02:41 reddawg task switching - We now are using software switching to be consistant with the rest of the world because all of this open source freaks gave me a hard time about something I liked. There doesn't seem to be any gain in performance but it is working fine and flawlessly Revision 1.67 2004/08/24 05:24:37 reddawg TCA Is A BONER!!!! Revision 1.66 2004/08/21 23:47:50 reddawg *** empty log message *** Revision 1.65 2004/08/21 20:21:20 reddawg *** empty log message *** Revision 1.64 2004/08/21 20:06:28 reddawg ok check out exec.c Revision 1.63 2004/08/21 17:36:57 reddawg updates: converted to software task switching however it is not working yet Revision 1.62 2004/08/14 11:23:02 reddawg Changes Revision 1.61 2004/08/09 12:58:05 reddawg let me know when you got the surce Revision 1.60 2004/08/06 22:32:16 reddawg Ubix Works Again Revision 1.58 2004/08/04 08:17:57 reddawg tty: we have primative ttys try f1-f5 so it is easier to use and debug ubixos Revision 1.57 2004/08/02 17:38:24 reddawg Cleaned up self inflicted bug that messed up argc,argv Revision 1.56 2004/07/28 17:07:25 reddawg MPI: moved the syscalls Revision 1.55 2004/07/28 15:05:43 reddawg Major: Pages now have strict security enforcement. Many null dereferences have been resolved. When apps loaded permissions set for pages rw and ro Revision 1.54 2004/07/28 00:24:20 reddawg removed debug code Revision 1.53 2004/07/28 00:17:05 reddawg Major: Disconnected page 0x0 from the system... Unfortunately this broke many things all of which have been fixed. This was good because nothing deferences NULL any more. Things affected: malloc,kmalloc,getfreepage,getfreevirtualpage,pagefault,fork,exec,ld,ld.so,exec,file Revision 1.52 2004/07/27 07:27:50 reddawg chg: I was fooled thought we failed but it was a casting issue Revision 1.51 2004/07/27 07:25:09 reddawg chg: I should of made it print out stackAddr Revision 1.50 2004/07/27 07:17:50 reddawg chg: verify location of stack before building process Revision 1.49 2004/07/24 23:04:44 reddawg Changes... mark let me know if you fault at pid 185 when you type stress Revision 1.48 2004/07/22 19:27:36 reddawg Did i forget to commit Revision 1.47 2004/07/22 19:18:14 reddawg Fixed some pointer arithmatic which isn't causing any troubles marky mark who can't code rock hard Revision 1.46 2004/07/22 18:46:16 reddawg Fixed up exec.c Revision 1.45 2004/07/22 17:32:25 reddawg I broke it hopefully Revision 1.44 2004/07/20 22:29:55 reddawg assert: remade assert Revision 1.43 2004/07/20 21:35:09 reddawg Let me commit before we start to overlap Revision 1.42 2004/07/18 05:24:15 reddawg Fixens Revision 1.41 2004/07/17 03:10:18 reddawg Added asserts no problems thusfar Revision 1.40 2004/06/28 23:12:58 reddawg file format now container:/path/to/file Revision 1.39 2004/06/18 15:29:38 reddawg zero out memory Revision 1.38 2004/06/18 15:18:04 reddawg bug fixes: did some double checking on pointers and 0x0 out memory Revision 1.37 2004/06/17 13:05:14 reddawg dynamic linking: fixed int6 issue problem was multiple rel's Revision 1.36 2004/06/17 12:28:56 reddawg debug: show me the output after this is a tested Revision 1.35 2004/06/17 02:58:49 reddawg Cleaned Out Dead Code Revision 1.34 2004/06/17 02:19:29 reddawg Cleaning out dead code Revision 1.32 2004/06/17 02:00:53 reddawg Test Revision 1.31 2004/06/17 01:57:52 reddawg Test Revision 1.30 2004/06/17 01:37:00 reddawg TCA: test now Revision 1.29 2004/06/16 17:46:42 reddawg Cleaned Dead Code Revision 1.28 2004/06/16 17:04:13 reddawg ld.so: rest of the commit Revision 1.25 2004/06/15 23:46:05 reddawg Nig Nog Revision 1.24 2004/06/14 13:16:26 reddawg Works now 0x1000 line 272 Revision 1.21 2004/06/14 12:45:12 reddawg Fixed!!! Revision 1.20 2004/06/14 12:40:48 reddawg Try now Revision 1.18 2004/06/14 12:20:54 reddawg notes: many bugs repaired and ld works 100% now. Revision 1.17 2004/06/12 01:27:26 reddawg shared objects: yes we almost fully support shared objects Revision 1.16 2004/06/10 13:08:00 reddawg Minor Bug Fixes Revision 1.15 2004/06/04 13:29:56 reddawg libc: modified mkdir(); interface kpanic: kPanic(); now says kPanic: %s system: now reboots when receives message for reboot also when command start sde is received by system the STD is started Revision 1.14 2004/06/01 00:04:53 reddawg Try now mark Revision 1.13 2004/05/25 22:44:08 reddawg Removed debug code Revision 1.12 2004/05/22 21:46:37 reddawg Fixed some bugs Revision 1.11 2004/05/22 02:10:42 reddawg Added Code To Debug ISsue Revision 1.10 2004/05/21 21:15:04 reddawg Fixed a few bugs which prevented the system from loadin Revision 1.9 2004/05/21 15:34:23 reddawg Fixed a couple of typo Revision 1.8 2004/05/21 15:32:06 reddawg Changed the sys fileExec works it just loads sections needed now instead of fully should be a performance increase of about 40% Revision 1.7 2004/05/19 04:31:12 reddawg Fixed some typedef problems in exec Revision 1.6 2004/05/19 04:07:42 reddawg kmalloc(size,pid) no more it is no kmalloc(size); the way it should of been Revision 1.5 2004/05/19 01:21:29 reddawg Tweaked Revision 1.4 2004/05/15 02:30:28 reddawg Lots of changes Revision 1.3 2004/05/02 03:19:51 reddawg Added Spinlock Provision For SMP Revision 1.2 2004/04/30 14:16:04 reddawg Fixed all the datatypes to be consistant uInt8,uInt16,uInt32,Int8,Int16,Int32 Revision 1.1.1.1 2004/04/15 12:07:18 reddawg UbixOS v1.0 Revision 1.44 2004/04/13 16:16:44 reddawg Changed our copyright, it is all now under a BSD-Style license END ***/