6 #include </sys/src/libc/9syscall/sys.h>
11 /* no locking necessary, since we're on the stack */
12 return *(u32int*) vaddrnol(P->R[13] + 4 + 4 * n, 4);
18 return arg(n) | ((u64int)arg(n+1) << 32);
30 namet = copyifnec(name, -1, &copied);
32 fprint(2, "open(%#ux=\"%s\", %#o)\n", name, namet, flags);
33 fd = open(namet, flags);
41 setcexec(P->fd, fd, flags & OCEXEC);
48 u32int name, flags, perm;
55 namet = copyifnec(name, -1, &copied);
57 fprint(2, "create(%#ux=\"%s\", %#o, %o)\n", name, namet, flags, perm);
58 fd = create(namet, flags, perm);
66 setcexec(P->fd, fd, flags & OCEXEC);
77 fprint(2, "close(%d)\n", fd);
78 P->R[0] = noteerr(close(fd), 0);
79 if((fd & (1<<31)) == 0)
80 setcexec(P->fd, fd, 0);
96 fprint(2, "pread(%d, %#ux, %ud, %#ullx)\n", fd, buf, size, off);
97 targ = bufifnec(buf, size, &buffered);
98 P->R[0] = noteerr(pread(fd, targ, size, off), size);
100 copyback(buf, P->R[0], targ);
106 u32int fd, size, buf;
115 buft = copyifnec(buf, size, &copied);
117 fprint(2, "pwrite(%d, %#ux, %ud, %#ullx)\n", fd, buf, size, off);
118 P->R[0] = noteerr(pwrite(fd, buft, size, off), size);
130 ret = vaddr(arg(0), 8, &seg);
135 fprint(2, "seek(%d, %lld, %d)\n", fd, n, type);
136 *ret = seek(fd, n, type);
137 if(*ret < 0) noteerr(0, 1);
144 u32int fd, buf, nbuf;
151 buft = bufifnec(buf, nbuf, &buffered);
153 fprint(2, "fd2path(%d, %#ux, %d)\n", fd, buf, nbuf);
154 P->R[0] = noteerr(fd2path(fd, buft, nbuf), 0);
156 copyback(buf, nbuf, buft);
162 u32int name, edir, nedir;
165 int copied, buffered;
168 namet = copyifnec(name, -1, &copied);
171 edirt = bufifnec(edir, nedir, &buffered);
173 fprint(2, "stat(%#ux=\"%s\", %#ux, %ud)\n", name, namet, edir, nedir);
174 P->R[0] = noteerr(stat(namet, edirt, nedir), nedir);
178 copyback(edir, P->R[0], edirt);
184 u32int fd, edir, nedir;
191 edirt = bufifnec(edir, nedir, &buffered);
193 fprint(2, "fstat(%d, %#ux, %d)\n", fd, edir, nedir);
194 P->R[0] = noteerr(fstat(fd, edirt, nedir), nedir);
196 copyback(edir, P->R[0], edirt);
202 u32int name, edir, nedir;
208 namet = copyifnec(name, -1, &copied);
211 edirt = copyifnec(edir, nedir, &copied2);
213 fprint(2, "wstat(%#ux=\"%s\", %#ux, %ud)\n", name, namet, edir, nedir);
214 P->R[0] = noteerr(wstat(namet, edirt, nedir), nedir);
224 u32int fd, edir, nedir;
231 edirt = copyifnec(edir, nedir, &copied);
233 fprint(2, "fwstat(%d, %#ux, %d)\n", fd, edir, nedir);
234 P->R[0] = noteerr(fwstat(fd, edirt, nedir), nedir);
245 exits(vaddrnol(arg(0), 0));
255 if(v >= P->S[SEGSTACK]->start)
256 sysfatal("bss > stack, wtf?");
257 if(v < P->S[SEGBSS]->start)
258 sysfatal("bss length < 0, wtf?");
261 s->dref = realloc(s->dref, v - s->start + 4);
263 sysfatal("error reallocating");
264 s->data = s->dref + 1;
265 if(s->size < v - s->start)
266 memset((char*)s->data + s->size, 0, v - s->start - s->size);
267 s->size = v - s->start;
275 char buf[ERRMAX], *srct;
281 srct = copyifnec(src, len, &copied);
282 strcpy(buf, P->errbuf);
283 utfecpy(P->errbuf, P->errbuf + ERRMAX, srct);
284 utfecpy(srct, srct + len, buf);
286 copyback(src, len, srct);
298 dirt = copyifnec(dir, -1, &copied);
300 fprint(2, "chdir(%#ux=\"%s\")\n", dir, dirt);
301 P->R[0] = noteerr(chdir(dirt), 0);
313 fprint(2, "notify(%#ux)\n", handler);
314 P->notehandler = handler;
325 fprint(2, "noted(%d)\n", v);
327 longjmp(P->notejmp, v + 1);
328 cherrstr("the front fell off");
343 fprint(2, "rfork(%#o)\n", flags);
344 if((flags & (RFFDG | RFCFDG)) == (RFFDG | RFCFDG) ||
345 (flags & (RFNAMEG | RFCNAMEG)) == (RFNAMEG | RFCNAMEG) ||
346 (flags & (RFENVG | RFCENVG)) == (RFENVG | RFCENVG)) {
348 cherrstr("bad arg in syscall");
351 if((flags & RFPROC) == 0) {
354 P->fd = copyfd(P->fd);
362 P->R[0] = noteerr(rfork(flags), 0);
366 p = emallocz(sizeof(Process));
367 memcpy(p, P, sizeof(Process));
368 for(i = 0; i < SEGNUM; i++) {
372 if((flags & RFMEM) == 0 && i != SEGTEXT || i == SEGSTACK) {
373 t = emallocz(sizeof(Segment));
377 t->dref = emalloc(sizeof(Ref) + s->size);
378 memset(t->dref, 0, sizeof(Ref));
380 t->data = t->dref + 1;
381 memcpy(t->data, s->data, s->size);
390 p->fd = copyfd(P->fd);
391 else if(flags & RFCFDG)
397 rc = rfork(RFMEM | flags);
398 if(rc < 0) /* this should NEVER happen */
399 sysfatal("rfork failed wtf: %r");
413 u32int name, argv, *argvt;
414 char *namet, **argvv;
416 Segment *seg1, *seg2;
420 namet = strdup(vaddr(name, 0, &seg1));
422 argvt = vaddr(argv, 0, &seg1);
424 fprint(2, "exec(%#ux=\"%s\", %#ux)\n", name, namet, argv);
425 for(argc = 0; argvt[argc]; argc++)
427 argvv = emalloc(sizeof(char *) * argc);
428 for(i = 0; i < argc; i++) {
429 argvv[i] = strdup(vaddr(argvt[i], 0, &seg2));
433 rc = loadtext(namet, argc, argvv);
434 for(i = 0; i < argc; i++)
438 P->R[0] = noteerr(rc, 0);
451 st = bufifnec(s, n, &buffered);
453 fprint(2, "await(%#ux, %d)\n", s, n);
454 P->R[0] = noteerr(await(st, n), 0);
456 copyback(s, P->R[0], st);
467 fprint(2, "pipe(%#ux)\n", fd);
468 fdt = bufifnec(fd, 8, &buffered);
469 P->R[0] = noteerr(pipe((int *) fdt), 0);
471 copyback(fd, 8, fdt);
482 fprint(2, "dup(%d, %d)\n", oldfd, newfd);
483 P->R[0] = noteerr(dup(oldfd, newfd), 0);
493 fprint(2, "sleep(%d)\n", n);
494 P->R[0] = noteerr(sleep(n), 0);
505 fprint(2, "rendezvous(%#ux, %#ux)\n", tag, value);
506 P->R[0] = (u32int) rendezvous((void *) tag, (void *) value);
514 u32int fd, afd, old, flag, aname;
516 int copiedold, copiedaname;
523 oldt = copyifnec(old, -1, &copiedold);
525 anamet = copyifnec(aname, -1, &copiedaname);
527 fprint(2, "mount(%d, %d, %#x=\"%s\", %#o, %#x=\"%s\")\n", fd, afd, old, oldt, flag, aname, anamet);
532 fprint(2, "mount(%d, %d, %#x=\"%s\", %#o, nil)\n", fd, afd, old, oldt, flag);
534 P->R[0] = noteerr(mount(fd, afd, oldt, flag, anamet), 0);
544 u32int name, old, flags;
546 int copiedname, copiedold;
551 namet = copyifnec(name, -1, &copiedname);
552 oldt = copyifnec(old, -1, &copiedold);
554 fprint(2, "bind(%#ux=\"%s\", %#ux=\"%s\", %#o)\n", name, namet, old, oldt, flags);
555 P->R[0] = noteerr(bind(namet, oldt, flags), 0);
567 int copiedname, copiedold;
571 oldt = copyifnec(old, -1, &copiedold);
576 fprint(2, "unmount(nil, %#ux=\"%s\")\n", old, oldt);
577 P->R[0] = noteerr(unmount(nil, oldt), 0);
579 namet = copyifnec(name, -1, &copiedname);
581 fprint(2, "unmount(%#ux=\"%s\", %#ux=\"%s\")\n", name, namet, old, oldt);
582 P->R[0] = noteerr(unmount(namet, oldt), 0);
598 filet = copyifnec(file, -1, &copied);
600 fprint(2, "remove(%#ux=\"%s\")\n", file, filet);
601 P->R[0] = noteerr(remove(filet), 0);
613 fprint(2, "alarm(%d)\n", msec);
614 P->R[0] = alarm(msec);
626 fprint(2, "semacquire(%#ux, %ud)\n", addr, block);
627 addrt = vaddrnol(addr, 4);
628 P->R[0] = noteerr(semacquire(addrt, block), 0);
641 fprint(2, "semrelease(%#ux, %ud)\n", addr, count);
642 addrt = vaddr(addr, 4, &seg);
643 P->R[0] = noteerr(semrelease(addrt, count), 0);
651 static void (*calls[])(void) = {
666 [FD2PATH] sysfd2path,
674 [RENDEZVOUS] sysrendezvous,
676 [UNMOUNT] sysunmount,
681 [SEMACQUIRE] syssemacquire,
682 [SEMRELEASE] syssemrelease,
686 if(n >= nelem(calls) || calls[n] == nil)
687 sysfatal("no such syscall %d @ %#ux", n, P->R[15] - 4);