2006-06-27 16:35:53 +02:00
|
|
|
#include "types.h"
|
2019-05-31 17:45:42 +02:00
|
|
|
#include "riscv.h"
|
2007-08-28 01:26:33 +02:00
|
|
|
#include "defs.h"
|
2006-06-27 16:35:53 +02:00
|
|
|
#include "param.h"
|
2019-07-02 15:14:47 +02:00
|
|
|
#include "spinlock.h"
|
2006-06-27 16:35:53 +02:00
|
|
|
#include "proc.h"
|
2009-08-08 10:07:30 +02:00
|
|
|
#include "fs.h"
|
2016-09-12 02:59:57 +02:00
|
|
|
#include "sleeplock.h"
|
|
|
|
#include "file.h"
|
2006-06-27 16:35:53 +02:00
|
|
|
|
|
|
|
#define PIPESIZE 512
|
|
|
|
|
|
|
|
struct pipe {
|
2006-07-12 03:48:35 +02:00
|
|
|
struct spinlock lock;
|
2024-06-15 16:55:06 +02:00
|
|
|
char data[PIPESIZE];
|
|
|
|
u32 nread; // number of bytes read
|
|
|
|
u32 nwrite; // number of bytes written
|
|
|
|
int readopen; // read fd is still open
|
|
|
|
int writeopen; // write fd is still open
|
2006-06-27 16:35:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2007-08-28 06:22:35 +02:00
|
|
|
pipealloc(struct file **f0, struct file **f1)
|
2006-06-27 16:35:53 +02:00
|
|
|
{
|
2019-07-20 23:07:20 +02:00
|
|
|
struct pipe *pi;
|
2006-06-27 16:35:53 +02:00
|
|
|
|
2019-07-20 23:07:20 +02:00
|
|
|
pi = 0;
|
2007-08-10 18:35:01 +02:00
|
|
|
*f0 = *f1 = 0;
|
2007-08-27 18:06:19 +02:00
|
|
|
if((*f0 = filealloc()) == 0 || (*f1 = filealloc()) == 0)
|
|
|
|
goto bad;
|
2024-06-15 16:55:06 +02:00
|
|
|
if((pi = (struct pipe *)kalloc()) == 0)
|
2007-08-27 18:06:19 +02:00
|
|
|
goto bad;
|
2019-07-20 23:07:20 +02:00
|
|
|
pi->readopen = 1;
|
|
|
|
pi->writeopen = 1;
|
|
|
|
pi->nwrite = 0;
|
|
|
|
pi->nread = 0;
|
|
|
|
initlock(&pi->lock, "pipe");
|
2006-09-08 15:44:42 +02:00
|
|
|
(*f0)->type = FD_PIPE;
|
|
|
|
(*f0)->readable = 1;
|
|
|
|
(*f0)->writable = 0;
|
2019-07-20 23:07:20 +02:00
|
|
|
(*f0)->pipe = pi;
|
2006-09-08 15:44:42 +02:00
|
|
|
(*f1)->type = FD_PIPE;
|
|
|
|
(*f1)->readable = 0;
|
|
|
|
(*f1)->writable = 1;
|
2019-07-20 23:07:20 +02:00
|
|
|
(*f1)->pipe = pi;
|
2006-06-27 16:35:53 +02:00
|
|
|
return 0;
|
2007-08-14 20:42:34 +02:00
|
|
|
|
2024-06-15 16:55:06 +02:00
|
|
|
bad:
|
2019-07-20 23:07:20 +02:00
|
|
|
if(pi)
|
2024-06-15 16:55:06 +02:00
|
|
|
kfree((char *)pi);
|
2009-05-31 04:07:51 +02:00
|
|
|
if(*f0)
|
2006-09-08 15:44:42 +02:00
|
|
|
fileclose(*f0);
|
2009-05-31 04:07:51 +02:00
|
|
|
if(*f1)
|
2006-09-08 15:44:42 +02:00
|
|
|
fileclose(*f1);
|
2006-06-27 16:35:53 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-07-20 23:07:20 +02:00
|
|
|
pipeclose(struct pipe *pi, int writable)
|
2006-06-27 16:35:53 +02:00
|
|
|
{
|
2019-07-20 23:07:20 +02:00
|
|
|
acquire(&pi->lock);
|
2024-06-15 16:55:06 +02:00
|
|
|
if(writable) {
|
2019-07-20 23:07:20 +02:00
|
|
|
pi->writeopen = 0;
|
|
|
|
wakeup(&pi->nread);
|
2007-08-28 20:37:41 +02:00
|
|
|
} else {
|
2019-07-20 23:07:20 +02:00
|
|
|
pi->readopen = 0;
|
|
|
|
wakeup(&pi->nwrite);
|
2006-07-01 23:26:01 +02:00
|
|
|
}
|
2024-06-15 16:55:06 +02:00
|
|
|
if(pi->readopen == 0 && pi->writeopen == 0) {
|
2019-07-20 23:07:20 +02:00
|
|
|
release(&pi->lock);
|
2024-06-15 16:55:06 +02:00
|
|
|
kfree((char *)pi);
|
2008-10-15 19:42:56 +02:00
|
|
|
} else
|
2019-07-20 23:07:20 +02:00
|
|
|
release(&pi->lock);
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2024-05-24 11:26:40 +02:00
|
|
|
pipewrite(struct pipe *pi, u64 addr, int n)
|
2006-06-27 16:35:53 +02:00
|
|
|
{
|
2024-06-15 16:55:06 +02:00
|
|
|
int i = 0;
|
2019-06-04 11:57:47 +02:00
|
|
|
struct proc *pr = myproc();
|
2006-06-27 16:35:53 +02:00
|
|
|
|
2019-07-20 23:07:20 +02:00
|
|
|
acquire(&pi->lock);
|
2024-06-15 16:55:06 +02:00
|
|
|
while(i < n) {
|
|
|
|
if(pi->readopen == 0 || killed(pr)) {
|
2020-10-22 12:36:36 +02:00
|
|
|
release(&pi->lock);
|
|
|
|
return -1;
|
|
|
|
}
|
2024-06-15 16:55:06 +02:00
|
|
|
if(pi->nwrite == pi->nread + PIPESIZE) { // DOC: pipewrite-full
|
2019-07-20 23:07:20 +02:00
|
|
|
wakeup(&pi->nread);
|
2019-08-20 00:12:19 +02:00
|
|
|
sleep(&pi->nwrite, &pi->lock);
|
2020-10-22 12:36:36 +02:00
|
|
|
} else {
|
|
|
|
char ch;
|
|
|
|
if(copyin(pr->pagetable, &ch, addr + i, 1) == -1)
|
|
|
|
break;
|
|
|
|
pi->data[pi->nwrite++ % PIPESIZE] = ch;
|
|
|
|
i++;
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-20 00:12:19 +02:00
|
|
|
wakeup(&pi->nread);
|
2019-07-20 23:07:20 +02:00
|
|
|
release(&pi->lock);
|
2020-10-22 12:36:36 +02:00
|
|
|
|
2020-08-07 21:06:43 +02:00
|
|
|
return i;
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2024-05-24 11:26:40 +02:00
|
|
|
piperead(struct pipe *pi, u64 addr, int n)
|
2006-06-27 16:35:53 +02:00
|
|
|
{
|
2024-06-15 16:55:06 +02:00
|
|
|
int i;
|
2019-06-04 11:57:47 +02:00
|
|
|
struct proc *pr = myproc();
|
2024-06-15 16:55:06 +02:00
|
|
|
char ch;
|
2006-06-27 16:35:53 +02:00
|
|
|
|
2019-07-20 23:07:20 +02:00
|
|
|
acquire(&pi->lock);
|
2024-06-15 16:55:06 +02:00
|
|
|
while(pi->nread == pi->nwrite && pi->writeopen) { // DOC: pipe-empty
|
|
|
|
if(killed(pr)) {
|
2019-07-20 23:07:20 +02:00
|
|
|
release(&pi->lock);
|
2007-08-27 18:06:19 +02:00
|
|
|
return -1;
|
2006-07-15 14:03:57 +02:00
|
|
|
}
|
2024-06-15 16:55:06 +02:00
|
|
|
sleep(&pi->nread, &pi->lock); // DOC: piperead-sleep
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
2024-06-15 16:55:06 +02:00
|
|
|
for(i = 0; i < n; i++) { // DOC: piperead-copy
|
2019-07-20 23:07:20 +02:00
|
|
|
if(pi->nread == pi->nwrite)
|
2006-06-27 16:35:53 +02:00
|
|
|
break;
|
2019-07-20 23:07:20 +02:00
|
|
|
ch = pi->data[pi->nread++ % PIPESIZE];
|
2019-06-04 11:57:47 +02:00
|
|
|
if(copyout(pr->pagetable, addr + i, &ch, 1) == -1)
|
|
|
|
break;
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
2024-06-15 16:55:06 +02:00
|
|
|
wakeup(&pi->nwrite); // DOC: piperead-wakeup
|
2019-07-20 23:07:20 +02:00
|
|
|
release(&pi->lock);
|
2006-06-27 16:35:53 +02:00
|
|
|
return i;
|
|
|
|
}
|