diff --git a/sys/include/sys/descrip.h b/sys/include/sys/descrip.h index 9953efd..bb4b9a9 100644 --- a/sys/include/sys/descrip.h +++ b/sys/include/sys/descrip.h @@ -114,6 +114,7 @@ fileDescriptor_t *fd; int fd_type; int socket; + void *data; }; struct fileOps { diff --git a/sys/include/sys/pipe.h b/sys/include/sys/pipe.h index dff67de..4de1505 100644 --- a/sys/include/sys/pipe.h +++ b/sys/include/sys/pipe.h @@ -34,4 +34,22 @@ int pipe(struct thread *, struct pipe_args *); +struct pipeBuf { + struct pipeBuf *next; + char *buffer; + size_t nbytes; + off_t offset; + +}; + +struct pipeInfo { + int rFD; + int rfdCNT; + int wFD; + int wfdCNT; + int bCNT; + struct pipeBuf *headPB; + struct pipeBuf *tailPB; +}; + #endif /* END _SYS_PIPE_H */ diff --git a/sys/kernel/Makefile b/sys/kernel/Makefile index 50d81ae..6d6e4e6 100644 --- a/sys/kernel/Makefile +++ b/sys/kernel/Makefile @@ -6,7 +6,7 @@ include ../Makefile.incl # Objects -OBJS = sem.o vfs_calls.o tty.o kern_sig.o pipe.o descrip.o kern_sysctl.o gen_calls.o endtask.o ld.o time.o elf.o ubthread.o vitals.o access.o syscall.o syscall_posix.o syscalls.o syscalls_posix.o execve.o +OBJS = sem.o vfs_calls.o tty.o kern_sig.o pipe.o descrip.o kern_sysctl.o gen_calls.o endtask.o ld.o time.o elf.o ubthread.o vitals.o access.o syscall.o syscall_posix.o syscalls.o syscalls_posix.o execve.o kern_pipe.o #OBJS += ../${_ARCH}/schedyield.o ../${_ARCH}/kpanic.o ../${_ARCH}/timer.o ../${_ARCH}/spinlock.o ../${_ARCH}/exec.o ../${_ARCH}/sys_call_new.o ../${_ARCH}/sys_call.o ../${_ARCH}/bioscall.o ../${_ARCH}/fork.o ../${_ARCH}/syscall.o ../${_ARCH}/systemtask.o ../${_ARCH}/sched.o ../${_ARCH}/cpu.o # ap-boot.o smp.o vitals.o(obsolete) diff --git a/sys/kernel/kern_pipe.c b/sys/kernel/kern_pipe.c new file mode 100644 index 0000000..4aef6a2 --- /dev/null +++ b/sys/kernel/kern_pipe.c @@ -0,0 +1,70 @@ +/*- + * 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 + +int sys_pipe2(struct thread *thr, struct sys_pipe2_args *args) { + int error = 0x0; + + int fd1 = 0x0; + int fd2 = 0x0; + + struct file *nfp1 = 0x0; + struct file *nfp2 = 0x0; + + struct pipeInfo *pipeDesc = kmalloc(sizeof(struct pipeInfo)); + memset(pipeDesc, 0x0, sizeof(struct pipeInfo)); + + error = falloc(thr, &nfp1, &fd1); + error = falloc(thr, &nfp2, &fd2); + + nfp1->data = pipeDesc; + nfp2->data = pipeDesc; + + nfp1->fd_type = 3; + nfp2->fd_type = 3; + + pipeDesc->rFD = fd1; + pipeDesc->rfdCNT = 2; + pipeDesc->wFD = fd2; + pipeDesc->wfdCNT = 2; + + args->fildes[0] = fd1; + args->fildes[1] = fd2; + + thr->td_retval[0] = 0; + + return (0x0); +} diff --git a/sys/kernel/vfs_calls.c b/sys/kernel/vfs_calls.c index 548e287..fe752c1 100644 --- a/sys/kernel/vfs_calls.c +++ b/sys/kernel/vfs_calls.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -117,7 +118,9 @@ getfd(td, &fd, args->fd); +#ifdef DEBUG_VFS_CALLS kprintf("[sC:%i:0x%X:0x%X]", args->fd, fd, fd->fd); +#endif if (fd == 0x0) { td->td_retval[0] = -1; @@ -150,6 +153,9 @@ struct file *fd = 0x0; + struct pipeInfo *pFD = 0x0; + struct pipeInfo *rpFD = 0x0; + size_t nbytes; getfd(td, &fd, args->fd); @@ -157,11 +163,22 @@ if (args->fd > 3) { switch (fd->fd_type) { case 3: /* XXX - Pipe2 Handling */ - nbytes = (args->nbyte - (1024 - fd->fd->offset) < 0) ? args->nbyte : (1024 - fd->fd->offset); - kprintf("[unb: %i, nbs: %i, bf: 0x%X]", args->nbyte, nbytes, fd->fd->buffer); - kprintf("PR: [%i]", nbytes); - memcpy(args->buf, fd->fd->buffer + fd->fd->offset, nbytes); - fd->fd->offset += nbytes; + pFD = fd->data; + while (pFD->bCNT == 0) + sched_yield(); + + nbytes = (args->nbyte - (pFD->headPB->nbytes - pFD->headPB->offset) <= 0) ? args->nbyte : (pFD->headPB->nbytes - pFD->headPB->offset); + //kprintf("[unb: %i, nbs: %i, bf: 0x%X]", args->nbyte, nbytes, fd->fd->buffer); + //kprintf("PR: [%i]", nbytes); + memcpy(args->buf, pFD->headPB->buffer + pFD->headPB->offset, nbytes); + pFD->headPB->offset += nbytes; + + if (pFD->headPB->offset >= pFD->headPB->nbytes) { + rpFD = pFD->headPB; + pFD->headPB = pFD->headPB->next; + kfree(rpFD); + } + td->td_retval[0] = nbytes; break; default: @@ -263,6 +280,9 @@ char *buffer = 0x0; struct file *fd = 0x0; + struct pipeInfo *pFD = 0x0; + struct pipeBuf *pBuf = 0x0; + size_t nbytes; if (uap->fd == 2) { @@ -288,12 +308,23 @@ switch (fd->fd_type) { case 3: /* XXX - Temp Pipe Stuff */ - nbytes = (uap->nbyte - (1024 - fd->fd->offset) < 0) ? uap->nbyte : (1024 - fd->fd->offset); - kprintf("[unb: %i, nbs: %i, bf: 0x%X]", uap->nbyte, nbytes, fd->fd->buffer); - memcpy(fd->fd->buffer + fd->fd->offset, uap->buf, nbytes); - fd->fd->offset += nbytes; + pFD = fd->data; + pBuf = (struct pipeBuf *) kmalloc(sizeof(struct pipeBuf)); + pBuf->buffer = kmalloc(uap->nbyte); + + //kprintf("[unb: %i, nbs: %i, bf: 0x%X]", uap->nbyte, nbytes, fd->fd->buffer); + memcpy(pBuf->buffer, uap->buf, nbytes); + + if (pFD->tailPB) + pFD->tailPB->next = pBuf; + + pFD->tailPB = pBuf; + + if (!pFD->headPB) + pFD->headPB = pBuf; + td->td_retval[0] = nbytes; - kprintf("[PW: %i:%i]", nbytes, fd->fd->offset); + //kprintf("[PW: %i:%i]", nbytes, fd->fd->offset); break; default: kprintf("[%i]", uap->nbyte); @@ -351,37 +382,3 @@ thr->td_retval[0] = 2; return (-1); } - -int sys_pipe2(struct thread *thr, struct sys_pipe2_args *args) { - int error = 0x0; - - int fd1 = 0x0; - int fd2 = 0x0; - - struct file *nfp1 = 0x0; - struct file *nfp2 = 0x0; - - fileDescriptor_t *pipeDesc1 = kmalloc(sizeof(fileDescriptor_t)); - fileDescriptor_t *pipeDesc2 = kmalloc(sizeof(fileDescriptor_t)); - - error = falloc(thr, &nfp1, &fd1); - error = falloc(thr, &nfp2, &fd2); - - nfp1->fd = pipeDesc1; - nfp2->fd = pipeDesc2; - - pipeDesc1->buffer = kmalloc(1024); - pipeDesc2->buffer = pipeDesc1->buffer; - - nfp1->fd_type = 3; - nfp2->fd_type = 3; - - args->fildes[0] = fd1; - args->fildes[1] = fd2; - - kprintf("P2: %i, fd1: %i:0x%X, fd1t: %i, fd2: %i:0x%X, fd2t: %i\n", args->flags, fd1, nfp1, nfp1->fd_type, fd2, nfp2, nfp2->fd_type); - - thr->td_retval[0] = 0; - - return (0x0); -}