diff --git a/sys/Makefile b/sys/Makefile index 929246e..9605989 100644 --- a/sys/Makefile +++ b/sys/Makefile @@ -37,8 +37,8 @@ init-code: init (cd init;make) -arch-code: ${_ARCH} - (cd ${_ARCH};make) +arch-code: arch/${_ARCH} + (cd arch/${_ARCH};make) kernel-code: kernel (cd kernel;make) @@ -110,7 +110,7 @@ clean: (cd init;make clean) - (cd ${_ARCH};make clean) + (cd arch/${_ARCH};make clean) (cd sys;make clean) (cd kernel;make clean) (cd vmm;make clean) diff --git a/sys/arch/i386/Makefile b/sys/arch/i386/Makefile new file mode 100644 index 0000000..97a86c3 --- /dev/null +++ b/sys/arch/i386/Makefile @@ -0,0 +1,28 @@ +# (C) 2002 The UbixOS Project +# $Id: Makefile 202 2016-01-23 15:21:35Z reddawg $ + +# Include Global 'Source' Options +include ../../../Makefile.incl +include ../../Makefile.incl + +# Objects +OBJS = support.o strcpy.o strcmp.o strncmp.o memset.o memcmp.o schedyield.o kpanic.o timer.o spinlock.o i386_exec.o sys_call_posix.o sys_call.o bioscall.o fork.o systemtask.o sched.o cpu.o trap.o bios16code.o +# ap-boot.o smp.o vitals.o(obsolete) + +all: $(OBJS) + +# Compile Types +.cc.o: + $(CXX) -DNOBOOL $(CFLAGS) $(INCLUDES) -c -o $@ $< +.cc.s: + $(CXX) -DNOBOOL $(CFLAGS) $(INCLUDES) -S -o $@ $< +.c.o: + $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< +.c.s: + $(CC) $(CFLAGS) $(INCLUDES) -S -o $@ $< +.S.o: + $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< + +# Clean up the junk +clean: + $(REMOVE) $(OBJS) diff --git a/sys/arch/i386/ap-boot.S b/sys/arch/i386/ap-boot.S new file mode 100644 index 0000000..c08bd1e --- /dev/null +++ b/sys/arch/i386/ap-boot.S @@ -0,0 +1,133 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + +/* + * Okay, this file contains the code that's going to bootstrap the AP cpus + */ + + + .globl ap_trampoline_start,ap_trampoline_end + .text + .code16 +ap_trampoline_start: + cli + cld + + movw %cs,%ax // The CPU knows its CS already, so lets use it for the other segments + movw %ax,%ds + movw %ax,%es + movw %ax,%ss + + // Do some bochs-specific bullshit + mov $0x31,%al // '1' + mov $0xe9,%dx + outb %al,%dx + //lgdt ap_gdt; + lgdt ap_trampoline_gdt_limit - ap_trampoline_start + movl %cr0,%eax + orl $0x1,%eax + movl %eax,%cr0 // PMODE! + +.code32 + .byte 0x66 + ljmp $0x08,$(ap_trampoline_32 - ap_trampoline_start) // 0x08 == KERNEL_CS + +ap_trampoline_32: + mov $0x32,%al // '2' + mov $0xe9,%dx + outb %al,%dx + + mov $0x10,%ax + mov %ax,%ds + mov %ax,%es + mov %ax,%fs + mov %ax,%gs + mov %ax,%ss + + // Spinlock + mov ap_trampoline_spl - ap_trampoline_start,%edi +ap_spl: + //cmp $1,(%edi) + //je ap_spl + + mov $1,%eax // Value to be set + xchgl (%edi),%eax + cmp $0,%eax + je ap_spl + // /Spinlock + + mov $0x30,%al // '0' + mov $0xe9,%dx + outb %al,%dx + + mov ap_trampoline_stackptr - ap_trampoline_start,%ebx + mov %ebx,%esp + add $0x1000,%ebx + mov %ebx,ap_trampoline_stackptr - ap_trampoline_start + + mov $0x31,%al // '1' + mov $0xe9,%dx + outb %al,%dx + + // spinunlock + mov $0,%eax + mov ap_trampoline_spl - ap_trampoline_start,%edi + xchgl (%edi),%eax + // /spinunlock + + mov $0x33,%al // '3' + mov $0xe9,%dx + outb %al,%dx + + mov ap_trampoline_epoint,%eax + call *%eax +1: + hlt + jmp 1b // Halt if we ever get here somehow + + // Stack.. This sucks, since CPU initialization isn't serialized +ap_trampoline_stackptr: + .long 0x10000 // 256KB +ap_trampoline_epoint: + .long c_ap_boot + +ap_trampoline_spl: + .long 0 +ap_gdt: + .long ubixGDT + + // GDT +ap_trampoline_gdt: + .word 0 +ap_trampoline_gdt_limit: + .word 128 // Room for 32 descriptors +ap_trampoline_gdt_base: + .long 0x20000 // 128KB (move this later) + + +ap_trampoline_end: diff --git a/sys/arch/i386/bios16code.S b/sys/arch/i386/bios16code.S new file mode 100644 index 0000000..b86ee01 --- /dev/null +++ b/sys/arch/i386/bios16code.S @@ -0,0 +1,5 @@ +.globl bios16Code +bios16Code: +.code16 +int $0x10 +int $0x69 diff --git a/sys/arch/i386/bioscall.c b/sys/arch/i386/bioscall.c new file mode 100644 index 0000000..e3d3094 --- /dev/null +++ b/sys/arch/i386/bioscall.c @@ -0,0 +1,90 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void biosCall(int biosInt, int eax, int ebx, int ecx, int edx, int esi, int edi, int es, int ds) { + short segment = 0x0, offset = 0x0; + uint32_t tmpAddr = (uint32_t) &bios16Code; + kTask_t *newProcess = 0x0; + + offset = tmpAddr & 0xF; // lower 4 bits + segment = tmpAddr >> 4; + + newProcess = schedNewTask(); + assert(newProcess); + + newProcess->tss.back_link = 0x0; + newProcess->tss.esp0 = (uint32_t) vmm_getFreeKernelPage(newProcess->id, 2) + (0x2000 - 0x4); // XXX I had 0xDEADBEEF I'm not sure why + newProcess->tss.ss0 = 0x10; + newProcess->tss.esp1 = 0x0; + newProcess->tss.ss1 = 0x0; + newProcess->tss.esp2 = 0x0; + newProcess->tss.ss2 = 0x0; + newProcess->tss.cr3 = kernelPageDirectory; //vmm_createVirtualSpace(newProcess->id); //(uint32_t)_current->tss.cr3; + newProcess->tss.eip = offset & 0xFFFF; + newProcess->tss.eflags = 2 | EFLAG_IF | EFLAG_VM; + newProcess->tss.eax = eax & 0xFFFF; + newProcess->tss.ebx = ebx & 0xFFFF; + newProcess->tss.ecx = ecx & 0xFFFF; + newProcess->tss.edx = edx & 0xFFFF; + newProcess->tss.esp = 0x1000 & 0xFFFF; + newProcess->tss.ebp = 0x1000 & 0xFFFF; + newProcess->tss.esi = esi & 0xFFFF; + newProcess->tss.edi = edi & 0xFFFF; + newProcess->tss.es = es & 0xFFFF; + newProcess->tss.cs = segment & 0xFFFF; + newProcess->tss.ss = 0x1000 & 0xFFFF; + newProcess->tss.ds = ds & 0xFFFF; + newProcess->tss.fs = 0x0 & 0xFFFF; + newProcess->tss.gs = 0x0 & 0xFFFF; + newProcess->tss.ldt = 0x0 & 0xFFFF; + newProcess->tss.trace_bitmap = 0x0 & 0xFFFF; + newProcess->tss.io_map = 0x0 & 0xFFFF; + newProcess->tss.io_map = sizeof(struct tssStruct) - 8192; + newProcess->oInfo.v86Task = 0x1; + + kprintf("EIP: [0x%X] 0x%X:0x%X", tmpAddr, newProcess->tss.eip, newProcess->tss.cs); + + newProcess->state = READY; + while (newProcess->state > 0) + sched_yield(); + + kprintf("EIP: [0x%X] 0x%X:0x%X!", tmpAddr, newProcess->tss.eip, newProcess->tss.cs); + kprintf("CALL DONE: %i 0x%X 0x%X!", newProcess->state, newProcess->tss.esp, newProcess->tss.ss); + + return; +} diff --git a/sys/arch/i386/cpu.c b/sys/arch/i386/cpu.c new file mode 100644 index 0000000..ef9e0c0 --- /dev/null +++ b/sys/arch/i386/cpu.c @@ -0,0 +1,29 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + +#include diff --git a/sys/arch/i386/fork.c b/sys/arch/i386/fork.c new file mode 100644 index 0000000..3e222f8 --- /dev/null +++ b/sys/arch/i386/fork.c @@ -0,0 +1,228 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int sys_fork(struct thread *td, struct sys_fork_args *args) { + struct taskStruct *newProcess; + + newProcess = schedNewTask(); + + /* + * + * Initalize New Task Information From Parrent + * + */ + + /* Set CWD */ + memcpy(newProcess->oInfo.cwd, _current->oInfo.cwd, 1024); + + /* Set PPID */ + newProcess->ppid = _current->id; + + /* Set PGRP */ + newProcess->pgrp = _current->pgrp; + + /* Set Up Task State */ + newProcess->tss.eip = td->frame->tf_eip; + newProcess->oInfo.vmStart = _current->oInfo.vmStart; + newProcess->term = _current->term; + newProcess->term->owner = newProcess->id; + newProcess->uid = _current->uid; + newProcess->gid = _current->gid; + newProcess->tss.back_link = 0x0; + newProcess->tss.esp0 = _current->tss.esp0; + newProcess->tss.ss0 = 0x10; + newProcess->tss.esp1 = 0x0; + newProcess->tss.ss1 = 0x0; + newProcess->tss.esp2 = 0x0; + newProcess->tss.ss2 = 0x0; + newProcess->tss.eflags = td->frame->tf_eflags; + newProcess->tss.eax = 0x0; + newProcess->tss.ebx = td->frame->tf_ebx; + newProcess->tss.ecx = td->frame->tf_ecx; + newProcess->tss.edx = td->frame->tf_edx; + newProcess->tss.esi = td->frame->tf_esi; + newProcess->tss.edi = td->frame->tf_edi; + newProcess->tss.ebp = td->frame->tf_ebp; + newProcess->tss.esp = td->frame->tf_esp; + newProcess->tss.cs = td->frame->tf_cs; // & 0xFF; + newProcess->tss.ss = td->frame->tf_ss; // & 0xFF; + newProcess->tss.ds = td->frame->tf_ds; //_current->tss.ds & 0xFF; + newProcess->tss.fs = td->frame->tf_fs; //_current->tss.fs & 0xFF; + newProcess->tss.gs = _current->tss.gs & 0xFF; + newProcess->tss.es = td->frame->tf_es; //_current->tss.es & 0xFF; + newProcess->tss.ldt = 0x18; + newProcess->tss.trace_bitmap = 0x0000; + newProcess->tss.io_map = 0x8000; + + newProcess->td.vm_tsize = _current->td.vm_tsize; + newProcess->td.vm_taddr = _current->td.vm_taddr; + newProcess->td.vm_dsize = _current->td.vm_dsize; + newProcess->td.vm_daddr = _current->td.vm_daddr; + + //kprintf("Copying Mem Space! [0x%X:0x%X:0x%X:0x%X:0x%X:%i:%i]\n", newProcess->tss.esp0, newProcess->tss.esp, newProcess->tss.ebp, td->frame->tf_esi, td->frame->tf_eip, newProcess->id, _current->id); + + newProcess->tss.cr3 = (uInt32) vmm_copyVirtualSpace(newProcess->id); + //kprintf( "Copied Mem Space! [0x%X]\n", newProcess->tss.cr3 ); + + newProcess->state = FORK; + /* Fix gcc optimization problems */ + while (newProcess->state == FORK) + sched_yield(); + + newProcess->parent = _current; + _current->children++; + + /* Return Id of Proccess */ + td->td_retval[0] = newProcess->id; + return (0); + +} + +/***************************************************************************************** + Functoin: static int fork_copyProcess(struct taskStruct *newProcess,long ebp,long edi, + long esi, long none,long ebx,long ecx,long edx,long eip,long cs,long eflags, + long esp,long ss) + + Desc: This function will copy a process + + Notes: + + *****************************************************************************************/ + +/* Had to remove static though tihs function is only used in this file */ +int fork_copyProcess(struct taskStruct *newProcess, long ebp, long edi, long esi, long none, long ebx, long ecx, long edx, long eip, long cs, long eflags, long esp, long ss) { + volatile struct taskStruct * tmpProcPtr = newProcess; + assert(newProcess); + assert(_current); + + /* Set Up New Tasks Information */ + memcpy(newProcess->oInfo.cwd, _current->oInfo.cwd, 1024); + //kprintf( "Initializing New CWD!\n" ); + + newProcess->tss.eip = eip; + newProcess->oInfo.vmStart = _current->oInfo.vmStart; + newProcess->term = _current->term; + newProcess->term->owner = newProcess->id; + newProcess->uid = _current->uid; + newProcess->gid = _current->gid; + newProcess->tss.back_link = 0x0; + newProcess->tss.esp0 = _current->tss.esp0; + newProcess->tss.ss0 = 0x10; + newProcess->tss.esp1 = 0x0; + newProcess->tss.ss1 = 0x0; + newProcess->tss.esp2 = 0x0; + newProcess->tss.ss2 = 0x0; + newProcess->tss.eflags = eflags; + newProcess->tss.eax = 0x0; + newProcess->tss.ebx = ebx; + newProcess->tss.ecx = ecx; + newProcess->tss.edx = edx; + newProcess->tss.esi = esi; + newProcess->tss.edi = edi; + newProcess->tss.ebp = ebp; + newProcess->tss.esp = esp; + newProcess->tss.cs = cs & 0xFF; + newProcess->tss.ss = ss & 0xFF; + newProcess->tss.ds = _current->tss.ds & 0xFF; + newProcess->tss.fs = _current->tss.fs & 0xFF; + newProcess->tss.gs = _current->tss.gs & 0xFF; + newProcess->tss.es = _current->tss.es & 0xFF; + newProcess->tss.ldt = 0x18; + newProcess->tss.trace_bitmap = 0x0000; + newProcess->tss.io_map = 0x8000; + + newProcess->td.vm_tsize = _current->td.vm_tsize; + newProcess->td.vm_taddr = _current->td.vm_taddr; + newProcess->td.vm_dsize = _current->td.vm_dsize; + newProcess->td.vm_daddr = _current->td.vm_daddr; + + /* Create A Copy Of The VM Space For New Task */ + //MrOlsen 2018kprintf("Copying Mem Space! [0x%X:0x%X:0x%X:0x%X:0x%X:%i:%i:0x%X]\n", newProcess->tss.esp0, newProcess->tss.esp, newProcess->tss.ebp, esi, eip, newProcess->id, _current->id, newProcess->td.vm_daddr); + newProcess->tss.cr3 = (uInt32) vmm_copyVirtualSpace(newProcess->id); + //kprintf( "Copied Mem Space!\n" ); + + newProcess->state = FORK; + + /* Fix gcc optimization problems */ + while (tmpProcPtr->state == FORK) + sched_yield(); + /* Return Id of Proccess */ + kprintf("Returning! [%i]", _current->id); + + return (newProcess->id); +} + +void qT() { + kprintf("qT\n"); +} + +/***************************************************************************************** + Functoin: void sysFork(); + + Desc: This function will fork a new task + + Notes: + + 08/01/02 - This Seems To Be Working Fine However I'm Not Sure If I + Chose The Best Path To Impliment It I Guess We Will See + What The Future May Bring + + *****************************************************************************************/ +//asm volatile( +__asm( + ".globl sysFork_old \n" + "sysFork_old: \n" + " xor %eax,%eax \n" + " call schedNewTask \n" + " testl %eax,%eax \n" + " je fork_ret \n" + " pushl %esi \n" + " pushl %edi \n" + " pushl %ebp \n" + " pushl %eax \n" + " call fork_copyProcess \n" + " movl %eax,(%ebx) \n" + " addl $16,%esp \n" + "fork_ret: \n" + " ret \n" +); + +/*** + END + ***/ + diff --git a/sys/arch/i386/i386_exec.c b/sys/arch/i386/i386_exec.c new file mode 100644 index 0000000..ae96533 --- /dev/null +++ b/sys/arch/i386/i386_exec.c @@ -0,0 +1,996 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ENVP_PAGE 0x100 +#define ARGV_PAGE 0x100 +#define ELF_AUX 0x100 +#define STACK_PAD 0x1000 + +#define ENOEXEC -1 + +#define AT_NULL 0 /* Terminates the vector. */ +#define AT_IGNORE 1 /* Ignored entry. */ +#define AT_EXECFD 2 /* File descriptor of program to load. */ +#define AT_PHDR 3 /* Program header of program already loaded. */ +#define AT_PHENT 4 /* Size of each program header entry. */ +#define AT_PHNUM 5 /* Number of program header entries. */ +#define AT_PAGESZ 6 /* Page size in bytes. */ +#define AT_BASE 7 /* Interpreter's base address. */ +#define AT_FLAGS 8 /* Flags (unused for i386). */ +#define AT_ENTRY 9 /* Where interpreter should transfer control. */ + +#define AUXARGS_ENTRY(pos, id, val) {*pos = id;pos++; *pos = val;pos++;} + +static int argv_count(char **argv) { + int i = 0; + + while (*argv++ != 0x0) + i++; + + return (i); +} + +static int envp_count(char **envp) { + int i = 0; + + while (*envp++ != 0x0) + i++; + + return (i); +} + +static int args_copyin(char **argv_in, char **argv_out, char **args_out) { + + int argc = argv_count(argv_in); + + uint32_t *argv_tmp = (uint32_t *) kmalloc(sizeof(char *) * (argc + 2)); // + 1 For ARGC + 1 For NULL TERM + + char *args_tmp = (char *) kmalloc(ARGV_PAGE); + + argv_tmp[0] = argc; + + uint32_t sp = 0x0; + + int i = 0x0; + + for (i = 1; i <= argc; i++) { + argv_tmp[i] = (uint32_t)(args_tmp + sp); + strcpy((char *)argv_tmp[i], argv_in[i - 1]); + sp += strlen(argv_in[i - 1]) + 1; + } + + argv_tmp[i++] = 0x0; + + *argv_out = (char *)argv_tmp; + *args_out = args_tmp; + + return (0); + +} + +static int envs_copyin(char **envp_in, char **envp_out, char **envs_out) { + + int envc = envp_count(envp_in); + + uint32_t *envp_tmp = (uint32_t *) kmalloc(sizeof(char *) * (envc + 1)); // + 1 For NULL TERM + + char *envs_tmp = (char *) kmalloc(ENVP_PAGE); + + uint32_t sp = 0x0; + + int i = 0x0; + + for (i = 0; i < envc; i++) { + envp_tmp[i] = (uint32_t)(envs_tmp + sp); + strcpy((char *)envp_tmp[i], envp_in[i]); + sp += strlen(envp_in[i]) + 1; + } + envp_tmp[i++] = 0x0; + + *envp_out = (char *)envp_tmp; + *envs_out = envs_tmp; + return (0); +} + +static int elf_parse_dynamic(elf_file_t ef); + +/***************************************************************************************** + + 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_t execThread(void (*tproc)(void), uint32_t stack, char *arg) { + + kTask_t * newProcess = 0x0; + uint32_t stackAddr = 0x0; + + /* Find A New Thread */ + newProcess = schedNewTask(); + assert(newProcess); + + stackAddr = vmm_getFreeKernelPage(newProcess->id, stack / PAGE_SIZE); + + /* Set All The Correct Thread Attributes */ + newProcess->tss.back_link = 0x0; + newProcess->tss.esp0 = 0x0; + newProcess->tss.ss0 = 0x0; + newProcess->tss.esp1 = 0x0; + newProcess->tss.ss1 = 0x0; + newProcess->tss.esp2 = 0x0; + newProcess->tss.ss2 = 0x0; + newProcess->tss.cr3 = (unsigned int) kernelPageDirectory; + newProcess->tss.eip = (unsigned int) tproc; + newProcess->tss.eflags = 0x206; + newProcess->tss.esp = stackAddr + (stack - 0x4); //stack; + newProcess->tss.ebp = 0x0;//stack; + newProcess->tss.esi = 0x0; + newProcess->tss.edi = 0x0; + + /* Ring 3 Selectors */ + /* + newProcess->tss.es = 0x30+3; + newProcess->tss.cs = 0x28+3; + newProcess->tss.ss = 0x30+3; + newProcess->tss.ds = 0x30+3; + newProcess->tss.fs = 0x30+3; + newProcess->tss.gs = 0x30+3; + */ + + /* Ring 0 Selectors */ + newProcess->tss.es = 0x10; + newProcess->tss.cs = 0x08; + newProcess->tss.ss = 0x10; + newProcess->tss.ds = 0x10; + newProcess->tss.fs = 0x10; + newProcess->tss.gs = 0x10; + + newProcess->tss.ldt = 0x18; + newProcess->tss.trace_bitmap = 0x0000; + newProcess->tss.io_map = 0x8000; + newProcess->oInfo.vmStart = 0x6400000; + + if (newProcess->files[0] != 0x0) + kpanic("Problem With File Descriptors"); + + newProcess->files[0] = 0x0; + + //kprintf("EIP: 0x%X(%i)", tproc, newProcess->id); + + /* 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 %%ebx \n" + "pushl %%ebx \n" + "pushl %%ebx \n" + "movl %%esp,%%eax \n" + "movl %%eax,%1 \n" + "movl %%ecx,%%esp \n" + "popa \n" + : + : "b" (arg),"m" (newProcess->tss.esp) + ); + + /* Put new thread into the READY state */ + sched_setStatus(newProcess->id, READY); + + /* Return with the new process ID */ + return ((uint32_t) 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, char **argv, char **envp, int console) { + + kTask_t *newProcess = 0x0; + + int i = 0x0; + int x = 0x0; + + uint32_t *tmp = 0x0; + + Elf_Ehdr *binaryHeader = 0x0; + + Elf_Phdr *programHeader = 0x0; + + int argc = argv_count(argv); + int envc = envp_count(envp); + + /* Get A New Task For This Proccess */ + newProcess = schedNewTask(); + assert(newProcess); + + newProcess->gid = 0x0; + newProcess->uid = 0x0; + newProcess->pgrp = newProcess->id; + newProcess->term = tty_find(console); + + if (newProcess->term == 0x0) + kprintf("Error: invalid console\n"); + + /* Set tty ownership */ + newProcess->term->owner = newProcess->id; + + /* Now We Must Create A Virtual Space For This Proccess To Run In */ + newProcess->tss.cr3 = (uint32_t) vmm_createVirtualSpace(newProcess->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_t *)(newProcess->tss.cr3)) + ); + + /* Lets Find The File */ + if (newProcess->files[0] != 0x0) + kpanic("Problem With File Descriptors"); + newProcess->files[0] = fopen(file, "r"); + + /* If We Dont Find the File Return */ + if (newProcess->files[0] == 0x0) { + kprintf("Exec Format Error: Binary File Not Executable.\n"); + fclose(newProcess->files[0]); + return; + } + + if (newProcess->files[0]->perms == 0x0) { + kprintf("Exec Format Error: Binary File Not Executable.\n"); + fclose(newProcess->files[0]); + return; + } + + /* Load ELF Header */ + binaryHeader = (Elf_Ehdr *) kmalloc(sizeof(Elf_Ehdr)); + + fread(binaryHeader, sizeof(Elf_Ehdr), 1, newProcess->files[0]); + + /* Check If App Is A Real Application */ + if ((binaryHeader->e_ident[1] != 'E') && (binaryHeader->e_ident[2] != 'L') && (binaryHeader->e_ident[3] != 'F')) { + kprintf("Exec Format Error: Binary File Not Executable.\n"); + kfree(binaryHeader); + fclose(newProcess->files[0]); + return; + } + else if (binaryHeader->e_type != 2) { + kprintf("Exec Format Error: Binary File Not Executable.\n"); + kfree(binaryHeader); + fclose(newProcess->files[0]); + return; + } + else if (binaryHeader->e_entry == 0x300000) { + kprintf("Exec Format Error: Binary File Not Executable.\n"); + kfree(binaryHeader); + fclose(newProcess->files[0]); + return; + } + + newProcess->td.abi = binaryHeader->e_ident[EI_OSABI]; + + /* Load The Program Header(s) */ + programHeader = (Elf_Phdr *) kmalloc(sizeof(Elf_Phdr) * binaryHeader->e_phnum); + fseek(newProcess->files[0], binaryHeader->e_phoff, 0); + + fread(programHeader, (sizeof(Elf_Phdr) * binaryHeader->e_phnum), 1, newProcess->files[0]); + + /* Loop Through The Header And Load Sections Which Need To Be Loaded */ + for (i = 0; i < binaryHeader->e_phnum; i++) { + if (programHeader[i].p_type == 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].p_memsz); x += 0x1000) { + /* Make readonly and read/write !!! */ + if (vmm_remapPage(vmm_findFreePage(newProcess->id), ((programHeader[i].p_vaddr & 0xFFFFF000) + x), PAGE_DEFAULT, newProcess->id, 0) == 0x0) + K_PANIC("Remap Page Failed"); + + memset((void *) ((programHeader[i].p_vaddr & 0xFFFFF000) + x), 0x0, 0x1000); + + } + + /* Now Load Section To Memory */ + fseek(newProcess->files[0], programHeader[i].p_offset, 0); + + fread((void *) programHeader[i].p_vaddr, programHeader[i].p_filesz, 1, newProcess->files[0]); + + if ((programHeader[i].p_flags & 0x2) != 0x2) { + for (x = 0x0; x < (programHeader[i].p_memsz); x += 0x1000) { + if ((vmm_setPageAttributes((programHeader[i].p_vaddr & 0xFFFFF000) + x, PAGE_PRESENT | PAGE_USER)) != 0x0) + kpanic("Error: vmm_setPageAttributes failed, File: %s, Line: %i\n", __FILE__, __LINE__); + } + } + } + } + + /* Set Virtual Memory Start */ + newProcess->oInfo.vmStart = 0x80000000; + newProcess->td.vm_daddr = (u_long) (programHeader[i].p_vaddr & 0xFFFFF000); + + /* Set Up Stack Space */ + //MrOlsen (2016-01-14) FIX: is the stack start supposed to be addressable xhcnage x= 1 to x=0 + //x = 0 because GS= stack address not address -1 fix! + for (x = 1; x <= 100; x++) { + vmm_remapPage(vmm_findFreePage(newProcess->id), (STACK_ADDR+1) - (x * PAGE_SIZE), PAGE_DEFAULT | PAGE_STACK, newProcess->id, 0); + bzero((void *)((STACK_ADDR+1) - (x * PAGE_SIZE)), PAGE_SIZE); + } + + /* Kernel Stack 0x2000 bytes long */ + + //vmm_remapPage(vmm_findFreePage(newProcess->id), 0x5BC000, KERNEL_PAGE_DEFAULT | PAGE_STACK, newProcess->id); + //vmm_remapPage(vmm_findFreePage(newProcess->id), 0x5BB000, KERNEL_PAGE_DEFAULT | PAGE_STACK, newProcess->id); + /* + for (x = 0; x < 2; x++) + vmm_remapPage(vmm_findFreePage(newProcess->id), 0xFFFFF000 - (PAGE_SIZE * x), KERNEL_PAGE_DEFAULT | PAGE_STACK, newProcess->id, 0); + */ + + /* Set All The Proper Information For The Task */ + newProcess->tss.back_link = 0x0; + newProcess->tss.esp0 = 0xFFFFFFFF; //0x5BC000; + newProcess->tss.ss0 = 0x10; + newProcess->tss.esp1 = 0x0; + newProcess->tss.ss1 = 0x0; + newProcess->tss.esp2 = 0x0; + newProcess->tss.ss2 = 0x0; + newProcess->tss.eip = (long) binaryHeader->e_entry; + newProcess->tss.eflags = 0x206; + newProcess->tss.esp = STACK_ADDR; + newProcess->tss.ebp = 0x0;//STACK_ADDR; + newProcess->tss.esi = 0x0; + newProcess->tss.edi = 0x0; + + /* Set these up to be ring 3 tasks */ + newProcess->tss.es = 0x30 + 3; + newProcess->tss.cs = 0x28 + 3; + newProcess->tss.ss = 0x30 + 3; + newProcess->tss.ds = 0x30 + 3; + newProcess->tss.fs = 0x30 + 3; + newProcess->tss.gs = 0x8 + 3 + 4;//0x50 + 3; //0x30 + 3; + + newProcess->tss.ldt = 0x18; + newProcess->tss.trace_bitmap = 0x0000; + newProcess->tss.io_map = 0x8000; + + //sched_setStatus(newProcess->id, READY); + + kfree(binaryHeader); + kfree(programHeader); + fclose(newProcess->files[0]); + newProcess->files[0] = 0x0; + + tmp = (uint32_t *) newProcess->tss.esp0 - 5; + + tmp[0] = binaryHeader->e_entry; + tmp[3] = STACK_ADDR - 12; + + newProcess->tss.esp = STACK_ADDR - ARGV_PAGE - ENVP_PAGE - ELF_AUX - (argc + 1) - (envc + 1) - STACK_PAD; + + tmp = (uint32_t *) newProcess->tss.esp; + + tmp[0] = argc; + + uint32_t sp = 0x0; + + for (i = 1; i <= argc; i++) { + tmp[i] = STACK_ADDR - ARGV_PAGE + sp; + strcpy((char *) tmp[i], argv[i - 1]); + sp += strlen(argv[i - 1]) + 1; + } + tmp[i++] = 0x0; + + sp = 0; + + for (int x = 0; x < envc; x++) { + tmp[x + i] = STACK_ADDR - ARGV_PAGE - ENVP_PAGE + sp; + strcpy((char *) tmp[x + i], envp[x]); + sp += strlen(envp[x]) + 1; + } + tmp[i + x] = 0x0; + + /* Build LDT For GS and FS */ + vmm_unmapPage(VMM_USER_LDT, 1); + + if (vmm_remapPage(vmm_findFreePage(newProcess->id), VMM_USER_LDT, PAGE_DEFAULT, newProcess->id, 0) == 0x0) { + K_PANIC("Error: Remap Page Failed"); + } + + struct gdtDescriptor *taskLDT = 0x0; + + taskLDT = (struct gdtDescriptor *)(VMM_USER_LDT + sizeof(struct gdtDescriptor)); + uint32_t data_addr = 0x0; + + taskLDT->limitLow = (0xFFFFF & 0xFFFF); + taskLDT->baseLow = (data_addr & 0xFFFF); + taskLDT->baseMed = ((data_addr >> 16) & 0xFF); + taskLDT->access = ((dData + dWrite + dBig + dBiglim + dDpl3) + dPresent) >> 8; + taskLDT->limitHigh = (0xFFFFF >> 16); + taskLDT->granularity = ((dData + dWrite + dBig + dBiglim + dDpl3) & 0xFF) >> 4; + taskLDT->baseHigh = data_addr >> 24; + + + /* Switch Back To The Kernels VM Space */ + asm volatile( + "movl %0,%%eax \n" + "movl %%eax,%%cr3 \n" + : : "d" ((uint32_t *)(kernelPageDirectory)) + ); + + sprintf(newProcess->oInfo.cwd, "/"); + + //MrOlsen 2018 kprintf("execFile Return: 0x%X - %i\n", newProcess->tss.eip, newProcess->id); + + /* Put new thread into the READY state */ + sched_setStatus(newProcess->id, READY); + + //_current = newProcess; + + /* Finally Return */ + return; +} + +int sys_exec(struct thread *td, char *file, char **argv, char **envp) { + + int i = 0x0; + int x = 0x0; + + int argc = argv_count(argv); + int envc = envp_count(envp); + + uint32_t cr3 = 0x0; + + uint32_t *tmp = 0x0; + + uInt32 seg_size = 0x0; + uInt32 seg_addr = 0x0; + + char *interp = 0x0; + uint32_t ldAddr = 0x0; + + fileDescriptor_t *fd = 0x0; + + Elf_Ehdr *binaryHeader = 0x0; + Elf_Phdr *programHeader = 0x0; + Elf_Shdr *sectionHeader = 0x0; + + elf_file_t ef = 0x0; + + u_long text_addr = 0, text_size = 0; + u_long data_addr = 0, data_size = 0; + + struct i386_frame *iFrame = 0x0; + + asm("movl %%cr3, %0;" : "=r" (cr3)); + + fd = fopen(file, "r"); + + if (fd == 0x0) { + td->td_retval[0] = 2; + return (-1); + } + + /* Test If Executable */ + if (fd->perms == 0) { + kprintf("Exec Format Error: Binary File Not Executable.\n"); + fclose(fd); + return (-1); + } + + /* Set Threads FD to open FD */ + _current->files[0] = fd; + + /* Copy In ARGS & ENVS Before Cleaning Virtual Space */ + uint32_t *argv_out = 0x0; + char *args_out = 0x0; + + args_copyin(argv, (char **)&argv_out, &args_out); + + uint32_t *envp_out = 0x0; + char *envs_out = 0x0; + + envs_copyin(envp, (char **)&envp_out, &envs_out); + + //! Clean the virtual of COW pages left over from the fork + //vmm_cleanVirtualSpace( (uint32_t) _current->td.vm_daddr + (_current->td.vm_dsize << PAGE_SHIFT) ); + //MrOlsen 2017-12-15 - FIX! - This should be done before it was causing a lot of problems why did I free space after loading binary???? + //vmm_cleanVirtualSpace((uint32_t) 0x8048000); + vmm_cleanVirtualSpace((uint32_t) VMM_USER_START); + + /* Clear Stack */ + //bzero(STACK_ADDR - (100 * PAGE_SIZE), (PAGE_SIZE * 100)); + for (x = 1; x <= 100; x++) { + vmm_remapPage(vmm_findFreePage(_current->id), (STACK_ADDR+1) - (x * 0x1000), PAGE_DEFAULT | PAGE_STACK, _current->id, 0); + bzero((void *)((STACK_ADDR+1) - (x * 0x1000)), 0x1000); + } + + /* Load ELF Header */ + if ((binaryHeader = (Elf_Ehdr *) kmalloc(sizeof(Elf_Ehdr))) == 0x0) + K_PANIC("MALLOC FAILED"); + + fread(binaryHeader, sizeof(Elf_Ehdr), 1, fd); + /* Done Loading ELF Header */ + + /* Check If App Is A Real Application */ + if ((binaryHeader->e_ident[1] != 'E') && (binaryHeader->e_ident[2] != 'L') && (binaryHeader->e_ident[3] != 'F')) { + kprintf("Exec Format Error: Binary File Not Executable.\n"); + kfree(binaryHeader); + fclose(fd); + return (-1); + } + else if (binaryHeader->e_type != ET_EXEC) { + kprintf("Exec Format Error: Binary File Not Executable.\n"); + kfree(binaryHeader); + fclose(fd); + return (-1); + } + else if (binaryHeader->e_entry == 0x300000) { + kprintf("Exec Format Error: Binary File Not Executable.\n"); + kfree(binaryHeader); + fclose(fd); + return (-1); + } + + /* Set Thread ABI */ + td->abi = binaryHeader->e_ident[EI_OSABI]; + + /* Load The Program Header(s) */ + if ((programHeader = (Elf_Phdr *) kmalloc(sizeof(Elf_Phdr) * binaryHeader->e_phnum)) == 0x0) + K_PANIC("MALLOC FAILED"); + + assert(programHeader); + + fseek(fd, binaryHeader->e_phoff, 0); + fread(programHeader, (sizeof(Elf_Phdr) * binaryHeader->e_phnum), 1, fd); + /* Done Loading Program Header(s) */ + + /* Load The Section Header(s) */ + if ((sectionHeader = (Elf_Shdr *) kmalloc(sizeof(Elf_Shdr) * binaryHeader->e_shnum)) == 0x0) + K_PANIC("MALLOC FAILED"); + + assert(sectionHeader); + fseek(fd, binaryHeader->e_shoff, 0); + fread(sectionHeader, sizeof(Elf_Shdr) * binaryHeader->e_shnum, 1, fd); + /* Done Loading Section Header(s) */ + + ef = kmalloc(sizeof(struct elf_file)); + memset(ef, 0x0, sizeof(struct elf_file)); + + /* Loop Through The Header And Load Sections Which Need To Be Loaded */ + for (i = 0; i < binaryHeader->e_phnum; i++) { + switch (programHeader[i].p_type) { + case PT_LOAD: + if (programHeader[i].p_memsz == 0x0) + break; + + seg_addr = trunc_page(programHeader[i].p_vaddr); + seg_size = round_page(programHeader[i].p_memsz + programHeader[i].p_vaddr - seg_addr); + + /* + 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 < (round_page(programHeader[i].p_memsz)); x += 0x1000) { + /* Make readonly and read/write !!! */ + if (vmm_remapPage(vmm_findFreePage(_current->id), ((programHeader[i].p_vaddr & 0xFFFFF000) + x), PAGE_DEFAULT, _current->id, 0) == 0x0) { + K_PANIC("Error: Remap Page Failed"); + } + else { + //MrOlsen 2018-01-15 kprintf("rP[0x%X]", (programHeader[i].p_vaddr & 0xFFFFF000) + x); + } + + memset((void *) ((programHeader[i].p_vaddr & 0xFFFFF000) + x), 0x0, 0x1000); + + } + + /* Now Load Section To Memory */ + fseek(fd, programHeader[i].p_offset, 0); + fread((void *) programHeader[i].p_vaddr, programHeader[i].p_filesz, 1, fd); + + if ((programHeader[i].p_flags & 0x2) != 0x2) { + for (x = 0x0; x < (round_page(programHeader[i].p_memsz)); x += 0x1000) { + if ((vmm_setPageAttributes((programHeader[i].p_vaddr & 0xFFFFF000) + x, PAGE_PRESENT | PAGE_USER)) != 0x0) + kpanic("Error: vmm_setPageAttributes failed, File: %s,Line: %i\n", __FILE__, __LINE__); + } + } + + if ((programHeader[i].p_flags & PF_X) && text_size < seg_size) { + //MrOlsen 2018kprintf("setting text: 0x%X - 0x%X\n", seg_addr, seg_size); + text_size = seg_size; + text_addr = seg_addr; + } + else { + //MrOlsen 2018kprintf("setting data: 0x%X - 0x%X\n", seg_addr, seg_size); + data_size = seg_size; + data_addr = seg_addr; + /* + _current->td.vm_dsize = seg_size >> PAGE_SHIFT; + _current->td.vm_daddr = (char *) seg_addr; + kprintf( "setting daddr: 0x%X, dsiize: 0x%X\n", _current->td.vm_daddr, _current->td.vm_dsize ); + */ + } + + /* + * MrOlsen (2016-01-19) NOTE: Note Sure, I should Do This Later + * Thjis is for stack space + */ + _current->oInfo.vmStart = ((programHeader[i].p_vaddr & 0xFFFFF000) + 0xA900000); + break; + case PT_DYNAMIC: + //newLoc = (char *)programHeader[i].phVaddr; + //elfDynamicS = (elfDynamic *) programHeader[i].p_vaddr; + ef->dynamic = (Elf_Dyn *) programHeader[i].p_vaddr; + //fseek( fd, programHeader[i].phOffset, 0 ); + //fread( (void *) programHeader[i].phVaddr, programHeader[i].phFilesz, 1, fd ); + break; + case PT_INTERP: + #ifdef DEBUG_EXEC + kprintf("%s:%i>Malloc: %i\n", _FILE_,_LINE_,programHeader[i].p_filesz); + #endif + interp = (char *) kmalloc(programHeader[i].p_filesz); + fseek(fd, programHeader[i].p_offset, 0); + fread((void *) interp, programHeader[i].p_filesz, 1, fd); + #ifdef DEBUG_EXEC + kprintf("Interp: [%s]\n", interp); + #endif + ldAddr = ldEnable(interp); + //ef->ld_addr = ldEnable(); + break; + case PT_GNU_STACK: + asm("nop"); + break; + default: + break; + } + } + + _current->td.vm_tsize = text_size >> PAGE_SHIFT; + _current->td.vm_taddr = text_addr; + _current->td.vm_dsize = data_size >> PAGE_SHIFT; + _current->td.vm_daddr = data_addr; + + //MrOlsen 2018kprintf("Done Looping\n"); + + ef->preloaded = 1; + ef->address = 0x0; + elf_parse_dynamic(ef); + + //asm("cld"); + //irqDisable(0); + iFrame = (struct i386_frame *) (_current->tss.esp0 - sizeof(struct i386_frame)); + + //iFrame->ebp = 0x0; + + if (ldAddr != 0x0) { + iFrame->eip = ldAddr; + } + else { + iFrame->eip = binaryHeader->e_entry; + } + + //iFrame->edx = 0x0; + + iFrame->user_esp = (uint32_t) (STACK_ADDR - ARGV_PAGE - ENVP_PAGE - ELF_AUX - (argc + 1) - (envc + 1) - STACK_PAD) & 0xFFFFF000; + + tmp = (uint32_t *) iFrame->user_esp; + +// memset((char *) tmp, 0x0, ARGV_PAGE + ENVP_PAGE + ELF_AUX + (argc + 1) + (envc + 1) + STACK_PAD); + + tmp[0] = argc; + + uint32_t sp = 0x0; + + char *EXECP = 0x0; + + for (i = 1; i <= argc; i++) { + tmp[i] = (uint32_t) STACK_ADDR - ARGV_PAGE + sp; + if (i == 1) { + EXECP = (char *)tmp[i]; + } + strcpy((char *)tmp[i], (const char *)argv_out[i]); + #ifdef EXEC_DEBUG + kprintf("argv[%i]:%s",i, (const char *)argv_out[i]); + #endif + sp += strlen((const char *)argv_out[i]) + 1; + } + + tmp[i++] = 0x0; + + kfree(argv_out); + kfree(args_out); + + sp = 0; + + x = 0; + + for (x = 0; x < envc; x++) { + tmp[x + i] = (uint32_t) STACK_ADDR - ARGV_PAGE - ENVP_PAGE + sp; + strcpy((char *) tmp[x + i], (const char *)envp_out[x]); + sp += strlen((const char *)envp_out[x]) + 1; + } + + tmp[i + x] = 0x0; + + kfree(envp_out); + kfree(envs_out); + + i = i + x + 1; + + struct file *tFP = 0x0; + int tFD = 0x0; + + fseek(_current->files[0], 0x0, 0x0); // Reset File Position + falloc(&_current->td, &tFP, &tFD); + + tFP->fd = _current->files[0]; + + + tmp[i++] = 2; + tmp[i++] = -1;// tFD; // _current->imageFd; + _current->td.o_files[4] = _current->files[0]; + //MrOlsen 2018kprintf("AT_EXECFD: [%i:%i]", tmp[i - 1], tFD); + + tmp[i++] = 3; + tmp[i++] = binaryHeader->e_phoff + 0x08048000; + //MrOlsen 2018kprintf("AT_PHDR: [0x%X]", tmp[i - 1]); + + tmp[i++] = 4; + tmp[i++] = binaryHeader->e_phentsize; + //MrOlsen 2018kprintf("AT_PHENT: [0x%X]", tmp[i - 1]); + + tmp[i++] = 5; + tmp[i++] = binaryHeader->e_phnum; + //MrOlsen 2018kprintf("AT_PHNUM: [0x%X]", tmp[i - 1]); + + tmp[i++] = 6; + tmp[i++] = 0x1000; + + tmp[i++] = 7; + tmp[i++] = LD_START; + //MrOlsen 2018kprintf("AT_BASE: [0x%X]", tmp[i - 1]); + + tmp[i++] = 8; + tmp[i++] = 0x0; + + tmp[i++] = 9; + tmp[i++] = binaryHeader->e_entry; + + tmp[i++] = 11; + tmp[i++] = 0x0; + + tmp[i++] = 12; + tmp[i++] = 0x0; + + tmp[i++] = 13; + tmp[i++] = 0x0; + + tmp[i++] = 14; + tmp[i++] = 0x0; + + tmp[i++] = 15; //EXEC PATH + tmp[i++] = (uint32_t)EXECP; + + tmp[i++] = 19; //NCPUS + tmp[i++] = 0x1; + + tmp[i++] = 23; //STACKPROT + tmp[i++] = 0x3; + + tmp[i++] = 0; + tmp[i++] = 0; + + /* Now That We Relocated The Binary We Can Unmap And Free Header Info */ + kfree(binaryHeader); + kfree(programHeader); + //irqEnable(0); + //asm("sti"); + + /* + _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 = 0x50 + 3; //0x30 + 3; + + _current->tss.ldt = 0x18; + _current->tss.trace_bitmap = 0x0000; + _current->tss.io_map = 0x8000; + */ + + /* + kfree (iFrameNew); + + memAddr = (uint32_t) & (_current->tss); + ubixGDT[4].descriptor.baseLow = (memAddr & 0xFFFF); + ubixGDT[4].descriptor.baseMed = ((memAddr >> 16) & 0xFF); + ubixGDT[4].descriptor.baseHigh = (memAddr >> 24); + ubixGDT[4].descriptor.access = '\x89'; + + ubixGDT[10].descriptor.baseLow = (STACK_ADDR & 0xFFFF); + ubixGDT[10].descriptor.baseMed = ((STACK_ADDR >> 16) & 0xFF); + ubixGDT[10].descriptor.baseHigh = (STACK_ADDR >> 24); + + */ + + /* Build LDT For GS and FS */ + vmm_unmapPage(VMM_USER_LDT, 1); // Can I Free This? + if (vmm_remapPage(vmm_findFreePage(_current->id), VMM_USER_LDT, PAGE_DEFAULT, _current->id, 0) == 0x0) { + K_PANIC("Error: Remap Page Failed"); + } + + struct gdtDescriptor *taskLDT = 0x0; + + taskLDT = (struct gdtDescriptor *)(VMM_USER_LDT + sizeof(struct gdtDescriptor)); + + //data_addr = 0x0; //TEMP + + taskLDT->limitLow = (0xFFFFF & 0xFFFF); + taskLDT->baseLow = (data_addr & 0xFFFF); + taskLDT->baseMed = ((data_addr >> 16) & 0xFF); + taskLDT->access = ((dData + dWrite + dBig + dBiglim + dDpl3) + dPresent) >> 8; + taskLDT->limitHigh = (0xFFFFF >> 16); + taskLDT->granularity = ((dData + dWrite + dBig + dBiglim + dDpl3) & 0xFF) >> 4; + taskLDT->baseHigh = data_addr >> 24; + + _current->tss.gs = 0xF; //Select 0x8 + Ring 3 + LDT + _current->pgrp = _current->id; + + return (0x0); +} + +static int elf_parse_dynamic(elf_file_t ef) { + Elf32_Dyn *dynp; + int plttype = DT_REL; + + for (dynp = ef->dynamic; dynp->d_tag != 0x0; dynp++) { + switch (dynp->d_tag) { + case DT_NEEDED: + asm("nop"); + break; + case DT_INIT: + asm("nop"); + break; + case DT_FINI: + asm("nop"); + break; + case DT_HASH: + asm("nop"); + /* From src/libexec/rtld-elf/rtld.c */ + const Elf_Hashelt *hashtab = (const Elf_Hashelt *) (ef->address + dynp->d_un.d_ptr); + ef->nbuckets = hashtab[0]; + ef->nchains = hashtab[1]; + ef->buckets = hashtab + 2; + ef->chains = ef->buckets + ef->nbuckets; + break; + case DT_STRTAB: + ef->strtab = (caddr_t) (ef->address + dynp->d_un.d_ptr); + break; + case DT_STRSZ: + ef->strsz = dynp->d_un.d_val; + break; + case DT_SYMTAB: + ef->symtab = (Elf_Sym *) (ef->address + dynp->d_un.d_ptr); + break; + case DT_SYMENT: + if (dynp->d_un.d_val != sizeof(Elf32_Sym)) + return (ENOEXEC); + break; + case DT_REL: + ef->rel = (const Elf_Rel *) (ef->address + dynp->d_un.d_ptr); + break; + case DT_RELSZ: + ef->relsize = dynp->d_un.d_val; + break; + case DT_RELENT: + if (dynp->d_un.d_val != sizeof(Elf_Rel)) + return (ENOEXEC); + break; + case DT_JMPREL: + ef->pltrel = (const Elf_Rel *) (ef->address + dynp->d_un.d_ptr); + break; + case DT_PLTRELSZ: + ef->pltrelsize = dynp->d_un.d_val; + break; + case DT_RELA: + ef->rela = (const Elf_Rela *) (ef->address + dynp->d_un.d_ptr); + break; + case DT_RELASZ: + ef->relasize = dynp->d_un.d_val; + break; + case DT_RELAENT: + if (dynp->d_un.d_val != sizeof(Elf_Rela)) + return (ENOEXEC); + break; + case DT_PLTREL: + plttype = dynp->d_un.d_val; + if (plttype != DT_REL && plttype != DT_RELA) + return (ENOEXEC); + break; + case DT_PLTGOT: + ef->got = (Elf_Addr *) (ef->address + dynp->d_un.d_ptr); + /* + tmp = (void *) dynp->d_un.d_ptr; //elfDynamicS[i].dynPtr; + tmp[2] = (uInt32) ef->ld_addr; + tmp[1] = (uInt32) ef; //0x0;//0xBEEFEAD;//STACK_ADDR - 128;//_current->imageFd;//0xBEEFDEAD;//ef; + */ + break; + default: + asm("nop"); + //kprintf("t_tag: 0x%X>", dynp->d_tag); + break; + } + } + + if (plttype == DT_RELA) { + ef->pltrela = (const Elf_Rela *) ef->pltrel; + ef->pltrel = NULL; + ef->pltrelasize = ef->pltrelsize; + ef->pltrelsize = 0; + } + + ef->ddbsymtab = ef->symtab; + ef->ddbsymcnt = ef->nchains; + ef->ddbstrtab = ef->strtab; + ef->ddbstrcnt = ef->strsz; + return (0); +} diff --git a/sys/arch/i386/kpanic.c b/sys/arch/i386/kpanic.c new file mode 100644 index 0000000..4b6ce22 --- /dev/null +++ b/sys/arch/i386/kpanic.c @@ -0,0 +1,66 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + +#include +#include +#include +#include +#include + +/*! + * \brief print panic message and halt system + * + * \param fmt panic message + * + */ +void kpanic(const char *fmt, ...) { + char buf[512]; + va_list args; + + va_start(args, fmt); + vsprintf(buf, fmt, args); + va_end(args); + + /* It's important that we print on the current terminal so let's reset foreground */ + tty_foreground = NULL; + kprintf("kPanic: %s", buf); + + /* Halt The System */ + //asm("cli"); + irqDisable(0x0); + + while (1) { + asm("hlt"); + } + +} + +/*** + END + ***/ + diff --git a/sys/arch/i386/memcmp.S b/sys/arch/i386/memcmp.S new file mode 100644 index 0000000..c8e68c2 --- /dev/null +++ b/sys/arch/i386/memcmp.S @@ -0,0 +1,66 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + + #include + +ENTRY(memcmp) + pushl %edi + pushl %esi + movl 12(%esp),%edi + movl 16(%esp),%esi + cld /* set compare direction forward */ + + movl 20(%esp),%ecx /* compare by words */ + shrl $2,%ecx + repe + cmpsl + jne L5 /* do we match so far? */ + + movl 20(%esp),%ecx /* compare remainder by bytes */ + andl $3,%ecx + repe + cmpsb + jne L6 /* do we match? */ + + xorl %eax,%eax /* we match, return zero */ + popl %esi + popl %edi + ret + +L5: movl $4,%ecx /* We know that one of the next */ + subl %ecx,%edi /* four pairs of bytes do not */ + subl %ecx,%esi /* match. */ + repe + cmpsb +L6: movzbl -1(%edi),%eax /* Perform unsigned comparison */ + movzbl -1(%esi),%edx + subl %edx,%eax + popl %esi + popl %edi + ret +END(memcmp) diff --git a/sys/arch/i386/memset.S b/sys/arch/i386/memset.S new file mode 100644 index 0000000..03bef8d --- /dev/null +++ b/sys/arch/i386/memset.S @@ -0,0 +1,79 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + + #include + + +ENTRY(memset) + pushl %edi + pushl %ebx + movl 12(%esp),%edi + movzbl 16(%esp),%eax /* unsigned char, zero extend */ + movl 20(%esp),%ecx + pushl %edi /* push address of buffer */ + + cld /* set fill direction forward */ + + /* + * if the string is too short, it's really not worth the overhead + * of aligning to word boundries, etc. So we jump to a plain + * unaligned set. + */ + cmpl $0x0f,%ecx + jle L1 + + movb %al,%ah /* copy char to all bytes in word */ + movl %eax,%edx + sall $16,%eax + orl %edx,%eax + + movl %edi,%edx /* compute misalignment */ + negl %edx + andl $3,%edx + movl %ecx,%ebx + subl %edx,%ebx + + movl %edx,%ecx /* set until word aligned */ + rep + stosb + + movl %ebx,%ecx + shrl $2,%ecx /* set by words */ + rep + stosl + + movl %ebx,%ecx /* set remainder by bytes */ + andl $3,%ecx +L1: rep + stosb + + popl %eax /* pop address of buffer */ + popl %ebx + popl %edi + ret +END(memset) diff --git a/sys/arch/i386/sched.c b/sys/arch/i386/sched.c new file mode 100644 index 0000000..069868d --- /dev/null +++ b/sys/arch/i386/sched.c @@ -0,0 +1,385 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +static kTask_t *taskList = 0x0; +static kTask_t *delList = 0x0; +static uint32_t nextID = 1; + +kTask_t *_current = 0x0; +kTask_t *_usedMath = 0x0; + +static struct spinLock schedulerSpinLock = SPIN_LOCK_INITIALIZER; + +int need_resched = 0; + +/************************************************************************ + + Function: int sched_init() + + Description: This function is used to enable the kernel scheduler + + Notes: + + 02/20/2004 - Approved for quality + + ************************************************************************/ + +int sched_init() { + taskList = (kTask_t *) kmalloc(sizeof(kTask_t)); + if (taskList == 0x0) + kpanic("Unable to create task list"); + + taskList->id = nextID++; + + /* Print out information on scheduler */ + kprintf("sched0 - Address: [0x%X]\n", taskList); + + /* Return so we know everything went well */ + return (0x0); +} + +void sched() { + uint32_t memAddr = 0x0; + kTask_t *tmpTask = 0x0; + kTask_t *delTask = 0x0; + + if (spinTryLock(&schedulerSpinLock)) + return; + + tmpTask = ((_current == 0) ? 0 : _current->next); + schedStart: + + /* Yield the next task from the current prio queue */ + for (; tmpTask != 0x0; tmpTask = tmpTask->next) { + if (tmpTask->state == FORK) + tmpTask->state = READY; + + if (tmpTask->state == READY) { + _current->state = (_current->state == DEAD) ? DEAD : READY; + _current = tmpTask; + break; + } + else if (tmpTask->state == DEAD) { + delTask = tmpTask; + if (delTask->parent != 0x0) { + delTask->parent->children -= 1; + delTask->parent->last_exit = delTask->id; + delTask->parent->state = READY; + } + + tmpTask = tmpTask->next; + sched_deleteTask(delTask->id); + sched_addDelTask(delTask); + goto schedStart; + } + } + + /* Finished all the tasks, restarting the list */ + if (0x0 == tmpTask) { + tmpTask = taskList; + goto schedStart; + } + + if (_current->state == READY || _current->state == RUNNING) { + + if (_current->oInfo.v86Task == 0x1) { + irqDisable(0x0); + kprintf("IRQD(%i): 0x%X*0x%X:0x%X@, esp: 0x%X:0x%X, ebp: 0x%X:0x%X ds: 0x%X", _current->id, _current->tss.eflags, _current->tss.cs, _current->tss.eip, _current->tss.ss, _current->tss.esp, _current->tss.ss, _current->tss.ebp,_current->tss.ds); + kprintf("ss0: 0x%X, esp0: 0x%X", _current->tss.ss0, _current->tss.esp0); + } + + asm("cli"); + + memAddr = (uint32_t) &(_current->tss); + ubixGDT[4].descriptor.baseLow = (memAddr & 0xFFFF); + ubixGDT[4].descriptor.baseMed = ((memAddr >> 16) & 0xFF); + ubixGDT[4].descriptor.baseHigh = (memAddr >> 24); + ubixGDT[4].descriptor.access = '\x89'; + + _current->state = RUNNING; + + spinUnlock(&schedulerSpinLock); + + asm("sti"); + asm("ljmp $0x20,$0"); + } + else { + spinUnlock(&schedulerSpinLock); + } + + return; +} + +kTask_t *schedNewTask() { + int i = 0; + + kTask_t *tmpTask = (kTask_t *) kmalloc(sizeof(kTask_t)); + + struct file *fp = 0x0; + + if (tmpTask == 0x0) + kpanic("Error: schedNewTask() - kmalloc failed trying to initialize a new task struct\n"); + + memset(tmpTask, 0x0, sizeof(kTask_t)); + + /* Filling in tasks attrs */ + tmpTask->usedMath = 0x0; + tmpTask->state = NEW; + + /* HACK */ + + for (i = 0; i < 3; i++) { + fp = (void *) kmalloc(sizeof(struct file)); + //kprintf("DB: [0x%X]\n", (uint32_t) fp); + tmpTask->td.o_files[i] = (void *) fp; + fp->f_flag = 0x4; + } + + spinLock(&schedulerSpinLock); + tmpTask->id = nextID++; + tmpTask->next = taskList; + tmpTask->prev = 0x0; + taskList->prev = tmpTask; + taskList = tmpTask; + + spinUnlock(&schedulerSpinLock); + + return (tmpTask); +} + +int sched_deleteTask(pidType id) { + kTask_t *tmpTask = 0x0; + + /* Checking each task from the prio queue */ + for (tmpTask = taskList; tmpTask != 0x0; tmpTask = tmpTask->next) { + if (tmpTask->id == id) { + if (tmpTask->prev != 0x0) + tmpTask->prev->next = tmpTask->next; + if (tmpTask->next != 0x0) + tmpTask->next->prev = tmpTask->prev; + if (taskList == tmpTask) + taskList = tmpTask->next; + + return (0x0); + } + } + return (0x1); +} + +int sched_addDelTask(kTask_t *tmpTask) { + tmpTask->next = delList; + tmpTask->prev = 0x0; + if (delList != 0x0) + delList->prev = tmpTask; + delList = tmpTask; + return (0x0); +} + +kTask_t *sched_getDelTask() { + kTask_t *tmpTask = 0x0; + + if (delList == 0x0) + return (0x0); + + tmpTask = delList; + delList = delList->next; + return (tmpTask); +} + +kTask_t *schedFindTask(uint32_t id) { + kTask_t *tmpTask = 0x0; + + for (tmpTask = taskList; tmpTask; tmpTask = tmpTask->next) { + if (tmpTask->id == id) + return (tmpTask); + } + + return (0x0); +} + +/************************************************************************ + + Function: void schedEndTask() + + Description: This function will end a task + + Notes: + + 02/20/2004 - Approved for quality + + ************************************************************************/ +void schedEndTask(pidType pid) { + endTask(_current->id); + sched_yield(); +} + +/************************************************************************ + + Function: int schedEndTask() + + Description: This function will yield a task + + Notes: + + 02/20/2004 - Approved for quality + + ************************************************************************/ + +void sched_yield() { + sched(); +} + +/* + asm( + ".globl sched_yield \n" + "sched_yield: \n" + " cli \n" + " call sched \n" + ); + */ + +/************************************************************************ + + Function: int sched_setStatus(pidType pid,tState state) + + Description: Change the tasks status + + Notes: + + ************************************************************************/ +int sched_setStatus(pidType pid, tState state) { + kTask_t *tmpTask = schedFindTask(pid); + if (tmpTask == 0x0) + return (0x1); + tmpTask->state = state; + return (0x0); +} + +void add_wait_queue(struct wait_queue ** p, struct wait_queue * wait) { + unsigned long flags; + + save_flags(flags); + cli(); + if (!*p) { + wait->next = wait; + *p = wait; + } + else { + wait->next = (*p)->next; + (*p)->next = wait; + } + restore_flags(flags); +} + +void remove_wait_queue(struct wait_queue ** p, struct wait_queue * wait) { + unsigned long flags; + struct wait_queue * tmp; + + save_flags(flags); + cli(); + if ((*p == wait) && ((*p = wait->next) == wait)) { + *p = NULL; + } + else { + tmp = wait; + while (tmp->next != wait) { + tmp = tmp->next; + } + tmp->next = wait->next; + } + wait->next = NULL; + restore_flags(flags); +} + +void wake_up_interruptible(struct wait_queue **q) { + struct wait_queue *tmp; + kTask_t *p; + + if (!q || !(tmp = *q)) + return; + do { + if ((p = tmp->task) != NULL) { + if (p->state == INTERRUPTIBLE) { + p->state = RUNNING; + if (p->counter > _current->counter) + need_resched = 1; + } + } + if (!tmp->next) { + kprintf("wait_queue is bad (eip = %08lx)\n", ((unsigned long *) q)[-1]); + kprintf(" q = %p\n", q); + kprintf(" *q = %p\n", *q); + kprintf(" tmp = %p\n", tmp); + break; + } + tmp = tmp->next; + } while (tmp != *q); +} + +void wake_up(struct wait_queue **q) { + struct wait_queue *tmp; + kTask_t * p; + + if (!q || !(tmp = *q)) + return; + do { + if ((p = tmp->task) != NULL) { + if ((p->state == UNINTERRUPTIBLE) || (p->state == INTERRUPTIBLE)) { + p->state = RUNNING; + if (p->counter > _current->counter) + need_resched = 1; + } + } + if (!tmp->next) { + kprintf("wait_queue is bad (eip = %08lx)\n", ((unsigned long *) q)[-1]); + kprintf(" q = %p\n", q); + kprintf(" *q = %p\n", *q); + kprintf(" tmp = %p\n", tmp); + break; + } + tmp = tmp->next; + } while (tmp != *q); +} diff --git a/sys/arch/i386/schedyield.S b/sys/arch/i386/schedyield.S new file mode 100644 index 0000000..1072793 --- /dev/null +++ b/sys/arch/i386/schedyield.S @@ -0,0 +1,51 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + +.globl sched_yield_new +.text +.code32 +sched_yield_new: + pusha /* Save all of the registers */ + push %ss + push %ds + push %es + push %fs + push %gs + call sched + mov %eax,%esp + pop %gs + pop %fs + pop %es + pop %ds + pop %ss + popa /* Restore Registers */ + iret + +/*** + END + ***/ diff --git a/sys/arch/i386/spinlock.c b/sys/arch/i386/spinlock.c new file mode 100644 index 0000000..db6653d --- /dev/null +++ b/sys/arch/i386/spinlock.c @@ -0,0 +1,71 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + +#include +#include +#include + +#define atomic_xadd(P, V) __sync_fetch_and_add((P), (V)) +#define cmpxchg(P, O, N) __sync_val_compare_and_swap((P), (O), (N)) +#define atomic_inc(P) __sync_add_and_fetch((P), 1) +#define atomic_dec(P) __sync_add_and_fetch((P), -1) +#define atomic_add(P, V) __sync_add_and_fetch((P), (V)) +#define atomic_set_bit(P, V) __sync_or_and_fetch((P), 1<<(V)) +#define atomic_clear_bit(P, V) __sync_and_and_fetch((P), ~(1<<(V))) + +#define barrier() asm volatile("": : :"memory") + +/* Pause instruction to prevent excess processor bus usage */ +#define cpu_relax() asm volatile("pause\n": : :"memory") + +void spinLockInit(spinLock_t lock) { + memset(lock, 0x0, sizeof(struct spinLock)); +} + +int spinTryLock(spinLock_t lock) { + if (!cmpxchg(&lock->locked, NULL, LLOCK_FLAG)) + return 0; + + /* Failure! */ + return LOCKED; +} + +void spinUnlock(spinLock_t lock) { + barrier(); + lock->locked = 0x0; +} + +void spinLock(spinLock_t lock) { + while (1) { + if (!xchg_32(&lock->locked, LOCKED)) + return; + while (lock->locked == 1) + sched_yield(); + } +} + diff --git a/sys/arch/i386/strcmp.S b/sys/arch/i386/strcmp.S new file mode 100644 index 0000000..19bb2ff --- /dev/null +++ b/sys/arch/i386/strcmp.S @@ -0,0 +1,98 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + + #include + +ENTRY(strcmp) + movl 0x04(%esp),%eax + movl 0x08(%esp),%edx + jmp L2 /* Jump into the loop! */ + + .align 2,0x90 +L1: incl %eax + incl %edx +L2: movb (%eax),%cl + testb %cl,%cl + je L3 + cmpb %cl,(%edx) + jne L3 + incl %eax + incl %edx + movb (%eax),%cl + testb %cl,%cl + je L3 + cmpb %cl,(%edx) + jne L3 + incl %eax + incl %edx + movb (%eax),%cl + testb %cl,%cl + je L3 + cmpb %cl,(%edx) + jne L3 + incl %eax + incl %edx + movb (%eax),%cl + testb %cl,%cl + je L3 + cmpb %cl,(%edx) + jne L3 + incl %eax + incl %edx + movb (%eax),%cl + testb %cl,%cl + je L3 + cmpb %cl,(%edx) + jne L3 + incl %eax + incl %edx + movb (%eax),%cl + testb %cl,%cl + je L3 + cmpb %cl,(%edx) + jne L3 + incl %eax + incl %edx + movb (%eax),%cl + testb %cl,%cl + je L3 + cmpb %cl,(%edx) + jne L3 + incl %eax + incl %edx + movb (%eax),%cl + testb %cl,%cl + je L3 + cmpb %cl,(%edx) + je L1 + .align 2, 0x90 +L3: movzbl (%eax),%eax /* unsigned comparison */ + movzbl (%edx),%edx + subl %edx,%eax + ret +END(strcmp) diff --git a/sys/arch/i386/strcpy.S b/sys/arch/i386/strcpy.S new file mode 100644 index 0000000..e6f6a0c --- /dev/null +++ b/sys/arch/i386/strcpy.S @@ -0,0 +1,73 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + + #include + + ENTRY(strcpy) + movl 4(%esp),%ecx /* dst address */ + movl 8(%esp),%edx /* src address */ + pushl %ecx /* push dst address */ + + .align 2,0x90 +L1: movb (%edx),%al /* unroll loop, but not too much */ + movb %al,(%ecx) + testb %al,%al + je L2 + movb 1(%edx),%al + movb %al,1(%ecx) + testb %al,%al + je L2 + movb 2(%edx),%al + movb %al,2(%ecx) + testb %al,%al + je L2 + movb 3(%edx),%al + movb %al,3(%ecx) + testb %al,%al + je L2 + movb 4(%edx),%al + movb %al,4(%ecx) + testb %al,%al + je L2 + movb 5(%edx),%al + movb %al,5(%ecx) + testb %al,%al + je L2 + movb 6(%edx),%al + movb %al,6(%ecx) + testb %al,%al + je L2 + movb 7(%edx),%al + movb %al,7(%ecx) + addl $8,%edx + addl $8,%ecx + testb %al,%al + jne L1 +L2: popl %eax /* pop dst address */ + ret +END(strcpy) diff --git a/sys/arch/i386/strncmp.S b/sys/arch/i386/strncmp.S new file mode 100644 index 0000000..9696281 --- /dev/null +++ b/sys/arch/i386/strncmp.S @@ -0,0 +1,129 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + + #include + +ENTRY(strncmp) + pushl %ebx + movl 8(%esp),%eax + movl 12(%esp),%ecx + movl 16(%esp),%edx + testl %edx,%edx + jmp L2 /* Jump into the loop! */ + + .align 2,0x90 +L1: incl %eax + incl %ecx + decl %edx +L2: jz L4 /* strings are equal */ + movb (%eax),%bl + testb %bl,%bl + jz L3 + cmpb %bl,(%ecx) + jne L3 + incl %eax + incl %ecx + decl %edx + jz L4 + movb (%eax),%bl + testb %bl,%bl + jz L3 + cmpb %bl,(%ecx) + jne L3 + + incl %eax + incl %ecx + decl %edx + jz L4 + movb (%eax),%bl + testb %bl,%bl + jz L3 + cmpb %bl,(%ecx) + jne L3 + + incl %eax + incl %ecx + decl %edx + jz L4 + movb (%eax),%bl + testb %bl,%bl + jz L3 + cmpb %bl,(%ecx) + jne L3 + + incl %eax + incl %ecx + decl %edx + jz L4 + movb (%eax),%bl + testb %bl,%bl + jz L3 + cmpb %bl,(%ecx) + jne L3 + + incl %eax + incl %ecx + decl %edx + jz L4 + movb (%eax),%bl + testb %bl,%bl + jz L3 + cmpb %bl,(%ecx) + jne L3 + + incl %eax + incl %ecx + decl %edx + jz L4 + movb (%eax),%bl + testb %bl,%bl + jz L3 + cmpb %bl,(%ecx) + jne L3 + + incl %eax + incl %ecx + decl %edx + jz L4 + movb (%eax),%bl + testb %bl,%bl + jz L3 + cmpb %bl,(%ecx) + je L1 + + .align 2,0x90 +L3: movzbl (%eax),%eax /* unsigned comparison */ + movzbl (%ecx),%ecx + subl %ecx,%eax + popl %ebx + ret + .align 2,0x90 +L4: xorl %eax,%eax + popl %ebx + ret +END(strncmp) diff --git a/sys/arch/i386/support.S b/sys/arch/i386/support.S new file mode 100644 index 0000000..347cdf8 --- /dev/null +++ b/sys/arch/i386/support.S @@ -0,0 +1,120 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + + #include +.text + +// void bzero(void *buf, u_int len) +ENTRY(bzero) + pushl %edi + movl 8(%esp),%edi + movl 12(%esp),%ecx + xorl %eax,%eax + shrl $2,%ecx + cld + rep + stosl + movl 12(%esp),%ecx + andl $3,%ecx + rep + stosb + popl %edi + ret +END(bzero) + +// bcopy(src, dst, cnt) +ENTRY(bcopy) + pushl %ebp + movl %esp,%ebp + pushl %esi + pushl %edi + movl 8(%ebp),%esi + movl 12(%ebp),%edi + movl 16(%ebp),%ecx + + movl %edi,%eax + subl %esi,%eax + cmpl %ecx,%eax /* overlapping && src < dst? */ + jb 1f + + shrl $2,%ecx /* copy by 32-bit words */ + cld /* nope, copy forwards */ + rep + movsl + movl 16(%ebp),%ecx + andl $3,%ecx /* any bytes left? */ + rep + movsb + popl %edi + popl %esi + popl %ebp + ret + + ALIGN_TEXT +1: + addl %ecx,%edi /* copy backwards */ + addl %ecx,%esi + decl %edi + decl %esi + andl $3,%ecx /* any fractional bytes? */ + std + rep + movsb + movl 16(%ebp),%ecx /* copy remainder by 32-bit words */ + shrl $2,%ecx + subl $3,%esi + subl $3,%edi + rep + movsl + popl %edi + popl %esi + cld + popl %ebp + ret +END(bcopy) + +// void *memcpy(const void *dst, const void * src, size_t length) +ENTRY(memcpy) + pushl %edi + pushl %esi + movl 12(%esp),%edi + movl 16(%esp),%esi + movl 20(%esp),%ecx + movl %edi,%eax + shrl $2,%ecx /* copy by 32-bit words */ + cld /* nope, copy forwards */ + rep + movsl + movl 20(%esp),%ecx + andl $3,%ecx /* any bytes left? */ + rep + movsb + popl %esi + popl %edi + ret +END(memcpy) diff --git a/sys/arch/i386/sys_call.S b/sys/arch/i386/sys_call.S new file mode 100644 index 0000000..7844128 --- /dev/null +++ b/sys/arch/i386/sys_call.S @@ -0,0 +1,109 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + +.globl _sys_call +.text +.code32 + +_B4: +_B3: +nop + +_ast: + +_astRet: + +_sysCall_MrOlsen: +//MrOlsen 2018-01-14 push $0x2 +//MrOlsen 2018-01-14 sub $0x4,%esp +pusha +push %ds +push %es +push %fs +push %gs +mov $0x10,%eax +mov %eax,%ds +mov %eax,%es +mov %eax,%fs +cld +push %esp +call sys_call +add $0x4,%esp +cmpb $0x13,0x38(%esp) +je _B4 +testl $0x2000,0x3c(%esp) /* Test If VM */ +jz _notVM +jmp _isVM +_notVM: +testb $0x3,0x38(%esp) /* See If We're In User CS (GDT Entry 5) */ +jz _popFS +jmp _popFS /* TMP BECAUSE ABOVE ISN'T RIGHT */ +cli +mov %fs:0x0,%eax +testl $0x10800,0x80(%eax) /* Document This */ +je _popFS +sti +push %esp +call _ast +add $0x4,%esp +jmp _astRet +_isVM: +hlt + +_popFS: +pop %gs +pop %fs +pop %es +pop %ds +popa +//MrOlsen 2018-01-14 add $0x8,%esp +iret + +_sys_call: +push $0x0 +push $0x80 +pusha +push %ds +push %es +push %fs +push %gs +mov $0x10,%eax +mov %eax, %ds +mov %eax, %es +mov %eax, %fs +cld +push %esp +call sys_call +add $0x4,%esp /* Remove Stack Pointer From Stack */ +pop %gs +pop %fs +pop %es +pop %ds +popa +add $0x8,%esp //Back Out Error Code & trap_no +iret diff --git a/sys/arch/i386/sys_call_posix.S b/sys/arch/i386/sys_call_posix.S new file mode 100644 index 0000000..d9b082f --- /dev/null +++ b/sys/arch/i386/sys_call_posix.S @@ -0,0 +1,57 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + +#define FAKE_MCOUNT(caller) pushl caller ; call __mcount ; popl %ecx + +.globl _sys_call_posix +.text +.code32 + +_sys_call_posix: +push $0x0 +push $0x80 +pusha +push %ds +push %es +push %fs +push %gs +mov $0x10,%eax +mov %eax, %ds +mov %eax, %es +mov %eax, %fs +cld +push %esp +call sys_call_posix +add $0x4,%esp /* Remove Stack Pointer From Stack */ +pop %gs +pop %fs +pop %es +pop %ds +popa +add $0x8,%esp //Back Out Error Code & trap_no +iret diff --git a/sys/arch/i386/systemtask.c b/sys/arch/i386/systemtask.c new file mode 100644 index 0000000..952fc06 --- /dev/null +++ b/sys/arch/i386/systemtask.c @@ -0,0 +1,131 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static unsigned char *videoBuffer = (unsigned char *) 0xB8000; + +void systemTask() { + + mpi_message_t myMsg; + uint32_t counter = 0x0; + int i = 0x0; + int *x = 0x0; + kTask_t *tmpTask = 0x0; + + char buf[16] = { "a\n" }; + + if (mpi_createMbox("system") != 0x0) { + kpanic("Error: Error creating mailbox: system\n"); + } + + while (1) { + if (mpi_fetchMessage("system", &myMsg) == 0x0) { + switch (myMsg.header) { + case 0x69: + x = (int *) &myMsg.data; + kprintf("Switching to term: [%i][%i]\n", *x, myMsg.pid); + schedFindTask(myMsg.pid)->term = tty_find(*x); + break; + case 1000: + kprintf("Restarting the system in 5 seconds\n"); + counter = systemVitals->sysUptime + 5; + while (systemVitals->sysUptime < counter) { + sched_yield(); + } + kprintf("Rebooting NOW!!!\n"); + while (inportByte(0x64) & 0x02) + ; + outportByte(0x64, 0xFE); + break; + case 31337: + kprintf("system: backdoor opened\n"); + break; + case 0x80: + if (!strcmp(myMsg.data, "sdeStart")) { + kprintf("Starting SDE\n"); + execThread(sdeThread,0x2000,0x0); + } + else if (!strcmp(myMsg.data, "freePage")) { + kprintf("kkk Free Pages"); + } + else if (!strcmp(myMsg.data, "sdeStop")) { + printOff = 0x0; + biosCall(0x10, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0); + for (i = 0x0; i < 100; i++) + asm("hlt"); + } + break; + default: + kprintf("system: Received message %i:%s\n", myMsg.header, myMsg.data); + break; + } + } + + /* + Here we get the next task from the delete task queue + we first check to see if it has an fd attached for the binary and after that + we free the pages for the process and then free the task + */ + tmpTask = sched_getDelTask(); + + if (tmpTask != 0x0) { + if (tmpTask->files[0] != 0x0) + fclose(tmpTask->files[0]); + vmm_freeProcessPages(tmpTask->id); + kfree(tmpTask); + + } + + if (ogprintOff == 1) { + videoBuffer[0] = systemVitals->sysTicks; + videoBuffer[1] = 'c'; + } + /* + else + ogPrintf(buf); + */ + + sched_yield(); + } + + return; +} diff --git a/sys/arch/i386/timer.S b/sys/arch/i386/timer.S new file mode 100644 index 0000000..1ddfcc1 --- /dev/null +++ b/sys/arch/i386/timer.S @@ -0,0 +1,75 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + +.globl timerInt +.text +.code32 +timerInt: + pusha /* Save all of the registers */ + mov $0x20,%dx /* The Following Sends Our EOI To The MPIC */ + mov $0x20,%ax + outb %al,%dx + movl systemVitals,%ecx /* Put Location Of System Vitals Into ECX */ + incl (%ecx) /* Increment sysTicks our 1000ms counter */ + movl (%ecx),%eax /* Increment our sysUptime by 1S if 1000MS */ + movl $200,%ebx /* Have Passed */ + xor %edx,%edx + div %ebx + test %edx,%edx + jnz next + incl 4(%ecx) +next: + movl (%ecx),%eax /* Test If quantum Has Passed If So Then */ + movl 8(%ecx),%ebx /* We Can CALL sched */ + xor %edx,%edx + div %ebx + test %edx,%edx + jnz done +/* +push %ds +push %es +push %fs +push %gs +mov $0x10,%eax +mov %eax, %ds +mov %eax, %es +mov %eax, %fs +cld +*/ +/* push %esp */ + call sched +/* add $0x4,%esp */ /* Remove Stack Pointer From Stack */ +/* +pop %gs +pop %fs +pop %es +pop %ds +*/ +done: + popa /* Restore Registers */ + iret diff --git a/sys/arch/i386/trap.c b/sys/arch/i386/trap.c new file mode 100644 index 0000000..60f0c65 --- /dev/null +++ b/sys/arch/i386/trap.c @@ -0,0 +1,133 @@ +/*- + * Copyright (c) 2002-2018 The UbixOS Project. + * All rights reserved. + * + * This was developed by Christopher W. Olsen for the UbixOS Project. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1) Redistributions of source code must retain the above copyright notice, this list of + * conditions, the following disclaimer and the list of authors. + * 2) 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. + * 3) 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 AUTHOR 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. + */ + +#include +#include +#include +#include +#include +#include +#include + +#define FIRST_TSS_ENTRY 6 +#define VM_MASK 0x00020000 + +#define store_TR(n) \ + __asm__("str %%ax\n\t" \ + "subl %2,%%eax\n\t" \ + "shrl $4,%%eax" \ + :"=a" (n) \ + :"0" (0),"i" (FIRST_TSS_ENTRY<<3)) + +#define get_seg_long(seg,addr) ({ \ + register unsigned long __res; \ + __asm__("push %%fs;mov %%ax,%%fs;movl %%fs:%2,%%eax;pop %%fs" \ + :"=a" (__res):"0" (seg),"m" (*(addr))); \ + __res;}) + +#define get_seg_byte(seg,addr) ({ \ +register char __res; \ +__asm__("push %%fs;mov %%ax,%%fs;movb %%fs:%2,%%al;pop %%fs" \ + :"=a" (__res):"0" (seg),"m" (*(addr))); \ +__res;}) + +void die_if_kernel(char *str, struct trapframe *regs, long err) { + int i; + unsigned long esp; + unsigned short ss; + unsigned long *stack; + + esp = (unsigned long) ®s->tf_esp; + + ss = 0x10; //KERNEL_DS + + //if ((regs->tf_eflags & VM_MASK) || (3 & regs->tf_cs) == 3) + // return; + + if ((regs->tf_cs & 3) == 3) { + esp = regs->tf_esp; + ss = regs->tf_ss; + kprintf("USER TASK!"); + } + else { + ss = 0x10; + } + + kprintf("\n%s: 0x%X:%i, CPU %d, EIP: 0x%X, EFLAGS: 0x%X\n", str, regs->tf_err, regs->tf_trapno, 0x0, regs->tf_eip, regs->tf_eflags); + kprintf("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n", regs->tf_eax, regs->tf_ebx, regs->tf_ecx, regs->tf_edx); + kprintf("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n", regs->tf_esi, regs->tf_edi, regs->tf_ebp, esp); + kprintf("cs: 0x%X ds: 0x%X es: 0x%X fs: 0x%X gs: 0x%X ss: 0x%X\n", regs->tf_cs, regs->tf_ds, regs->tf_es, regs->tf_fs, regs->tf_gs, ss); + kprintf("cr0: 0x%X, cr2: 0x%X, cr3: 0x%X, cr4: 0x%X\n", rcr0(), rcr2(), rcr3(), rcr4()); + + store_TR(i); + kprintf("Process %s (pid: %i, process nr: %d, stackpage=%08lx)\nStack:", _current->name, _current->id, 0xffff & i, esp); + + stack = (unsigned long *) esp; + + for (i = 0; i < 16; i++) { + if (i && ((i % 8) == 0)) + kprintf("\n "); + kprintf("%08lx ", get_seg_long(ss, stack++)); + } + + endTask(_current->id); +} + +void trap(struct trapframe *frame) { + u_int trap_code; + u_int cr2 = 0; + + trap_code = frame->tf_trapno; + + cr2 = rcr2(); + kprintf("CR2: 0x%X(0x%X)[0x%X]", cr2,_current->tss.eip,_current->tss.ldt); + if (_current->id == 7) + while(1) asm("nop"); + + if ((frame->tf_eflags & PSL_I) == 0) { + if (SEL_GET_PL(frame->tf_cs) == SEL_PL_USER || (frame->tf_eflags & PSL_VM)) { + kpanic( "INT OFF! USER" ); + die_if_kernel("TEST", frame, 0x100); + } + else { + kpanic( "INT OFF! KERN[0x%X]", trap_code ); + die_if_kernel("TEST", frame, 0x200); + } + } + + kprintf("trap_code: %i(0x%X), EIP: 0x%X, CR2: 0x%X\n", frame->tf_trapno, frame->tf_trapno, frame->tf_eip, cr2); + if (frame->tf_trapno == 0xc) { + vmm_pageFault(frame, cr2); + } + else { + kpanic("TRAPCODE"); + die_if_kernel("trapCode", frame, frame->tf_trapno); + endTask(_current->id); + sched_yield(); + } +} diff --git a/sys/compile/Makefile b/sys/compile/Makefile index 341c4c1..4f28ac4 100644 --- a/sys/compile/Makefile +++ b/sys/compile/Makefile @@ -9,7 +9,7 @@ OBJS = null.o #Kernel Parts -KPARTS = ../${_ARCH}/*.o ../init/*.o ../sys/*.o ../vmm/*.o ../lib/*.o ../kernel/*.o ../isa/*.o ../fs/vfs/*.o ../pci/*.o ../fs/devfs/*.o ../mpi/*.o ../fs/ufs/*.o ../fs/common/*.o ../net/net/*.o ../net/netif/*.o ../net/api/*.o ../net/core/*.o ../net/core/ipv4/*.o ../sde/*.o +KPARTS = ../arch/${_ARCH}/*.o ../init/*.o ../sys/*.o ../vmm/*.o ../lib/*.o ../kernel/*.o ../isa/*.o ../fs/vfs/*.o ../pci/*.o ../fs/devfs/*.o ../mpi/*.o ../fs/ufs/*.o ../fs/common/*.o ../net/net/*.o ../net/netif/*.o ../net/api/*.o ../net/core/*.o ../net/core/ipv4/*.o ../sde/*.o # ../net/core/ipv6/*.o # ../fs/ubixfs/*.o # ../sde/*.o ../graphics/*.o ../ld/*.o -Ttext 0x30000 -Tdata 0x34000 ../ubixfs/*.o diff --git a/sys/i386/Makefile b/sys/i386/Makefile deleted file mode 100644 index 0ce7291..0000000 --- a/sys/i386/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -# (C) 2002 The UbixOS Project -# $Id: Makefile 202 2016-01-23 15:21:35Z reddawg $ - -# Include Global 'Source' Options -include ../../Makefile.incl -include ../Makefile.incl - -# Objects -OBJS = support.o strcpy.o strcmp.o strncmp.o memset.o memcmp.o schedyield.o kpanic.o timer.o spinlock.o i386_exec.o sys_call_posix.o sys_call.o bioscall.o fork.o systemtask.o sched.o cpu.o trap.o bios16code.o -# ap-boot.o smp.o vitals.o(obsolete) - -all: $(OBJS) - -# Compile Types -.cc.o: - $(CXX) -DNOBOOL $(CFLAGS) $(INCLUDES) -c -o $@ $< -.cc.s: - $(CXX) -DNOBOOL $(CFLAGS) $(INCLUDES) -S -o $@ $< -.c.o: - $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< -.c.s: - $(CC) $(CFLAGS) $(INCLUDES) -S -o $@ $< -.S.o: - $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< - -# Clean up the junk -clean: - $(REMOVE) $(OBJS) diff --git a/sys/i386/ap-boot.S b/sys/i386/ap-boot.S deleted file mode 100644 index c08bd1e..0000000 --- a/sys/i386/ap-boot.S +++ /dev/null @@ -1,133 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - -/* - * Okay, this file contains the code that's going to bootstrap the AP cpus - */ - - - .globl ap_trampoline_start,ap_trampoline_end - .text - .code16 -ap_trampoline_start: - cli - cld - - movw %cs,%ax // The CPU knows its CS already, so lets use it for the other segments - movw %ax,%ds - movw %ax,%es - movw %ax,%ss - - // Do some bochs-specific bullshit - mov $0x31,%al // '1' - mov $0xe9,%dx - outb %al,%dx - //lgdt ap_gdt; - lgdt ap_trampoline_gdt_limit - ap_trampoline_start - movl %cr0,%eax - orl $0x1,%eax - movl %eax,%cr0 // PMODE! - -.code32 - .byte 0x66 - ljmp $0x08,$(ap_trampoline_32 - ap_trampoline_start) // 0x08 == KERNEL_CS - -ap_trampoline_32: - mov $0x32,%al // '2' - mov $0xe9,%dx - outb %al,%dx - - mov $0x10,%ax - mov %ax,%ds - mov %ax,%es - mov %ax,%fs - mov %ax,%gs - mov %ax,%ss - - // Spinlock - mov ap_trampoline_spl - ap_trampoline_start,%edi -ap_spl: - //cmp $1,(%edi) - //je ap_spl - - mov $1,%eax // Value to be set - xchgl (%edi),%eax - cmp $0,%eax - je ap_spl - // /Spinlock - - mov $0x30,%al // '0' - mov $0xe9,%dx - outb %al,%dx - - mov ap_trampoline_stackptr - ap_trampoline_start,%ebx - mov %ebx,%esp - add $0x1000,%ebx - mov %ebx,ap_trampoline_stackptr - ap_trampoline_start - - mov $0x31,%al // '1' - mov $0xe9,%dx - outb %al,%dx - - // spinunlock - mov $0,%eax - mov ap_trampoline_spl - ap_trampoline_start,%edi - xchgl (%edi),%eax - // /spinunlock - - mov $0x33,%al // '3' - mov $0xe9,%dx - outb %al,%dx - - mov ap_trampoline_epoint,%eax - call *%eax -1: - hlt - jmp 1b // Halt if we ever get here somehow - - // Stack.. This sucks, since CPU initialization isn't serialized -ap_trampoline_stackptr: - .long 0x10000 // 256KB -ap_trampoline_epoint: - .long c_ap_boot - -ap_trampoline_spl: - .long 0 -ap_gdt: - .long ubixGDT - - // GDT -ap_trampoline_gdt: - .word 0 -ap_trampoline_gdt_limit: - .word 128 // Room for 32 descriptors -ap_trampoline_gdt_base: - .long 0x20000 // 128KB (move this later) - - -ap_trampoline_end: diff --git a/sys/i386/bios16code.S b/sys/i386/bios16code.S deleted file mode 100644 index b86ee01..0000000 --- a/sys/i386/bios16code.S +++ /dev/null @@ -1,5 +0,0 @@ -.globl bios16Code -bios16Code: -.code16 -int $0x10 -int $0x69 diff --git a/sys/i386/bioscall.c b/sys/i386/bioscall.c deleted file mode 100644 index e3d3094..0000000 --- a/sys/i386/bioscall.c +++ /dev/null @@ -1,90 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -void biosCall(int biosInt, int eax, int ebx, int ecx, int edx, int esi, int edi, int es, int ds) { - short segment = 0x0, offset = 0x0; - uint32_t tmpAddr = (uint32_t) &bios16Code; - kTask_t *newProcess = 0x0; - - offset = tmpAddr & 0xF; // lower 4 bits - segment = tmpAddr >> 4; - - newProcess = schedNewTask(); - assert(newProcess); - - newProcess->tss.back_link = 0x0; - newProcess->tss.esp0 = (uint32_t) vmm_getFreeKernelPage(newProcess->id, 2) + (0x2000 - 0x4); // XXX I had 0xDEADBEEF I'm not sure why - newProcess->tss.ss0 = 0x10; - newProcess->tss.esp1 = 0x0; - newProcess->tss.ss1 = 0x0; - newProcess->tss.esp2 = 0x0; - newProcess->tss.ss2 = 0x0; - newProcess->tss.cr3 = kernelPageDirectory; //vmm_createVirtualSpace(newProcess->id); //(uint32_t)_current->tss.cr3; - newProcess->tss.eip = offset & 0xFFFF; - newProcess->tss.eflags = 2 | EFLAG_IF | EFLAG_VM; - newProcess->tss.eax = eax & 0xFFFF; - newProcess->tss.ebx = ebx & 0xFFFF; - newProcess->tss.ecx = ecx & 0xFFFF; - newProcess->tss.edx = edx & 0xFFFF; - newProcess->tss.esp = 0x1000 & 0xFFFF; - newProcess->tss.ebp = 0x1000 & 0xFFFF; - newProcess->tss.esi = esi & 0xFFFF; - newProcess->tss.edi = edi & 0xFFFF; - newProcess->tss.es = es & 0xFFFF; - newProcess->tss.cs = segment & 0xFFFF; - newProcess->tss.ss = 0x1000 & 0xFFFF; - newProcess->tss.ds = ds & 0xFFFF; - newProcess->tss.fs = 0x0 & 0xFFFF; - newProcess->tss.gs = 0x0 & 0xFFFF; - newProcess->tss.ldt = 0x0 & 0xFFFF; - newProcess->tss.trace_bitmap = 0x0 & 0xFFFF; - newProcess->tss.io_map = 0x0 & 0xFFFF; - newProcess->tss.io_map = sizeof(struct tssStruct) - 8192; - newProcess->oInfo.v86Task = 0x1; - - kprintf("EIP: [0x%X] 0x%X:0x%X", tmpAddr, newProcess->tss.eip, newProcess->tss.cs); - - newProcess->state = READY; - while (newProcess->state > 0) - sched_yield(); - - kprintf("EIP: [0x%X] 0x%X:0x%X!", tmpAddr, newProcess->tss.eip, newProcess->tss.cs); - kprintf("CALL DONE: %i 0x%X 0x%X!", newProcess->state, newProcess->tss.esp, newProcess->tss.ss); - - return; -} diff --git a/sys/i386/cpu.c b/sys/i386/cpu.c deleted file mode 100644 index ef9e0c0..0000000 --- a/sys/i386/cpu.c +++ /dev/null @@ -1,29 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - -#include diff --git a/sys/i386/fork.c b/sys/i386/fork.c deleted file mode 100644 index 3e222f8..0000000 --- a/sys/i386/fork.c +++ /dev/null @@ -1,228 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -int sys_fork(struct thread *td, struct sys_fork_args *args) { - struct taskStruct *newProcess; - - newProcess = schedNewTask(); - - /* - * - * Initalize New Task Information From Parrent - * - */ - - /* Set CWD */ - memcpy(newProcess->oInfo.cwd, _current->oInfo.cwd, 1024); - - /* Set PPID */ - newProcess->ppid = _current->id; - - /* Set PGRP */ - newProcess->pgrp = _current->pgrp; - - /* Set Up Task State */ - newProcess->tss.eip = td->frame->tf_eip; - newProcess->oInfo.vmStart = _current->oInfo.vmStart; - newProcess->term = _current->term; - newProcess->term->owner = newProcess->id; - newProcess->uid = _current->uid; - newProcess->gid = _current->gid; - newProcess->tss.back_link = 0x0; - newProcess->tss.esp0 = _current->tss.esp0; - newProcess->tss.ss0 = 0x10; - newProcess->tss.esp1 = 0x0; - newProcess->tss.ss1 = 0x0; - newProcess->tss.esp2 = 0x0; - newProcess->tss.ss2 = 0x0; - newProcess->tss.eflags = td->frame->tf_eflags; - newProcess->tss.eax = 0x0; - newProcess->tss.ebx = td->frame->tf_ebx; - newProcess->tss.ecx = td->frame->tf_ecx; - newProcess->tss.edx = td->frame->tf_edx; - newProcess->tss.esi = td->frame->tf_esi; - newProcess->tss.edi = td->frame->tf_edi; - newProcess->tss.ebp = td->frame->tf_ebp; - newProcess->tss.esp = td->frame->tf_esp; - newProcess->tss.cs = td->frame->tf_cs; // & 0xFF; - newProcess->tss.ss = td->frame->tf_ss; // & 0xFF; - newProcess->tss.ds = td->frame->tf_ds; //_current->tss.ds & 0xFF; - newProcess->tss.fs = td->frame->tf_fs; //_current->tss.fs & 0xFF; - newProcess->tss.gs = _current->tss.gs & 0xFF; - newProcess->tss.es = td->frame->tf_es; //_current->tss.es & 0xFF; - newProcess->tss.ldt = 0x18; - newProcess->tss.trace_bitmap = 0x0000; - newProcess->tss.io_map = 0x8000; - - newProcess->td.vm_tsize = _current->td.vm_tsize; - newProcess->td.vm_taddr = _current->td.vm_taddr; - newProcess->td.vm_dsize = _current->td.vm_dsize; - newProcess->td.vm_daddr = _current->td.vm_daddr; - - //kprintf("Copying Mem Space! [0x%X:0x%X:0x%X:0x%X:0x%X:%i:%i]\n", newProcess->tss.esp0, newProcess->tss.esp, newProcess->tss.ebp, td->frame->tf_esi, td->frame->tf_eip, newProcess->id, _current->id); - - newProcess->tss.cr3 = (uInt32) vmm_copyVirtualSpace(newProcess->id); - //kprintf( "Copied Mem Space! [0x%X]\n", newProcess->tss.cr3 ); - - newProcess->state = FORK; - /* Fix gcc optimization problems */ - while (newProcess->state == FORK) - sched_yield(); - - newProcess->parent = _current; - _current->children++; - - /* Return Id of Proccess */ - td->td_retval[0] = newProcess->id; - return (0); - -} - -/***************************************************************************************** - Functoin: static int fork_copyProcess(struct taskStruct *newProcess,long ebp,long edi, - long esi, long none,long ebx,long ecx,long edx,long eip,long cs,long eflags, - long esp,long ss) - - Desc: This function will copy a process - - Notes: - - *****************************************************************************************/ - -/* Had to remove static though tihs function is only used in this file */ -int fork_copyProcess(struct taskStruct *newProcess, long ebp, long edi, long esi, long none, long ebx, long ecx, long edx, long eip, long cs, long eflags, long esp, long ss) { - volatile struct taskStruct * tmpProcPtr = newProcess; - assert(newProcess); - assert(_current); - - /* Set Up New Tasks Information */ - memcpy(newProcess->oInfo.cwd, _current->oInfo.cwd, 1024); - //kprintf( "Initializing New CWD!\n" ); - - newProcess->tss.eip = eip; - newProcess->oInfo.vmStart = _current->oInfo.vmStart; - newProcess->term = _current->term; - newProcess->term->owner = newProcess->id; - newProcess->uid = _current->uid; - newProcess->gid = _current->gid; - newProcess->tss.back_link = 0x0; - newProcess->tss.esp0 = _current->tss.esp0; - newProcess->tss.ss0 = 0x10; - newProcess->tss.esp1 = 0x0; - newProcess->tss.ss1 = 0x0; - newProcess->tss.esp2 = 0x0; - newProcess->tss.ss2 = 0x0; - newProcess->tss.eflags = eflags; - newProcess->tss.eax = 0x0; - newProcess->tss.ebx = ebx; - newProcess->tss.ecx = ecx; - newProcess->tss.edx = edx; - newProcess->tss.esi = esi; - newProcess->tss.edi = edi; - newProcess->tss.ebp = ebp; - newProcess->tss.esp = esp; - newProcess->tss.cs = cs & 0xFF; - newProcess->tss.ss = ss & 0xFF; - newProcess->tss.ds = _current->tss.ds & 0xFF; - newProcess->tss.fs = _current->tss.fs & 0xFF; - newProcess->tss.gs = _current->tss.gs & 0xFF; - newProcess->tss.es = _current->tss.es & 0xFF; - newProcess->tss.ldt = 0x18; - newProcess->tss.trace_bitmap = 0x0000; - newProcess->tss.io_map = 0x8000; - - newProcess->td.vm_tsize = _current->td.vm_tsize; - newProcess->td.vm_taddr = _current->td.vm_taddr; - newProcess->td.vm_dsize = _current->td.vm_dsize; - newProcess->td.vm_daddr = _current->td.vm_daddr; - - /* Create A Copy Of The VM Space For New Task */ - //MrOlsen 2018kprintf("Copying Mem Space! [0x%X:0x%X:0x%X:0x%X:0x%X:%i:%i:0x%X]\n", newProcess->tss.esp0, newProcess->tss.esp, newProcess->tss.ebp, esi, eip, newProcess->id, _current->id, newProcess->td.vm_daddr); - newProcess->tss.cr3 = (uInt32) vmm_copyVirtualSpace(newProcess->id); - //kprintf( "Copied Mem Space!\n" ); - - newProcess->state = FORK; - - /* Fix gcc optimization problems */ - while (tmpProcPtr->state == FORK) - sched_yield(); - /* Return Id of Proccess */ - kprintf("Returning! [%i]", _current->id); - - return (newProcess->id); -} - -void qT() { - kprintf("qT\n"); -} - -/***************************************************************************************** - Functoin: void sysFork(); - - Desc: This function will fork a new task - - Notes: - - 08/01/02 - This Seems To Be Working Fine However I'm Not Sure If I - Chose The Best Path To Impliment It I Guess We Will See - What The Future May Bring - - *****************************************************************************************/ -//asm volatile( -__asm( - ".globl sysFork_old \n" - "sysFork_old: \n" - " xor %eax,%eax \n" - " call schedNewTask \n" - " testl %eax,%eax \n" - " je fork_ret \n" - " pushl %esi \n" - " pushl %edi \n" - " pushl %ebp \n" - " pushl %eax \n" - " call fork_copyProcess \n" - " movl %eax,(%ebx) \n" - " addl $16,%esp \n" - "fork_ret: \n" - " ret \n" -); - -/*** - END - ***/ - diff --git a/sys/i386/i386_exec.c b/sys/i386/i386_exec.c deleted file mode 100644 index ae96533..0000000 --- a/sys/i386/i386_exec.c +++ /dev/null @@ -1,996 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define ENVP_PAGE 0x100 -#define ARGV_PAGE 0x100 -#define ELF_AUX 0x100 -#define STACK_PAD 0x1000 - -#define ENOEXEC -1 - -#define AT_NULL 0 /* Terminates the vector. */ -#define AT_IGNORE 1 /* Ignored entry. */ -#define AT_EXECFD 2 /* File descriptor of program to load. */ -#define AT_PHDR 3 /* Program header of program already loaded. */ -#define AT_PHENT 4 /* Size of each program header entry. */ -#define AT_PHNUM 5 /* Number of program header entries. */ -#define AT_PAGESZ 6 /* Page size in bytes. */ -#define AT_BASE 7 /* Interpreter's base address. */ -#define AT_FLAGS 8 /* Flags (unused for i386). */ -#define AT_ENTRY 9 /* Where interpreter should transfer control. */ - -#define AUXARGS_ENTRY(pos, id, val) {*pos = id;pos++; *pos = val;pos++;} - -static int argv_count(char **argv) { - int i = 0; - - while (*argv++ != 0x0) - i++; - - return (i); -} - -static int envp_count(char **envp) { - int i = 0; - - while (*envp++ != 0x0) - i++; - - return (i); -} - -static int args_copyin(char **argv_in, char **argv_out, char **args_out) { - - int argc = argv_count(argv_in); - - uint32_t *argv_tmp = (uint32_t *) kmalloc(sizeof(char *) * (argc + 2)); // + 1 For ARGC + 1 For NULL TERM - - char *args_tmp = (char *) kmalloc(ARGV_PAGE); - - argv_tmp[0] = argc; - - uint32_t sp = 0x0; - - int i = 0x0; - - for (i = 1; i <= argc; i++) { - argv_tmp[i] = (uint32_t)(args_tmp + sp); - strcpy((char *)argv_tmp[i], argv_in[i - 1]); - sp += strlen(argv_in[i - 1]) + 1; - } - - argv_tmp[i++] = 0x0; - - *argv_out = (char *)argv_tmp; - *args_out = args_tmp; - - return (0); - -} - -static int envs_copyin(char **envp_in, char **envp_out, char **envs_out) { - - int envc = envp_count(envp_in); - - uint32_t *envp_tmp = (uint32_t *) kmalloc(sizeof(char *) * (envc + 1)); // + 1 For NULL TERM - - char *envs_tmp = (char *) kmalloc(ENVP_PAGE); - - uint32_t sp = 0x0; - - int i = 0x0; - - for (i = 0; i < envc; i++) { - envp_tmp[i] = (uint32_t)(envs_tmp + sp); - strcpy((char *)envp_tmp[i], envp_in[i]); - sp += strlen(envp_in[i]) + 1; - } - envp_tmp[i++] = 0x0; - - *envp_out = (char *)envp_tmp; - *envs_out = envs_tmp; - return (0); -} - -static int elf_parse_dynamic(elf_file_t ef); - -/***************************************************************************************** - - 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_t execThread(void (*tproc)(void), uint32_t stack, char *arg) { - - kTask_t * newProcess = 0x0; - uint32_t stackAddr = 0x0; - - /* Find A New Thread */ - newProcess = schedNewTask(); - assert(newProcess); - - stackAddr = vmm_getFreeKernelPage(newProcess->id, stack / PAGE_SIZE); - - /* Set All The Correct Thread Attributes */ - newProcess->tss.back_link = 0x0; - newProcess->tss.esp0 = 0x0; - newProcess->tss.ss0 = 0x0; - newProcess->tss.esp1 = 0x0; - newProcess->tss.ss1 = 0x0; - newProcess->tss.esp2 = 0x0; - newProcess->tss.ss2 = 0x0; - newProcess->tss.cr3 = (unsigned int) kernelPageDirectory; - newProcess->tss.eip = (unsigned int) tproc; - newProcess->tss.eflags = 0x206; - newProcess->tss.esp = stackAddr + (stack - 0x4); //stack; - newProcess->tss.ebp = 0x0;//stack; - newProcess->tss.esi = 0x0; - newProcess->tss.edi = 0x0; - - /* Ring 3 Selectors */ - /* - newProcess->tss.es = 0x30+3; - newProcess->tss.cs = 0x28+3; - newProcess->tss.ss = 0x30+3; - newProcess->tss.ds = 0x30+3; - newProcess->tss.fs = 0x30+3; - newProcess->tss.gs = 0x30+3; - */ - - /* Ring 0 Selectors */ - newProcess->tss.es = 0x10; - newProcess->tss.cs = 0x08; - newProcess->tss.ss = 0x10; - newProcess->tss.ds = 0x10; - newProcess->tss.fs = 0x10; - newProcess->tss.gs = 0x10; - - newProcess->tss.ldt = 0x18; - newProcess->tss.trace_bitmap = 0x0000; - newProcess->tss.io_map = 0x8000; - newProcess->oInfo.vmStart = 0x6400000; - - if (newProcess->files[0] != 0x0) - kpanic("Problem With File Descriptors"); - - newProcess->files[0] = 0x0; - - //kprintf("EIP: 0x%X(%i)", tproc, newProcess->id); - - /* 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 %%ebx \n" - "pushl %%ebx \n" - "pushl %%ebx \n" - "movl %%esp,%%eax \n" - "movl %%eax,%1 \n" - "movl %%ecx,%%esp \n" - "popa \n" - : - : "b" (arg),"m" (newProcess->tss.esp) - ); - - /* Put new thread into the READY state */ - sched_setStatus(newProcess->id, READY); - - /* Return with the new process ID */ - return ((uint32_t) 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, char **argv, char **envp, int console) { - - kTask_t *newProcess = 0x0; - - int i = 0x0; - int x = 0x0; - - uint32_t *tmp = 0x0; - - Elf_Ehdr *binaryHeader = 0x0; - - Elf_Phdr *programHeader = 0x0; - - int argc = argv_count(argv); - int envc = envp_count(envp); - - /* Get A New Task For This Proccess */ - newProcess = schedNewTask(); - assert(newProcess); - - newProcess->gid = 0x0; - newProcess->uid = 0x0; - newProcess->pgrp = newProcess->id; - newProcess->term = tty_find(console); - - if (newProcess->term == 0x0) - kprintf("Error: invalid console\n"); - - /* Set tty ownership */ - newProcess->term->owner = newProcess->id; - - /* Now We Must Create A Virtual Space For This Proccess To Run In */ - newProcess->tss.cr3 = (uint32_t) vmm_createVirtualSpace(newProcess->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_t *)(newProcess->tss.cr3)) - ); - - /* Lets Find The File */ - if (newProcess->files[0] != 0x0) - kpanic("Problem With File Descriptors"); - newProcess->files[0] = fopen(file, "r"); - - /* If We Dont Find the File Return */ - if (newProcess->files[0] == 0x0) { - kprintf("Exec Format Error: Binary File Not Executable.\n"); - fclose(newProcess->files[0]); - return; - } - - if (newProcess->files[0]->perms == 0x0) { - kprintf("Exec Format Error: Binary File Not Executable.\n"); - fclose(newProcess->files[0]); - return; - } - - /* Load ELF Header */ - binaryHeader = (Elf_Ehdr *) kmalloc(sizeof(Elf_Ehdr)); - - fread(binaryHeader, sizeof(Elf_Ehdr), 1, newProcess->files[0]); - - /* Check If App Is A Real Application */ - if ((binaryHeader->e_ident[1] != 'E') && (binaryHeader->e_ident[2] != 'L') && (binaryHeader->e_ident[3] != 'F')) { - kprintf("Exec Format Error: Binary File Not Executable.\n"); - kfree(binaryHeader); - fclose(newProcess->files[0]); - return; - } - else if (binaryHeader->e_type != 2) { - kprintf("Exec Format Error: Binary File Not Executable.\n"); - kfree(binaryHeader); - fclose(newProcess->files[0]); - return; - } - else if (binaryHeader->e_entry == 0x300000) { - kprintf("Exec Format Error: Binary File Not Executable.\n"); - kfree(binaryHeader); - fclose(newProcess->files[0]); - return; - } - - newProcess->td.abi = binaryHeader->e_ident[EI_OSABI]; - - /* Load The Program Header(s) */ - programHeader = (Elf_Phdr *) kmalloc(sizeof(Elf_Phdr) * binaryHeader->e_phnum); - fseek(newProcess->files[0], binaryHeader->e_phoff, 0); - - fread(programHeader, (sizeof(Elf_Phdr) * binaryHeader->e_phnum), 1, newProcess->files[0]); - - /* Loop Through The Header And Load Sections Which Need To Be Loaded */ - for (i = 0; i < binaryHeader->e_phnum; i++) { - if (programHeader[i].p_type == 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].p_memsz); x += 0x1000) { - /* Make readonly and read/write !!! */ - if (vmm_remapPage(vmm_findFreePage(newProcess->id), ((programHeader[i].p_vaddr & 0xFFFFF000) + x), PAGE_DEFAULT, newProcess->id, 0) == 0x0) - K_PANIC("Remap Page Failed"); - - memset((void *) ((programHeader[i].p_vaddr & 0xFFFFF000) + x), 0x0, 0x1000); - - } - - /* Now Load Section To Memory */ - fseek(newProcess->files[0], programHeader[i].p_offset, 0); - - fread((void *) programHeader[i].p_vaddr, programHeader[i].p_filesz, 1, newProcess->files[0]); - - if ((programHeader[i].p_flags & 0x2) != 0x2) { - for (x = 0x0; x < (programHeader[i].p_memsz); x += 0x1000) { - if ((vmm_setPageAttributes((programHeader[i].p_vaddr & 0xFFFFF000) + x, PAGE_PRESENT | PAGE_USER)) != 0x0) - kpanic("Error: vmm_setPageAttributes failed, File: %s, Line: %i\n", __FILE__, __LINE__); - } - } - } - } - - /* Set Virtual Memory Start */ - newProcess->oInfo.vmStart = 0x80000000; - newProcess->td.vm_daddr = (u_long) (programHeader[i].p_vaddr & 0xFFFFF000); - - /* Set Up Stack Space */ - //MrOlsen (2016-01-14) FIX: is the stack start supposed to be addressable xhcnage x= 1 to x=0 - //x = 0 because GS= stack address not address -1 fix! - for (x = 1; x <= 100; x++) { - vmm_remapPage(vmm_findFreePage(newProcess->id), (STACK_ADDR+1) - (x * PAGE_SIZE), PAGE_DEFAULT | PAGE_STACK, newProcess->id, 0); - bzero((void *)((STACK_ADDR+1) - (x * PAGE_SIZE)), PAGE_SIZE); - } - - /* Kernel Stack 0x2000 bytes long */ - - //vmm_remapPage(vmm_findFreePage(newProcess->id), 0x5BC000, KERNEL_PAGE_DEFAULT | PAGE_STACK, newProcess->id); - //vmm_remapPage(vmm_findFreePage(newProcess->id), 0x5BB000, KERNEL_PAGE_DEFAULT | PAGE_STACK, newProcess->id); - /* - for (x = 0; x < 2; x++) - vmm_remapPage(vmm_findFreePage(newProcess->id), 0xFFFFF000 - (PAGE_SIZE * x), KERNEL_PAGE_DEFAULT | PAGE_STACK, newProcess->id, 0); - */ - - /* Set All The Proper Information For The Task */ - newProcess->tss.back_link = 0x0; - newProcess->tss.esp0 = 0xFFFFFFFF; //0x5BC000; - newProcess->tss.ss0 = 0x10; - newProcess->tss.esp1 = 0x0; - newProcess->tss.ss1 = 0x0; - newProcess->tss.esp2 = 0x0; - newProcess->tss.ss2 = 0x0; - newProcess->tss.eip = (long) binaryHeader->e_entry; - newProcess->tss.eflags = 0x206; - newProcess->tss.esp = STACK_ADDR; - newProcess->tss.ebp = 0x0;//STACK_ADDR; - newProcess->tss.esi = 0x0; - newProcess->tss.edi = 0x0; - - /* Set these up to be ring 3 tasks */ - newProcess->tss.es = 0x30 + 3; - newProcess->tss.cs = 0x28 + 3; - newProcess->tss.ss = 0x30 + 3; - newProcess->tss.ds = 0x30 + 3; - newProcess->tss.fs = 0x30 + 3; - newProcess->tss.gs = 0x8 + 3 + 4;//0x50 + 3; //0x30 + 3; - - newProcess->tss.ldt = 0x18; - newProcess->tss.trace_bitmap = 0x0000; - newProcess->tss.io_map = 0x8000; - - //sched_setStatus(newProcess->id, READY); - - kfree(binaryHeader); - kfree(programHeader); - fclose(newProcess->files[0]); - newProcess->files[0] = 0x0; - - tmp = (uint32_t *) newProcess->tss.esp0 - 5; - - tmp[0] = binaryHeader->e_entry; - tmp[3] = STACK_ADDR - 12; - - newProcess->tss.esp = STACK_ADDR - ARGV_PAGE - ENVP_PAGE - ELF_AUX - (argc + 1) - (envc + 1) - STACK_PAD; - - tmp = (uint32_t *) newProcess->tss.esp; - - tmp[0] = argc; - - uint32_t sp = 0x0; - - for (i = 1; i <= argc; i++) { - tmp[i] = STACK_ADDR - ARGV_PAGE + sp; - strcpy((char *) tmp[i], argv[i - 1]); - sp += strlen(argv[i - 1]) + 1; - } - tmp[i++] = 0x0; - - sp = 0; - - for (int x = 0; x < envc; x++) { - tmp[x + i] = STACK_ADDR - ARGV_PAGE - ENVP_PAGE + sp; - strcpy((char *) tmp[x + i], envp[x]); - sp += strlen(envp[x]) + 1; - } - tmp[i + x] = 0x0; - - /* Build LDT For GS and FS */ - vmm_unmapPage(VMM_USER_LDT, 1); - - if (vmm_remapPage(vmm_findFreePage(newProcess->id), VMM_USER_LDT, PAGE_DEFAULT, newProcess->id, 0) == 0x0) { - K_PANIC("Error: Remap Page Failed"); - } - - struct gdtDescriptor *taskLDT = 0x0; - - taskLDT = (struct gdtDescriptor *)(VMM_USER_LDT + sizeof(struct gdtDescriptor)); - uint32_t data_addr = 0x0; - - taskLDT->limitLow = (0xFFFFF & 0xFFFF); - taskLDT->baseLow = (data_addr & 0xFFFF); - taskLDT->baseMed = ((data_addr >> 16) & 0xFF); - taskLDT->access = ((dData + dWrite + dBig + dBiglim + dDpl3) + dPresent) >> 8; - taskLDT->limitHigh = (0xFFFFF >> 16); - taskLDT->granularity = ((dData + dWrite + dBig + dBiglim + dDpl3) & 0xFF) >> 4; - taskLDT->baseHigh = data_addr >> 24; - - - /* Switch Back To The Kernels VM Space */ - asm volatile( - "movl %0,%%eax \n" - "movl %%eax,%%cr3 \n" - : : "d" ((uint32_t *)(kernelPageDirectory)) - ); - - sprintf(newProcess->oInfo.cwd, "/"); - - //MrOlsen 2018 kprintf("execFile Return: 0x%X - %i\n", newProcess->tss.eip, newProcess->id); - - /* Put new thread into the READY state */ - sched_setStatus(newProcess->id, READY); - - //_current = newProcess; - - /* Finally Return */ - return; -} - -int sys_exec(struct thread *td, char *file, char **argv, char **envp) { - - int i = 0x0; - int x = 0x0; - - int argc = argv_count(argv); - int envc = envp_count(envp); - - uint32_t cr3 = 0x0; - - uint32_t *tmp = 0x0; - - uInt32 seg_size = 0x0; - uInt32 seg_addr = 0x0; - - char *interp = 0x0; - uint32_t ldAddr = 0x0; - - fileDescriptor_t *fd = 0x0; - - Elf_Ehdr *binaryHeader = 0x0; - Elf_Phdr *programHeader = 0x0; - Elf_Shdr *sectionHeader = 0x0; - - elf_file_t ef = 0x0; - - u_long text_addr = 0, text_size = 0; - u_long data_addr = 0, data_size = 0; - - struct i386_frame *iFrame = 0x0; - - asm("movl %%cr3, %0;" : "=r" (cr3)); - - fd = fopen(file, "r"); - - if (fd == 0x0) { - td->td_retval[0] = 2; - return (-1); - } - - /* Test If Executable */ - if (fd->perms == 0) { - kprintf("Exec Format Error: Binary File Not Executable.\n"); - fclose(fd); - return (-1); - } - - /* Set Threads FD to open FD */ - _current->files[0] = fd; - - /* Copy In ARGS & ENVS Before Cleaning Virtual Space */ - uint32_t *argv_out = 0x0; - char *args_out = 0x0; - - args_copyin(argv, (char **)&argv_out, &args_out); - - uint32_t *envp_out = 0x0; - char *envs_out = 0x0; - - envs_copyin(envp, (char **)&envp_out, &envs_out); - - //! Clean the virtual of COW pages left over from the fork - //vmm_cleanVirtualSpace( (uint32_t) _current->td.vm_daddr + (_current->td.vm_dsize << PAGE_SHIFT) ); - //MrOlsen 2017-12-15 - FIX! - This should be done before it was causing a lot of problems why did I free space after loading binary???? - //vmm_cleanVirtualSpace((uint32_t) 0x8048000); - vmm_cleanVirtualSpace((uint32_t) VMM_USER_START); - - /* Clear Stack */ - //bzero(STACK_ADDR - (100 * PAGE_SIZE), (PAGE_SIZE * 100)); - for (x = 1; x <= 100; x++) { - vmm_remapPage(vmm_findFreePage(_current->id), (STACK_ADDR+1) - (x * 0x1000), PAGE_DEFAULT | PAGE_STACK, _current->id, 0); - bzero((void *)((STACK_ADDR+1) - (x * 0x1000)), 0x1000); - } - - /* Load ELF Header */ - if ((binaryHeader = (Elf_Ehdr *) kmalloc(sizeof(Elf_Ehdr))) == 0x0) - K_PANIC("MALLOC FAILED"); - - fread(binaryHeader, sizeof(Elf_Ehdr), 1, fd); - /* Done Loading ELF Header */ - - /* Check If App Is A Real Application */ - if ((binaryHeader->e_ident[1] != 'E') && (binaryHeader->e_ident[2] != 'L') && (binaryHeader->e_ident[3] != 'F')) { - kprintf("Exec Format Error: Binary File Not Executable.\n"); - kfree(binaryHeader); - fclose(fd); - return (-1); - } - else if (binaryHeader->e_type != ET_EXEC) { - kprintf("Exec Format Error: Binary File Not Executable.\n"); - kfree(binaryHeader); - fclose(fd); - return (-1); - } - else if (binaryHeader->e_entry == 0x300000) { - kprintf("Exec Format Error: Binary File Not Executable.\n"); - kfree(binaryHeader); - fclose(fd); - return (-1); - } - - /* Set Thread ABI */ - td->abi = binaryHeader->e_ident[EI_OSABI]; - - /* Load The Program Header(s) */ - if ((programHeader = (Elf_Phdr *) kmalloc(sizeof(Elf_Phdr) * binaryHeader->e_phnum)) == 0x0) - K_PANIC("MALLOC FAILED"); - - assert(programHeader); - - fseek(fd, binaryHeader->e_phoff, 0); - fread(programHeader, (sizeof(Elf_Phdr) * binaryHeader->e_phnum), 1, fd); - /* Done Loading Program Header(s) */ - - /* Load The Section Header(s) */ - if ((sectionHeader = (Elf_Shdr *) kmalloc(sizeof(Elf_Shdr) * binaryHeader->e_shnum)) == 0x0) - K_PANIC("MALLOC FAILED"); - - assert(sectionHeader); - fseek(fd, binaryHeader->e_shoff, 0); - fread(sectionHeader, sizeof(Elf_Shdr) * binaryHeader->e_shnum, 1, fd); - /* Done Loading Section Header(s) */ - - ef = kmalloc(sizeof(struct elf_file)); - memset(ef, 0x0, sizeof(struct elf_file)); - - /* Loop Through The Header And Load Sections Which Need To Be Loaded */ - for (i = 0; i < binaryHeader->e_phnum; i++) { - switch (programHeader[i].p_type) { - case PT_LOAD: - if (programHeader[i].p_memsz == 0x0) - break; - - seg_addr = trunc_page(programHeader[i].p_vaddr); - seg_size = round_page(programHeader[i].p_memsz + programHeader[i].p_vaddr - seg_addr); - - /* - 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 < (round_page(programHeader[i].p_memsz)); x += 0x1000) { - /* Make readonly and read/write !!! */ - if (vmm_remapPage(vmm_findFreePage(_current->id), ((programHeader[i].p_vaddr & 0xFFFFF000) + x), PAGE_DEFAULT, _current->id, 0) == 0x0) { - K_PANIC("Error: Remap Page Failed"); - } - else { - //MrOlsen 2018-01-15 kprintf("rP[0x%X]", (programHeader[i].p_vaddr & 0xFFFFF000) + x); - } - - memset((void *) ((programHeader[i].p_vaddr & 0xFFFFF000) + x), 0x0, 0x1000); - - } - - /* Now Load Section To Memory */ - fseek(fd, programHeader[i].p_offset, 0); - fread((void *) programHeader[i].p_vaddr, programHeader[i].p_filesz, 1, fd); - - if ((programHeader[i].p_flags & 0x2) != 0x2) { - for (x = 0x0; x < (round_page(programHeader[i].p_memsz)); x += 0x1000) { - if ((vmm_setPageAttributes((programHeader[i].p_vaddr & 0xFFFFF000) + x, PAGE_PRESENT | PAGE_USER)) != 0x0) - kpanic("Error: vmm_setPageAttributes failed, File: %s,Line: %i\n", __FILE__, __LINE__); - } - } - - if ((programHeader[i].p_flags & PF_X) && text_size < seg_size) { - //MrOlsen 2018kprintf("setting text: 0x%X - 0x%X\n", seg_addr, seg_size); - text_size = seg_size; - text_addr = seg_addr; - } - else { - //MrOlsen 2018kprintf("setting data: 0x%X - 0x%X\n", seg_addr, seg_size); - data_size = seg_size; - data_addr = seg_addr; - /* - _current->td.vm_dsize = seg_size >> PAGE_SHIFT; - _current->td.vm_daddr = (char *) seg_addr; - kprintf( "setting daddr: 0x%X, dsiize: 0x%X\n", _current->td.vm_daddr, _current->td.vm_dsize ); - */ - } - - /* - * MrOlsen (2016-01-19) NOTE: Note Sure, I should Do This Later - * Thjis is for stack space - */ - _current->oInfo.vmStart = ((programHeader[i].p_vaddr & 0xFFFFF000) + 0xA900000); - break; - case PT_DYNAMIC: - //newLoc = (char *)programHeader[i].phVaddr; - //elfDynamicS = (elfDynamic *) programHeader[i].p_vaddr; - ef->dynamic = (Elf_Dyn *) programHeader[i].p_vaddr; - //fseek( fd, programHeader[i].phOffset, 0 ); - //fread( (void *) programHeader[i].phVaddr, programHeader[i].phFilesz, 1, fd ); - break; - case PT_INTERP: - #ifdef DEBUG_EXEC - kprintf("%s:%i>Malloc: %i\n", _FILE_,_LINE_,programHeader[i].p_filesz); - #endif - interp = (char *) kmalloc(programHeader[i].p_filesz); - fseek(fd, programHeader[i].p_offset, 0); - fread((void *) interp, programHeader[i].p_filesz, 1, fd); - #ifdef DEBUG_EXEC - kprintf("Interp: [%s]\n", interp); - #endif - ldAddr = ldEnable(interp); - //ef->ld_addr = ldEnable(); - break; - case PT_GNU_STACK: - asm("nop"); - break; - default: - break; - } - } - - _current->td.vm_tsize = text_size >> PAGE_SHIFT; - _current->td.vm_taddr = text_addr; - _current->td.vm_dsize = data_size >> PAGE_SHIFT; - _current->td.vm_daddr = data_addr; - - //MrOlsen 2018kprintf("Done Looping\n"); - - ef->preloaded = 1; - ef->address = 0x0; - elf_parse_dynamic(ef); - - //asm("cld"); - //irqDisable(0); - iFrame = (struct i386_frame *) (_current->tss.esp0 - sizeof(struct i386_frame)); - - //iFrame->ebp = 0x0; - - if (ldAddr != 0x0) { - iFrame->eip = ldAddr; - } - else { - iFrame->eip = binaryHeader->e_entry; - } - - //iFrame->edx = 0x0; - - iFrame->user_esp = (uint32_t) (STACK_ADDR - ARGV_PAGE - ENVP_PAGE - ELF_AUX - (argc + 1) - (envc + 1) - STACK_PAD) & 0xFFFFF000; - - tmp = (uint32_t *) iFrame->user_esp; - -// memset((char *) tmp, 0x0, ARGV_PAGE + ENVP_PAGE + ELF_AUX + (argc + 1) + (envc + 1) + STACK_PAD); - - tmp[0] = argc; - - uint32_t sp = 0x0; - - char *EXECP = 0x0; - - for (i = 1; i <= argc; i++) { - tmp[i] = (uint32_t) STACK_ADDR - ARGV_PAGE + sp; - if (i == 1) { - EXECP = (char *)tmp[i]; - } - strcpy((char *)tmp[i], (const char *)argv_out[i]); - #ifdef EXEC_DEBUG - kprintf("argv[%i]:%s",i, (const char *)argv_out[i]); - #endif - sp += strlen((const char *)argv_out[i]) + 1; - } - - tmp[i++] = 0x0; - - kfree(argv_out); - kfree(args_out); - - sp = 0; - - x = 0; - - for (x = 0; x < envc; x++) { - tmp[x + i] = (uint32_t) STACK_ADDR - ARGV_PAGE - ENVP_PAGE + sp; - strcpy((char *) tmp[x + i], (const char *)envp_out[x]); - sp += strlen((const char *)envp_out[x]) + 1; - } - - tmp[i + x] = 0x0; - - kfree(envp_out); - kfree(envs_out); - - i = i + x + 1; - - struct file *tFP = 0x0; - int tFD = 0x0; - - fseek(_current->files[0], 0x0, 0x0); // Reset File Position - falloc(&_current->td, &tFP, &tFD); - - tFP->fd = _current->files[0]; - - - tmp[i++] = 2; - tmp[i++] = -1;// tFD; // _current->imageFd; - _current->td.o_files[4] = _current->files[0]; - //MrOlsen 2018kprintf("AT_EXECFD: [%i:%i]", tmp[i - 1], tFD); - - tmp[i++] = 3; - tmp[i++] = binaryHeader->e_phoff + 0x08048000; - //MrOlsen 2018kprintf("AT_PHDR: [0x%X]", tmp[i - 1]); - - tmp[i++] = 4; - tmp[i++] = binaryHeader->e_phentsize; - //MrOlsen 2018kprintf("AT_PHENT: [0x%X]", tmp[i - 1]); - - tmp[i++] = 5; - tmp[i++] = binaryHeader->e_phnum; - //MrOlsen 2018kprintf("AT_PHNUM: [0x%X]", tmp[i - 1]); - - tmp[i++] = 6; - tmp[i++] = 0x1000; - - tmp[i++] = 7; - tmp[i++] = LD_START; - //MrOlsen 2018kprintf("AT_BASE: [0x%X]", tmp[i - 1]); - - tmp[i++] = 8; - tmp[i++] = 0x0; - - tmp[i++] = 9; - tmp[i++] = binaryHeader->e_entry; - - tmp[i++] = 11; - tmp[i++] = 0x0; - - tmp[i++] = 12; - tmp[i++] = 0x0; - - tmp[i++] = 13; - tmp[i++] = 0x0; - - tmp[i++] = 14; - tmp[i++] = 0x0; - - tmp[i++] = 15; //EXEC PATH - tmp[i++] = (uint32_t)EXECP; - - tmp[i++] = 19; //NCPUS - tmp[i++] = 0x1; - - tmp[i++] = 23; //STACKPROT - tmp[i++] = 0x3; - - tmp[i++] = 0; - tmp[i++] = 0; - - /* Now That We Relocated The Binary We Can Unmap And Free Header Info */ - kfree(binaryHeader); - kfree(programHeader); - //irqEnable(0); - //asm("sti"); - - /* - _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 = 0x50 + 3; //0x30 + 3; - - _current->tss.ldt = 0x18; - _current->tss.trace_bitmap = 0x0000; - _current->tss.io_map = 0x8000; - */ - - /* - kfree (iFrameNew); - - memAddr = (uint32_t) & (_current->tss); - ubixGDT[4].descriptor.baseLow = (memAddr & 0xFFFF); - ubixGDT[4].descriptor.baseMed = ((memAddr >> 16) & 0xFF); - ubixGDT[4].descriptor.baseHigh = (memAddr >> 24); - ubixGDT[4].descriptor.access = '\x89'; - - ubixGDT[10].descriptor.baseLow = (STACK_ADDR & 0xFFFF); - ubixGDT[10].descriptor.baseMed = ((STACK_ADDR >> 16) & 0xFF); - ubixGDT[10].descriptor.baseHigh = (STACK_ADDR >> 24); - - */ - - /* Build LDT For GS and FS */ - vmm_unmapPage(VMM_USER_LDT, 1); // Can I Free This? - if (vmm_remapPage(vmm_findFreePage(_current->id), VMM_USER_LDT, PAGE_DEFAULT, _current->id, 0) == 0x0) { - K_PANIC("Error: Remap Page Failed"); - } - - struct gdtDescriptor *taskLDT = 0x0; - - taskLDT = (struct gdtDescriptor *)(VMM_USER_LDT + sizeof(struct gdtDescriptor)); - - //data_addr = 0x0; //TEMP - - taskLDT->limitLow = (0xFFFFF & 0xFFFF); - taskLDT->baseLow = (data_addr & 0xFFFF); - taskLDT->baseMed = ((data_addr >> 16) & 0xFF); - taskLDT->access = ((dData + dWrite + dBig + dBiglim + dDpl3) + dPresent) >> 8; - taskLDT->limitHigh = (0xFFFFF >> 16); - taskLDT->granularity = ((dData + dWrite + dBig + dBiglim + dDpl3) & 0xFF) >> 4; - taskLDT->baseHigh = data_addr >> 24; - - _current->tss.gs = 0xF; //Select 0x8 + Ring 3 + LDT - _current->pgrp = _current->id; - - return (0x0); -} - -static int elf_parse_dynamic(elf_file_t ef) { - Elf32_Dyn *dynp; - int plttype = DT_REL; - - for (dynp = ef->dynamic; dynp->d_tag != 0x0; dynp++) { - switch (dynp->d_tag) { - case DT_NEEDED: - asm("nop"); - break; - case DT_INIT: - asm("nop"); - break; - case DT_FINI: - asm("nop"); - break; - case DT_HASH: - asm("nop"); - /* From src/libexec/rtld-elf/rtld.c */ - const Elf_Hashelt *hashtab = (const Elf_Hashelt *) (ef->address + dynp->d_un.d_ptr); - ef->nbuckets = hashtab[0]; - ef->nchains = hashtab[1]; - ef->buckets = hashtab + 2; - ef->chains = ef->buckets + ef->nbuckets; - break; - case DT_STRTAB: - ef->strtab = (caddr_t) (ef->address + dynp->d_un.d_ptr); - break; - case DT_STRSZ: - ef->strsz = dynp->d_un.d_val; - break; - case DT_SYMTAB: - ef->symtab = (Elf_Sym *) (ef->address + dynp->d_un.d_ptr); - break; - case DT_SYMENT: - if (dynp->d_un.d_val != sizeof(Elf32_Sym)) - return (ENOEXEC); - break; - case DT_REL: - ef->rel = (const Elf_Rel *) (ef->address + dynp->d_un.d_ptr); - break; - case DT_RELSZ: - ef->relsize = dynp->d_un.d_val; - break; - case DT_RELENT: - if (dynp->d_un.d_val != sizeof(Elf_Rel)) - return (ENOEXEC); - break; - case DT_JMPREL: - ef->pltrel = (const Elf_Rel *) (ef->address + dynp->d_un.d_ptr); - break; - case DT_PLTRELSZ: - ef->pltrelsize = dynp->d_un.d_val; - break; - case DT_RELA: - ef->rela = (const Elf_Rela *) (ef->address + dynp->d_un.d_ptr); - break; - case DT_RELASZ: - ef->relasize = dynp->d_un.d_val; - break; - case DT_RELAENT: - if (dynp->d_un.d_val != sizeof(Elf_Rela)) - return (ENOEXEC); - break; - case DT_PLTREL: - plttype = dynp->d_un.d_val; - if (plttype != DT_REL && plttype != DT_RELA) - return (ENOEXEC); - break; - case DT_PLTGOT: - ef->got = (Elf_Addr *) (ef->address + dynp->d_un.d_ptr); - /* - tmp = (void *) dynp->d_un.d_ptr; //elfDynamicS[i].dynPtr; - tmp[2] = (uInt32) ef->ld_addr; - tmp[1] = (uInt32) ef; //0x0;//0xBEEFEAD;//STACK_ADDR - 128;//_current->imageFd;//0xBEEFDEAD;//ef; - */ - break; - default: - asm("nop"); - //kprintf("t_tag: 0x%X>", dynp->d_tag); - break; - } - } - - if (plttype == DT_RELA) { - ef->pltrela = (const Elf_Rela *) ef->pltrel; - ef->pltrel = NULL; - ef->pltrelasize = ef->pltrelsize; - ef->pltrelsize = 0; - } - - ef->ddbsymtab = ef->symtab; - ef->ddbsymcnt = ef->nchains; - ef->ddbstrtab = ef->strtab; - ef->ddbstrcnt = ef->strsz; - return (0); -} diff --git a/sys/i386/kpanic.c b/sys/i386/kpanic.c deleted file mode 100644 index 4b6ce22..0000000 --- a/sys/i386/kpanic.c +++ /dev/null @@ -1,66 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - -#include -#include -#include -#include -#include - -/*! - * \brief print panic message and halt system - * - * \param fmt panic message - * - */ -void kpanic(const char *fmt, ...) { - char buf[512]; - va_list args; - - va_start(args, fmt); - vsprintf(buf, fmt, args); - va_end(args); - - /* It's important that we print on the current terminal so let's reset foreground */ - tty_foreground = NULL; - kprintf("kPanic: %s", buf); - - /* Halt The System */ - //asm("cli"); - irqDisable(0x0); - - while (1) { - asm("hlt"); - } - -} - -/*** - END - ***/ - diff --git a/sys/i386/memcmp.S b/sys/i386/memcmp.S deleted file mode 100644 index c8e68c2..0000000 --- a/sys/i386/memcmp.S +++ /dev/null @@ -1,66 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - - #include - -ENTRY(memcmp) - pushl %edi - pushl %esi - movl 12(%esp),%edi - movl 16(%esp),%esi - cld /* set compare direction forward */ - - movl 20(%esp),%ecx /* compare by words */ - shrl $2,%ecx - repe - cmpsl - jne L5 /* do we match so far? */ - - movl 20(%esp),%ecx /* compare remainder by bytes */ - andl $3,%ecx - repe - cmpsb - jne L6 /* do we match? */ - - xorl %eax,%eax /* we match, return zero */ - popl %esi - popl %edi - ret - -L5: movl $4,%ecx /* We know that one of the next */ - subl %ecx,%edi /* four pairs of bytes do not */ - subl %ecx,%esi /* match. */ - repe - cmpsb -L6: movzbl -1(%edi),%eax /* Perform unsigned comparison */ - movzbl -1(%esi),%edx - subl %edx,%eax - popl %esi - popl %edi - ret -END(memcmp) diff --git a/sys/i386/memset.S b/sys/i386/memset.S deleted file mode 100644 index 03bef8d..0000000 --- a/sys/i386/memset.S +++ /dev/null @@ -1,79 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - - #include - - -ENTRY(memset) - pushl %edi - pushl %ebx - movl 12(%esp),%edi - movzbl 16(%esp),%eax /* unsigned char, zero extend */ - movl 20(%esp),%ecx - pushl %edi /* push address of buffer */ - - cld /* set fill direction forward */ - - /* - * if the string is too short, it's really not worth the overhead - * of aligning to word boundries, etc. So we jump to a plain - * unaligned set. - */ - cmpl $0x0f,%ecx - jle L1 - - movb %al,%ah /* copy char to all bytes in word */ - movl %eax,%edx - sall $16,%eax - orl %edx,%eax - - movl %edi,%edx /* compute misalignment */ - negl %edx - andl $3,%edx - movl %ecx,%ebx - subl %edx,%ebx - - movl %edx,%ecx /* set until word aligned */ - rep - stosb - - movl %ebx,%ecx - shrl $2,%ecx /* set by words */ - rep - stosl - - movl %ebx,%ecx /* set remainder by bytes */ - andl $3,%ecx -L1: rep - stosb - - popl %eax /* pop address of buffer */ - popl %ebx - popl %edi - ret -END(memset) diff --git a/sys/i386/sched.c b/sys/i386/sched.c deleted file mode 100644 index 069868d..0000000 --- a/sys/i386/sched.c +++ /dev/null @@ -1,385 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -static kTask_t *taskList = 0x0; -static kTask_t *delList = 0x0; -static uint32_t nextID = 1; - -kTask_t *_current = 0x0; -kTask_t *_usedMath = 0x0; - -static struct spinLock schedulerSpinLock = SPIN_LOCK_INITIALIZER; - -int need_resched = 0; - -/************************************************************************ - - Function: int sched_init() - - Description: This function is used to enable the kernel scheduler - - Notes: - - 02/20/2004 - Approved for quality - - ************************************************************************/ - -int sched_init() { - taskList = (kTask_t *) kmalloc(sizeof(kTask_t)); - if (taskList == 0x0) - kpanic("Unable to create task list"); - - taskList->id = nextID++; - - /* Print out information on scheduler */ - kprintf("sched0 - Address: [0x%X]\n", taskList); - - /* Return so we know everything went well */ - return (0x0); -} - -void sched() { - uint32_t memAddr = 0x0; - kTask_t *tmpTask = 0x0; - kTask_t *delTask = 0x0; - - if (spinTryLock(&schedulerSpinLock)) - return; - - tmpTask = ((_current == 0) ? 0 : _current->next); - schedStart: - - /* Yield the next task from the current prio queue */ - for (; tmpTask != 0x0; tmpTask = tmpTask->next) { - if (tmpTask->state == FORK) - tmpTask->state = READY; - - if (tmpTask->state == READY) { - _current->state = (_current->state == DEAD) ? DEAD : READY; - _current = tmpTask; - break; - } - else if (tmpTask->state == DEAD) { - delTask = tmpTask; - if (delTask->parent != 0x0) { - delTask->parent->children -= 1; - delTask->parent->last_exit = delTask->id; - delTask->parent->state = READY; - } - - tmpTask = tmpTask->next; - sched_deleteTask(delTask->id); - sched_addDelTask(delTask); - goto schedStart; - } - } - - /* Finished all the tasks, restarting the list */ - if (0x0 == tmpTask) { - tmpTask = taskList; - goto schedStart; - } - - if (_current->state == READY || _current->state == RUNNING) { - - if (_current->oInfo.v86Task == 0x1) { - irqDisable(0x0); - kprintf("IRQD(%i): 0x%X*0x%X:0x%X@, esp: 0x%X:0x%X, ebp: 0x%X:0x%X ds: 0x%X", _current->id, _current->tss.eflags, _current->tss.cs, _current->tss.eip, _current->tss.ss, _current->tss.esp, _current->tss.ss, _current->tss.ebp,_current->tss.ds); - kprintf("ss0: 0x%X, esp0: 0x%X", _current->tss.ss0, _current->tss.esp0); - } - - asm("cli"); - - memAddr = (uint32_t) &(_current->tss); - ubixGDT[4].descriptor.baseLow = (memAddr & 0xFFFF); - ubixGDT[4].descriptor.baseMed = ((memAddr >> 16) & 0xFF); - ubixGDT[4].descriptor.baseHigh = (memAddr >> 24); - ubixGDT[4].descriptor.access = '\x89'; - - _current->state = RUNNING; - - spinUnlock(&schedulerSpinLock); - - asm("sti"); - asm("ljmp $0x20,$0"); - } - else { - spinUnlock(&schedulerSpinLock); - } - - return; -} - -kTask_t *schedNewTask() { - int i = 0; - - kTask_t *tmpTask = (kTask_t *) kmalloc(sizeof(kTask_t)); - - struct file *fp = 0x0; - - if (tmpTask == 0x0) - kpanic("Error: schedNewTask() - kmalloc failed trying to initialize a new task struct\n"); - - memset(tmpTask, 0x0, sizeof(kTask_t)); - - /* Filling in tasks attrs */ - tmpTask->usedMath = 0x0; - tmpTask->state = NEW; - - /* HACK */ - - for (i = 0; i < 3; i++) { - fp = (void *) kmalloc(sizeof(struct file)); - //kprintf("DB: [0x%X]\n", (uint32_t) fp); - tmpTask->td.o_files[i] = (void *) fp; - fp->f_flag = 0x4; - } - - spinLock(&schedulerSpinLock); - tmpTask->id = nextID++; - tmpTask->next = taskList; - tmpTask->prev = 0x0; - taskList->prev = tmpTask; - taskList = tmpTask; - - spinUnlock(&schedulerSpinLock); - - return (tmpTask); -} - -int sched_deleteTask(pidType id) { - kTask_t *tmpTask = 0x0; - - /* Checking each task from the prio queue */ - for (tmpTask = taskList; tmpTask != 0x0; tmpTask = tmpTask->next) { - if (tmpTask->id == id) { - if (tmpTask->prev != 0x0) - tmpTask->prev->next = tmpTask->next; - if (tmpTask->next != 0x0) - tmpTask->next->prev = tmpTask->prev; - if (taskList == tmpTask) - taskList = tmpTask->next; - - return (0x0); - } - } - return (0x1); -} - -int sched_addDelTask(kTask_t *tmpTask) { - tmpTask->next = delList; - tmpTask->prev = 0x0; - if (delList != 0x0) - delList->prev = tmpTask; - delList = tmpTask; - return (0x0); -} - -kTask_t *sched_getDelTask() { - kTask_t *tmpTask = 0x0; - - if (delList == 0x0) - return (0x0); - - tmpTask = delList; - delList = delList->next; - return (tmpTask); -} - -kTask_t *schedFindTask(uint32_t id) { - kTask_t *tmpTask = 0x0; - - for (tmpTask = taskList; tmpTask; tmpTask = tmpTask->next) { - if (tmpTask->id == id) - return (tmpTask); - } - - return (0x0); -} - -/************************************************************************ - - Function: void schedEndTask() - - Description: This function will end a task - - Notes: - - 02/20/2004 - Approved for quality - - ************************************************************************/ -void schedEndTask(pidType pid) { - endTask(_current->id); - sched_yield(); -} - -/************************************************************************ - - Function: int schedEndTask() - - Description: This function will yield a task - - Notes: - - 02/20/2004 - Approved for quality - - ************************************************************************/ - -void sched_yield() { - sched(); -} - -/* - asm( - ".globl sched_yield \n" - "sched_yield: \n" - " cli \n" - " call sched \n" - ); - */ - -/************************************************************************ - - Function: int sched_setStatus(pidType pid,tState state) - - Description: Change the tasks status - - Notes: - - ************************************************************************/ -int sched_setStatus(pidType pid, tState state) { - kTask_t *tmpTask = schedFindTask(pid); - if (tmpTask == 0x0) - return (0x1); - tmpTask->state = state; - return (0x0); -} - -void add_wait_queue(struct wait_queue ** p, struct wait_queue * wait) { - unsigned long flags; - - save_flags(flags); - cli(); - if (!*p) { - wait->next = wait; - *p = wait; - } - else { - wait->next = (*p)->next; - (*p)->next = wait; - } - restore_flags(flags); -} - -void remove_wait_queue(struct wait_queue ** p, struct wait_queue * wait) { - unsigned long flags; - struct wait_queue * tmp; - - save_flags(flags); - cli(); - if ((*p == wait) && ((*p = wait->next) == wait)) { - *p = NULL; - } - else { - tmp = wait; - while (tmp->next != wait) { - tmp = tmp->next; - } - tmp->next = wait->next; - } - wait->next = NULL; - restore_flags(flags); -} - -void wake_up_interruptible(struct wait_queue **q) { - struct wait_queue *tmp; - kTask_t *p; - - if (!q || !(tmp = *q)) - return; - do { - if ((p = tmp->task) != NULL) { - if (p->state == INTERRUPTIBLE) { - p->state = RUNNING; - if (p->counter > _current->counter) - need_resched = 1; - } - } - if (!tmp->next) { - kprintf("wait_queue is bad (eip = %08lx)\n", ((unsigned long *) q)[-1]); - kprintf(" q = %p\n", q); - kprintf(" *q = %p\n", *q); - kprintf(" tmp = %p\n", tmp); - break; - } - tmp = tmp->next; - } while (tmp != *q); -} - -void wake_up(struct wait_queue **q) { - struct wait_queue *tmp; - kTask_t * p; - - if (!q || !(tmp = *q)) - return; - do { - if ((p = tmp->task) != NULL) { - if ((p->state == UNINTERRUPTIBLE) || (p->state == INTERRUPTIBLE)) { - p->state = RUNNING; - if (p->counter > _current->counter) - need_resched = 1; - } - } - if (!tmp->next) { - kprintf("wait_queue is bad (eip = %08lx)\n", ((unsigned long *) q)[-1]); - kprintf(" q = %p\n", q); - kprintf(" *q = %p\n", *q); - kprintf(" tmp = %p\n", tmp); - break; - } - tmp = tmp->next; - } while (tmp != *q); -} diff --git a/sys/i386/schedyield.S b/sys/i386/schedyield.S deleted file mode 100644 index 1072793..0000000 --- a/sys/i386/schedyield.S +++ /dev/null @@ -1,51 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - -.globl sched_yield_new -.text -.code32 -sched_yield_new: - pusha /* Save all of the registers */ - push %ss - push %ds - push %es - push %fs - push %gs - call sched - mov %eax,%esp - pop %gs - pop %fs - pop %es - pop %ds - pop %ss - popa /* Restore Registers */ - iret - -/*** - END - ***/ diff --git a/sys/i386/spinlock.c b/sys/i386/spinlock.c deleted file mode 100644 index db6653d..0000000 --- a/sys/i386/spinlock.c +++ /dev/null @@ -1,71 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - -#include -#include -#include - -#define atomic_xadd(P, V) __sync_fetch_and_add((P), (V)) -#define cmpxchg(P, O, N) __sync_val_compare_and_swap((P), (O), (N)) -#define atomic_inc(P) __sync_add_and_fetch((P), 1) -#define atomic_dec(P) __sync_add_and_fetch((P), -1) -#define atomic_add(P, V) __sync_add_and_fetch((P), (V)) -#define atomic_set_bit(P, V) __sync_or_and_fetch((P), 1<<(V)) -#define atomic_clear_bit(P, V) __sync_and_and_fetch((P), ~(1<<(V))) - -#define barrier() asm volatile("": : :"memory") - -/* Pause instruction to prevent excess processor bus usage */ -#define cpu_relax() asm volatile("pause\n": : :"memory") - -void spinLockInit(spinLock_t lock) { - memset(lock, 0x0, sizeof(struct spinLock)); -} - -int spinTryLock(spinLock_t lock) { - if (!cmpxchg(&lock->locked, NULL, LLOCK_FLAG)) - return 0; - - /* Failure! */ - return LOCKED; -} - -void spinUnlock(spinLock_t lock) { - barrier(); - lock->locked = 0x0; -} - -void spinLock(spinLock_t lock) { - while (1) { - if (!xchg_32(&lock->locked, LOCKED)) - return; - while (lock->locked == 1) - sched_yield(); - } -} - diff --git a/sys/i386/strcmp.S b/sys/i386/strcmp.S deleted file mode 100644 index 19bb2ff..0000000 --- a/sys/i386/strcmp.S +++ /dev/null @@ -1,98 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - - #include - -ENTRY(strcmp) - movl 0x04(%esp),%eax - movl 0x08(%esp),%edx - jmp L2 /* Jump into the loop! */ - - .align 2,0x90 -L1: incl %eax - incl %edx -L2: movb (%eax),%cl - testb %cl,%cl - je L3 - cmpb %cl,(%edx) - jne L3 - incl %eax - incl %edx - movb (%eax),%cl - testb %cl,%cl - je L3 - cmpb %cl,(%edx) - jne L3 - incl %eax - incl %edx - movb (%eax),%cl - testb %cl,%cl - je L3 - cmpb %cl,(%edx) - jne L3 - incl %eax - incl %edx - movb (%eax),%cl - testb %cl,%cl - je L3 - cmpb %cl,(%edx) - jne L3 - incl %eax - incl %edx - movb (%eax),%cl - testb %cl,%cl - je L3 - cmpb %cl,(%edx) - jne L3 - incl %eax - incl %edx - movb (%eax),%cl - testb %cl,%cl - je L3 - cmpb %cl,(%edx) - jne L3 - incl %eax - incl %edx - movb (%eax),%cl - testb %cl,%cl - je L3 - cmpb %cl,(%edx) - jne L3 - incl %eax - incl %edx - movb (%eax),%cl - testb %cl,%cl - je L3 - cmpb %cl,(%edx) - je L1 - .align 2, 0x90 -L3: movzbl (%eax),%eax /* unsigned comparison */ - movzbl (%edx),%edx - subl %edx,%eax - ret -END(strcmp) diff --git a/sys/i386/strcpy.S b/sys/i386/strcpy.S deleted file mode 100644 index e6f6a0c..0000000 --- a/sys/i386/strcpy.S +++ /dev/null @@ -1,73 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - - #include - - ENTRY(strcpy) - movl 4(%esp),%ecx /* dst address */ - movl 8(%esp),%edx /* src address */ - pushl %ecx /* push dst address */ - - .align 2,0x90 -L1: movb (%edx),%al /* unroll loop, but not too much */ - movb %al,(%ecx) - testb %al,%al - je L2 - movb 1(%edx),%al - movb %al,1(%ecx) - testb %al,%al - je L2 - movb 2(%edx),%al - movb %al,2(%ecx) - testb %al,%al - je L2 - movb 3(%edx),%al - movb %al,3(%ecx) - testb %al,%al - je L2 - movb 4(%edx),%al - movb %al,4(%ecx) - testb %al,%al - je L2 - movb 5(%edx),%al - movb %al,5(%ecx) - testb %al,%al - je L2 - movb 6(%edx),%al - movb %al,6(%ecx) - testb %al,%al - je L2 - movb 7(%edx),%al - movb %al,7(%ecx) - addl $8,%edx - addl $8,%ecx - testb %al,%al - jne L1 -L2: popl %eax /* pop dst address */ - ret -END(strcpy) diff --git a/sys/i386/strncmp.S b/sys/i386/strncmp.S deleted file mode 100644 index 9696281..0000000 --- a/sys/i386/strncmp.S +++ /dev/null @@ -1,129 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - - #include - -ENTRY(strncmp) - pushl %ebx - movl 8(%esp),%eax - movl 12(%esp),%ecx - movl 16(%esp),%edx - testl %edx,%edx - jmp L2 /* Jump into the loop! */ - - .align 2,0x90 -L1: incl %eax - incl %ecx - decl %edx -L2: jz L4 /* strings are equal */ - movb (%eax),%bl - testb %bl,%bl - jz L3 - cmpb %bl,(%ecx) - jne L3 - incl %eax - incl %ecx - decl %edx - jz L4 - movb (%eax),%bl - testb %bl,%bl - jz L3 - cmpb %bl,(%ecx) - jne L3 - - incl %eax - incl %ecx - decl %edx - jz L4 - movb (%eax),%bl - testb %bl,%bl - jz L3 - cmpb %bl,(%ecx) - jne L3 - - incl %eax - incl %ecx - decl %edx - jz L4 - movb (%eax),%bl - testb %bl,%bl - jz L3 - cmpb %bl,(%ecx) - jne L3 - - incl %eax - incl %ecx - decl %edx - jz L4 - movb (%eax),%bl - testb %bl,%bl - jz L3 - cmpb %bl,(%ecx) - jne L3 - - incl %eax - incl %ecx - decl %edx - jz L4 - movb (%eax),%bl - testb %bl,%bl - jz L3 - cmpb %bl,(%ecx) - jne L3 - - incl %eax - incl %ecx - decl %edx - jz L4 - movb (%eax),%bl - testb %bl,%bl - jz L3 - cmpb %bl,(%ecx) - jne L3 - - incl %eax - incl %ecx - decl %edx - jz L4 - movb (%eax),%bl - testb %bl,%bl - jz L3 - cmpb %bl,(%ecx) - je L1 - - .align 2,0x90 -L3: movzbl (%eax),%eax /* unsigned comparison */ - movzbl (%ecx),%ecx - subl %ecx,%eax - popl %ebx - ret - .align 2,0x90 -L4: xorl %eax,%eax - popl %ebx - ret -END(strncmp) diff --git a/sys/i386/support.S b/sys/i386/support.S deleted file mode 100644 index 347cdf8..0000000 --- a/sys/i386/support.S +++ /dev/null @@ -1,120 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - - #include -.text - -// void bzero(void *buf, u_int len) -ENTRY(bzero) - pushl %edi - movl 8(%esp),%edi - movl 12(%esp),%ecx - xorl %eax,%eax - shrl $2,%ecx - cld - rep - stosl - movl 12(%esp),%ecx - andl $3,%ecx - rep - stosb - popl %edi - ret -END(bzero) - -// bcopy(src, dst, cnt) -ENTRY(bcopy) - pushl %ebp - movl %esp,%ebp - pushl %esi - pushl %edi - movl 8(%ebp),%esi - movl 12(%ebp),%edi - movl 16(%ebp),%ecx - - movl %edi,%eax - subl %esi,%eax - cmpl %ecx,%eax /* overlapping && src < dst? */ - jb 1f - - shrl $2,%ecx /* copy by 32-bit words */ - cld /* nope, copy forwards */ - rep - movsl - movl 16(%ebp),%ecx - andl $3,%ecx /* any bytes left? */ - rep - movsb - popl %edi - popl %esi - popl %ebp - ret - - ALIGN_TEXT -1: - addl %ecx,%edi /* copy backwards */ - addl %ecx,%esi - decl %edi - decl %esi - andl $3,%ecx /* any fractional bytes? */ - std - rep - movsb - movl 16(%ebp),%ecx /* copy remainder by 32-bit words */ - shrl $2,%ecx - subl $3,%esi - subl $3,%edi - rep - movsl - popl %edi - popl %esi - cld - popl %ebp - ret -END(bcopy) - -// void *memcpy(const void *dst, const void * src, size_t length) -ENTRY(memcpy) - pushl %edi - pushl %esi - movl 12(%esp),%edi - movl 16(%esp),%esi - movl 20(%esp),%ecx - movl %edi,%eax - shrl $2,%ecx /* copy by 32-bit words */ - cld /* nope, copy forwards */ - rep - movsl - movl 20(%esp),%ecx - andl $3,%ecx /* any bytes left? */ - rep - movsb - popl %esi - popl %edi - ret -END(memcpy) diff --git a/sys/i386/sys_call.S b/sys/i386/sys_call.S deleted file mode 100644 index 7844128..0000000 --- a/sys/i386/sys_call.S +++ /dev/null @@ -1,109 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - -.globl _sys_call -.text -.code32 - -_B4: -_B3: -nop - -_ast: - -_astRet: - -_sysCall_MrOlsen: -//MrOlsen 2018-01-14 push $0x2 -//MrOlsen 2018-01-14 sub $0x4,%esp -pusha -push %ds -push %es -push %fs -push %gs -mov $0x10,%eax -mov %eax,%ds -mov %eax,%es -mov %eax,%fs -cld -push %esp -call sys_call -add $0x4,%esp -cmpb $0x13,0x38(%esp) -je _B4 -testl $0x2000,0x3c(%esp) /* Test If VM */ -jz _notVM -jmp _isVM -_notVM: -testb $0x3,0x38(%esp) /* See If We're In User CS (GDT Entry 5) */ -jz _popFS -jmp _popFS /* TMP BECAUSE ABOVE ISN'T RIGHT */ -cli -mov %fs:0x0,%eax -testl $0x10800,0x80(%eax) /* Document This */ -je _popFS -sti -push %esp -call _ast -add $0x4,%esp -jmp _astRet -_isVM: -hlt - -_popFS: -pop %gs -pop %fs -pop %es -pop %ds -popa -//MrOlsen 2018-01-14 add $0x8,%esp -iret - -_sys_call: -push $0x0 -push $0x80 -pusha -push %ds -push %es -push %fs -push %gs -mov $0x10,%eax -mov %eax, %ds -mov %eax, %es -mov %eax, %fs -cld -push %esp -call sys_call -add $0x4,%esp /* Remove Stack Pointer From Stack */ -pop %gs -pop %fs -pop %es -pop %ds -popa -add $0x8,%esp //Back Out Error Code & trap_no -iret diff --git a/sys/i386/sys_call_posix.S b/sys/i386/sys_call_posix.S deleted file mode 100644 index d9b082f..0000000 --- a/sys/i386/sys_call_posix.S +++ /dev/null @@ -1,57 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - -#define FAKE_MCOUNT(caller) pushl caller ; call __mcount ; popl %ecx - -.globl _sys_call_posix -.text -.code32 - -_sys_call_posix: -push $0x0 -push $0x80 -pusha -push %ds -push %es -push %fs -push %gs -mov $0x10,%eax -mov %eax, %ds -mov %eax, %es -mov %eax, %fs -cld -push %esp -call sys_call_posix -add $0x4,%esp /* Remove Stack Pointer From Stack */ -pop %gs -pop %fs -pop %es -pop %ds -popa -add $0x8,%esp //Back Out Error Code & trap_no -iret diff --git a/sys/i386/systemtask.c b/sys/i386/systemtask.c deleted file mode 100644 index 952fc06..0000000 --- a/sys/i386/systemtask.c +++ /dev/null @@ -1,131 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -static unsigned char *videoBuffer = (unsigned char *) 0xB8000; - -void systemTask() { - - mpi_message_t myMsg; - uint32_t counter = 0x0; - int i = 0x0; - int *x = 0x0; - kTask_t *tmpTask = 0x0; - - char buf[16] = { "a\n" }; - - if (mpi_createMbox("system") != 0x0) { - kpanic("Error: Error creating mailbox: system\n"); - } - - while (1) { - if (mpi_fetchMessage("system", &myMsg) == 0x0) { - switch (myMsg.header) { - case 0x69: - x = (int *) &myMsg.data; - kprintf("Switching to term: [%i][%i]\n", *x, myMsg.pid); - schedFindTask(myMsg.pid)->term = tty_find(*x); - break; - case 1000: - kprintf("Restarting the system in 5 seconds\n"); - counter = systemVitals->sysUptime + 5; - while (systemVitals->sysUptime < counter) { - sched_yield(); - } - kprintf("Rebooting NOW!!!\n"); - while (inportByte(0x64) & 0x02) - ; - outportByte(0x64, 0xFE); - break; - case 31337: - kprintf("system: backdoor opened\n"); - break; - case 0x80: - if (!strcmp(myMsg.data, "sdeStart")) { - kprintf("Starting SDE\n"); - execThread(sdeThread,0x2000,0x0); - } - else if (!strcmp(myMsg.data, "freePage")) { - kprintf("kkk Free Pages"); - } - else if (!strcmp(myMsg.data, "sdeStop")) { - printOff = 0x0; - biosCall(0x10, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0); - for (i = 0x0; i < 100; i++) - asm("hlt"); - } - break; - default: - kprintf("system: Received message %i:%s\n", myMsg.header, myMsg.data); - break; - } - } - - /* - Here we get the next task from the delete task queue - we first check to see if it has an fd attached for the binary and after that - we free the pages for the process and then free the task - */ - tmpTask = sched_getDelTask(); - - if (tmpTask != 0x0) { - if (tmpTask->files[0] != 0x0) - fclose(tmpTask->files[0]); - vmm_freeProcessPages(tmpTask->id); - kfree(tmpTask); - - } - - if (ogprintOff == 1) { - videoBuffer[0] = systemVitals->sysTicks; - videoBuffer[1] = 'c'; - } - /* - else - ogPrintf(buf); - */ - - sched_yield(); - } - - return; -} diff --git a/sys/i386/timer.S b/sys/i386/timer.S deleted file mode 100644 index 1ddfcc1..0000000 --- a/sys/i386/timer.S +++ /dev/null @@ -1,75 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - -.globl timerInt -.text -.code32 -timerInt: - pusha /* Save all of the registers */ - mov $0x20,%dx /* The Following Sends Our EOI To The MPIC */ - mov $0x20,%ax - outb %al,%dx - movl systemVitals,%ecx /* Put Location Of System Vitals Into ECX */ - incl (%ecx) /* Increment sysTicks our 1000ms counter */ - movl (%ecx),%eax /* Increment our sysUptime by 1S if 1000MS */ - movl $200,%ebx /* Have Passed */ - xor %edx,%edx - div %ebx - test %edx,%edx - jnz next - incl 4(%ecx) -next: - movl (%ecx),%eax /* Test If quantum Has Passed If So Then */ - movl 8(%ecx),%ebx /* We Can CALL sched */ - xor %edx,%edx - div %ebx - test %edx,%edx - jnz done -/* -push %ds -push %es -push %fs -push %gs -mov $0x10,%eax -mov %eax, %ds -mov %eax, %es -mov %eax, %fs -cld -*/ -/* push %esp */ - call sched -/* add $0x4,%esp */ /* Remove Stack Pointer From Stack */ -/* -pop %gs -pop %fs -pop %es -pop %ds -*/ -done: - popa /* Restore Registers */ - iret diff --git a/sys/i386/trap.c b/sys/i386/trap.c deleted file mode 100644 index 60f0c65..0000000 --- a/sys/i386/trap.c +++ /dev/null @@ -1,133 +0,0 @@ -/*- - * Copyright (c) 2002-2018 The UbixOS Project. - * All rights reserved. - * - * This was developed by Christopher W. Olsen for the UbixOS Project. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1) Redistributions of source code must retain the above copyright notice, this list of - * conditions, the following disclaimer and the list of authors. - * 2) 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. - * 3) 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 AUTHOR 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. - */ - -#include -#include -#include -#include -#include -#include -#include - -#define FIRST_TSS_ENTRY 6 -#define VM_MASK 0x00020000 - -#define store_TR(n) \ - __asm__("str %%ax\n\t" \ - "subl %2,%%eax\n\t" \ - "shrl $4,%%eax" \ - :"=a" (n) \ - :"0" (0),"i" (FIRST_TSS_ENTRY<<3)) - -#define get_seg_long(seg,addr) ({ \ - register unsigned long __res; \ - __asm__("push %%fs;mov %%ax,%%fs;movl %%fs:%2,%%eax;pop %%fs" \ - :"=a" (__res):"0" (seg),"m" (*(addr))); \ - __res;}) - -#define get_seg_byte(seg,addr) ({ \ -register char __res; \ -__asm__("push %%fs;mov %%ax,%%fs;movb %%fs:%2,%%al;pop %%fs" \ - :"=a" (__res):"0" (seg),"m" (*(addr))); \ -__res;}) - -void die_if_kernel(char *str, struct trapframe *regs, long err) { - int i; - unsigned long esp; - unsigned short ss; - unsigned long *stack; - - esp = (unsigned long) ®s->tf_esp; - - ss = 0x10; //KERNEL_DS - - //if ((regs->tf_eflags & VM_MASK) || (3 & regs->tf_cs) == 3) - // return; - - if ((regs->tf_cs & 3) == 3) { - esp = regs->tf_esp; - ss = regs->tf_ss; - kprintf("USER TASK!"); - } - else { - ss = 0x10; - } - - kprintf("\n%s: 0x%X:%i, CPU %d, EIP: 0x%X, EFLAGS: 0x%X\n", str, regs->tf_err, regs->tf_trapno, 0x0, regs->tf_eip, regs->tf_eflags); - kprintf("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n", regs->tf_eax, regs->tf_ebx, regs->tf_ecx, regs->tf_edx); - kprintf("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n", regs->tf_esi, regs->tf_edi, regs->tf_ebp, esp); - kprintf("cs: 0x%X ds: 0x%X es: 0x%X fs: 0x%X gs: 0x%X ss: 0x%X\n", regs->tf_cs, regs->tf_ds, regs->tf_es, regs->tf_fs, regs->tf_gs, ss); - kprintf("cr0: 0x%X, cr2: 0x%X, cr3: 0x%X, cr4: 0x%X\n", rcr0(), rcr2(), rcr3(), rcr4()); - - store_TR(i); - kprintf("Process %s (pid: %i, process nr: %d, stackpage=%08lx)\nStack:", _current->name, _current->id, 0xffff & i, esp); - - stack = (unsigned long *) esp; - - for (i = 0; i < 16; i++) { - if (i && ((i % 8) == 0)) - kprintf("\n "); - kprintf("%08lx ", get_seg_long(ss, stack++)); - } - - endTask(_current->id); -} - -void trap(struct trapframe *frame) { - u_int trap_code; - u_int cr2 = 0; - - trap_code = frame->tf_trapno; - - cr2 = rcr2(); - kprintf("CR2: 0x%X(0x%X)[0x%X]", cr2,_current->tss.eip,_current->tss.ldt); - if (_current->id == 7) - while(1) asm("nop"); - - if ((frame->tf_eflags & PSL_I) == 0) { - if (SEL_GET_PL(frame->tf_cs) == SEL_PL_USER || (frame->tf_eflags & PSL_VM)) { - kpanic( "INT OFF! USER" ); - die_if_kernel("TEST", frame, 0x100); - } - else { - kpanic( "INT OFF! KERN[0x%X]", trap_code ); - die_if_kernel("TEST", frame, 0x200); - } - } - - kprintf("trap_code: %i(0x%X), EIP: 0x%X, CR2: 0x%X\n", frame->tf_trapno, frame->tf_trapno, frame->tf_eip, cr2); - if (frame->tf_trapno == 0xc) { - vmm_pageFault(frame, cr2); - } - else { - kpanic("TRAPCODE"); - die_if_kernel("trapCode", frame, frame->tf_trapno); - endTask(_current->id); - sched_yield(); - } -}