Exemple #1
0
int
_tread(Fd *f, void *a, int n)
{
	int m;

	assert(f->mode == OREAD);

//fprint(2, "%s: tread %p %d\n", argv0, f, n);
	if(f->p == f->ep){
		f->p = f->buf;
		f->ep = f->buf;
//fprint(2, "%s: ioread %p %d\n", argv0, f, FdSize);
		dbg(DbgFdbuf, "_treading fd=%d n=%d\n", f->fd, n);
		m = ioread(f->io, f->fd, f->ep, FdSize);
//Xfprint(2, "%s: ioread %p got %d\n", argv0, f, m);
		dbg(DbgFdbuf, "_tread fd=%d n=%d m=%d\n", f->fd, n, m);
		if(m <= 0)
			return m;
		f->ep += m;
	}
	m = f->ep - f->p;
	if(m > n)
		m = n;
	memmove(a, f->p, m);
	f->p += m;
//fprint(2, "%s: tread %p return %d\n", argv0, f, m);
	return m;
}
Exemple #2
0
static void
readthread(void *a)
{
	uint8_t *buf;
	int n;
	Client *c;
	Ioproc *io;
	Msg *m;
	char tmp[32];

	c = a;
	snprint(tmp, sizeof tmp, "read%d", c->num);
	threadsetname(tmp);

	buf = emalloc(8192);
	io = c->readerproc;
	while((n = ioread(io, c->fd[0], buf, 8192)) >= 0){
		m = emalloc(sizeof(Msg)+n);
		m->rp = (uint8_t*)&m[1];
		m->ep = m->rp + n;
		if(n)
			memmove(m->rp, buf, n);
		queuemsg(c, m);
		matchmsgs(c);
	}
	queuemsg(c, Zmsg);
	free(buf);
	die(c);
}
Exemple #3
0
INT32 elf_readphdrs(struct pm_task *task, INT32 (*ioread)(struct fsio_event_source *iosrc, UINT32 size, ADDR ptr), INT32 (*ioseek)(struct fsio_event_source *iosrc, UINT32 offset))
{
	UINT32 size;

	// seek to program headers possition
	size = task->loader_inf.elf_header.e_phentsize * task->loader_inf.elf_header.e_phnum;

	task->loader_inf.elf_pheaders = (BYTE*)kmalloc(size);
    if(task->loader_inf.elf_pheaders == NULL)
    {
        return -1;
    }

    if(task->loader_inf.elf_pheaders == NULL)
    {
        return -1;
    }
    
	task->io_finished.callback = elf_readh_finished_callback;

	if(ioread(&task->io_event_src, size, task->loader_inf.elf_pheaders))
	{
        if(!elf_check(task)) return -1;

		task->loader_inf.stage = LOADER_STAGE_LOADED;
        return 1;
	}

	return 0;
}
Exemple #4
0
/* Begin elf parsing. This function takes a task id and a function pointer, to an io function.
The io function will return 1 if data is already present, or 0 y we have to wait for a response.
This function will return -1 if it's not a valid elf file. */
INT32 elf_begin(struct pm_task *task, INT32 (*ioread)(struct fsio_event_source *iosrc, UINT32 size, ADDR ptr), INT32 (*ioseek)(struct fsio_event_source *iosrc, UINT32 offset))
{
	/* read header */
	task->loader_inf.stage = LOADER_STAGE_ELF_HDR;

	task->io_finished.callback = elf_read_finished_callback;

	if(ioread(&task->io_event_src, sizeof(struct Elf32_Ehdr),(char*)&task->loader_inf.elf_header))
	{
		if(!elf_check_header(task)) return -1;

		// data is already present, read program headers
		return elf_seekphdrs(task, ioread, ioseek);
	}
	// if data is not present, leave, for elf_request will continue
	return 0;
}
Exemple #5
0
int
readline(Ibuf *b, char *buf, int len)
{
	int n;
	char *p;

	len--;

	for(p = buf;;){
		if(b->rp >= b->wp){
			n = ioread(b->io, b->fd, b->wp, sizeof(b->buf)/2);
			if(n < 0)
				return -1;
			if(n == 0)
				break;
			b->wp += n;
		}
		n = *b->rp++;
		if(len > 0){
			*p++ = n;
			len--;
		}
		if(n == '\n')
			break;
	}

	/* drop trailing white */
	for(;;){
		if(p <= buf)
			break;
		n = *(p-1);
		if(n != ' ' && n != '\t' && n != '\r' && n != '\n')
			break;
		p--;
	}

	*p = 0;
	return p-buf;
}
Exemple #6
0
static void
openfdthread(void *v)
{
	Conn *c;
	Fid *fid;
	Msg *m;
	int n;
	vlong tot;
	Ioproc *io;
	char buf[1024];

	c = v;
	fid = c->fdfid;
	io = ioproc();
	threadsetname("openfd %s", c->fdfid);
	tot = 0;
	m = nil;
	if(c->fdmode == OREAD){
		for(;;){
			if(verbose) fprint(2, "%T tread...");
			m = msgnew(0);
			m->internal = 1;
			m->c = c;
			m->tx.type = Tread;
			m->tx.count = msize - IOHDRSZ;
			m->tx.fid = fid->fid;
			m->tx.tag = m->tag;
			m->tx.offset = tot;
			m->fid = fid;
			fid->ref++;
			msgincref(m);
			sendomsg(m);
			recvp(c->internal);
			if(m->rx.type == Rerror){
			/*	fprint(2, "%T read error: %s\n", m->rx.ename); */
				break;
			}
			if(m->rx.count == 0)
				break;
			tot += m->rx.count;
			if(iowrite(io, c->fd, m->rx.data, m->rx.count) != m->rx.count){
				/* fprint(2, "%T pipe write error: %r\n"); */
				break;
			}
			msgput(m);
			msgput(m);
			m = nil;
		}
	}else{
		for(;;){
			if(verbose) fprint(2, "%T twrite...");
			n = sizeof buf;
			if(n > msize)
				n = msize;
			if((n=ioread(io, c->fd, buf, n)) <= 0){
				if(n < 0)
					fprint(2, "%T pipe read error: %r\n");
				break;
			}
			m = msgnew(0);
			m->internal = 1;
			m->c = c;
			m->tx.type = Twrite;
			m->tx.fid = fid->fid;
			m->tx.data = buf;
			m->tx.count = n;
			m->tx.tag = m->tag;
			m->tx.offset = tot;
			m->fid = fid;
			fid->ref++;
			msgincref(m);
			sendomsg(m);
			recvp(c->internal);
			if(m->rx.type == Rerror){
			/*	fprint(2, "%T write error: %s\n", m->rx.ename); */
			}
			tot += n;
			msgput(m);
			msgput(m);
			m = nil;
		}
	}
	if(verbose) fprint(2, "%T eof on %d fid %d\n", c->fd, fid->fid);
	close(c->fd);
	closeioproc(io);
	if(m){
		msgput(m);
		msgput(m);
	}
	if(verbose) fprint(2, "%T eof on %d fid %d ref %d\n", c->fd, fid->fid, fid->ref);
	if(--fid->openfd == 0){
		m = msgnew(0);
		m->internal = 1;
		m->c = c;
		m->tx.type = Tclunk;
		m->tx.tag = m->tag;
		m->tx.fid = fid->fid;
		m->fid = fid;
		fid->ref++;
		msgincref(m);
		sendomsg(m);
		recvp(c->internal);
		msgput(m);
		msgput(m);
	}
	fidput(fid);
	c->fdfid = nil;
	chanfree(c->internal);
	c->internal = 0;
	free(c);
}			
Exemple #7
0
void io_handle(int (*io_read)(), int (*io_write)(), int (*io_pend)(),
	       void *priv)
{
  void *addr;
  int  err,res;
  char c;
  int count,rc_count;
  w32 qaddr;
  int op=(w8)reg[0];
 
  reg[0]=0;

#ifdef IOTEST
printf("call io_handle \t\td0=%d\td1=%x\td2=%x\td3=%x\ta1=%x\n",op,reg[1],reg[2],reg[3],aReg[1]);
#endif
  
  switch (op)
    {
    case 0: 
      *reg=(*io_pend)(priv);
      break;
      
    case 1:
      res=(*io_read)(priv,&c,1);
      if (res==1)
      *((char*)reg+4+RBO)=c;
      else *reg=res ? res: QERR_EOF;
      break;
      
    case 2:                   /* read line */
      count=max(0,(uw16)reg[2]-(uw16)reg[1]);
      rc_count=(uw16)reg[1];
      qaddr=aReg[1];
      prepChangeMem(aReg[1],aReg[1]+rc_count);
      ioread(io_read,priv,qaddr,&count,true);
      //(uw16)reg[1]=count+rc_count;
      SETREG16(reg[1], count+rc_count);
      aReg[1]=qaddr+count;
      break;

    case 3:			/* fetch string */
      qaddr=aReg[1];
      count=max(0,(uw16)reg[2]-(uw16)reg[1]);
      rc_count=(uw16)reg[1];
      prepChangeMem(aReg[1],aReg[1]+rc_count);
      ioread(io_read,priv,qaddr,&count,false);
      reg[1]=count+rc_count;
      aReg[1]=qaddr+count;
      break;

    case 5:
      res=(*io_write)(priv,(Ptr)reg+4+RBO,1);
      if (res<0) *reg=res;
      break;
      
    case 7:			/* send string */
      count=(uw16)reg[2];
      res=(*io_write)(priv,(Ptr)theROM+aReg[1],count);
      if (res<0)
	{
	  count=0;
	  *reg=res;
	} else count=res;
      
      reg[1]=count;
      aReg[1]+=count;
      break;		
      
    case 0x48:		/* read file into memory */
      qaddr=aReg[1];
      count=reg[2];
      rc_count=reg[1];
      prepChangeMem(aReg[1],aReg[1]+rc_count);
      ioread(io_read,priv,qaddr,&count,false);
      aReg[1]=qaddr+count;
      break;
      
    case 0x49:
      count=reg[2];
      res=(*io_write)(priv,(Ptr)theROM+aReg[1],count);
      if (res<0)
	{
	  count=0;
	  *reg=res;
	} else count=res;
      
      aReg[1]+=count;
      break;
      
    default: 
      *reg=QERR_BP;
      break;
    }
#ifdef IOTEST
printf("ret from io_handle \td0=%d\td1=%x\t\ta1=%x\n",reg[0],reg[1],aReg[1]);
#endif
}
Exemple #8
0
int
read(void *addr, int count)
{
	return(ioread(addr, count, 0));
}
Exemple #9
0
int
xread(void *addr, int count)
{
	return(ioread(addr, count, 1));
}