| plan 9 kernel history: overview | file list | diff list |
1991/0707/port/sysproc.c (diff list | history)
| 1991/0707/sys/src/9/port/sysproc.c:1,551 – 1991/0709/sys/src/9/port/sysproc.c:1,545 (short | long | prev | next) | ||
| 1990/0227 | #include "u.h" #include "lib.h" #include "mem.h" #include "dat.h" #include "fns.h" #include "ureg.h" #include "errno.h" | |
| 1990/0330 | #include <a.out.h> | |
| 1990/0227 | int shargs(char*, int, char**); long sysr1(ulong *arg) { | |
| 1990/1123 | print("[%s %s %d] r1 = %d\n", u->p->pgrp->user, u->p->text, u->p->pid, arg[0]); | |
| 1990/0227 | return 0; } long sysfork(ulong *arg) { Proc *p; | |
| 1991/0705 | Segment *s; | |
| 1990/0227 | Page *np, *op; ulong usp, upa, pid; Chan *c; | |
| 1990/0614 | KMap *k; | |
| 1990/0227 | int n, on, i; int lastvar; /* used to compute stack address */ | |
| 1991/0709 | ||
| 1990/0227 | p = newproc(); | |
| 1991/0705 | /* Page va of upage used as check in mapstack */ | |
| 1990/0227 | p->upage = newpage(1, 0, USERADDR|(p->pid&0xFFFF)); | |
| 1990/0614 | k = kmap(p->upage); upa = VA(k); | |
| 1990/0227 | /* * Save time: only copy u-> data and useful stack */ | |
| 1991/0529 | clearmmucache(); /* so child doesn't inherit any of your mappings */ | |
| 1991/0318 | memmove((void*)upa, u, sizeof(User)); | |
| 1990/0227 | n = USERADDR+BY2PG - (ulong)&lastvar; n = (n+32) & ~(BY2WD-1); /* be safe & word align */ | |
| 1991/0318 | memmove((void*)(upa+BY2PG-n), (void*)(USERADDR+BY2PG-n), n); | |
| 1990/0227 | ((User *)upa)->p = p; | |
| 1990/0614 | kunmap(k); | |
| 1990/0227 | ||
| 1991/0705 | /* Make a new set of memory segments */ for(i = 0; i < NSEG; i++) if(u->p->seg[i]) p->seg[i] = dupseg(u->p->seg[i]); | |
| 1990/0227 | /* * Refs */ | |
| 1991/0705 | incref(u->dot); /* File descriptors etc. */ p->fgrp = dupfgrp(u->p->fgrp); p->pgrp = u->p->pgrp; /* Process groups */ incref(p->pgrp); p->egrp = u->p->egrp; /* Environment group */ incref(p->egrp); | |
| 1990/0227 | /* * Sched */ if(setlabel(&p->sched)){ u->p = p; p->state = Running; p->mach = m; m->proc = p; spllo(); return 0; } | |
| 1991/0705 | ||
| 1990/0227 | p->parent = u->p; p->parentpid = u->p->pid; | |
| 1991/0705 | ||
| 1991/0118 | p->fpstate = u->p->fpstate; | |
| 1990/0324 | u->p->nchild++; | |
| 1990/0227 | pid = p->pid; memset(p->time, 0, sizeof(p->time)); p->time[TReal] = MACHP(0)->ticks; | |
| 1991/0318 | memmove(p->text, u->p->text, NAMELEN); | |
| 1991/0529 | /* * since the bss/data segments are now shareable, * any mmu info about this process is now stale * (i.e. has bad properties) and has to be discarded. */ | |
| 1990/0227 | flushmmu(); | |
| 1990/1211 | clearmmucache(); | |
| 1991/0430 | ready(p); | |
| 1990/0227 | return pid; } long sysexec(ulong *arg) { Proc *p; | |
| 1991/0705 | Segment *s, *ts; | |
| 1990/0227 | ulong l, t, d, b, v; int i; | |
| 1990/08141 | Chan *tc, *c; | |
| 1990/0227 | char **argv, **argp; char *a, *charp, *file; char *progarg[sizeof(Exec)/2+1], elem[NAMELEN]; | |
| 1991/0523 | ulong ssize, spage, nargs, nbytes, n, bssend; | |
| 1990/0227 | ulong *sp; int indir; Exec exec; char line[sizeof(Exec)]; | |
| 1991/0705 | Fgrp *f; Image *img; | |
| 1990/0227 | p = u->p; validaddr(arg[0], 1, 0); file = (char*)arg[0]; indir = 0; Header: tc = namec(file, Aopen, OEXEC, 0); if(waserror()){ close(tc); nexterror(); } if(!indir) strcpy(elem, u->elem); | |
| 1991/0706 | ||
| 1991/0411 | n = (*devtab[tc->type].read)(tc, &exec, sizeof(Exec), 0); | |
| 1990/0227 | if(n < 2) Err: | |
| 1990/11211 | error(Ebadexec); | |
| 1990/1211 | if(n==sizeof(Exec) && exec.magic==AOUT_MAGIC){ | |
| 1990/0227 | if((exec.text&KZERO) || (ulong)exec.entry < UTZERO+sizeof(Exec) || (ulong)exec.entry >= UTZERO+sizeof(Exec)+exec.text) goto Err; goto Binary; } /* * Process #! /bin/sh args ... */ | |
| 1991/0318 | memmove(line, &exec, sizeof(Exec)); | |
| 1990/0227 | if(indir || line[0]!='#' || line[1]!='!') goto Err; n = shargs(line, n, progarg); if(n == 0) goto Err; indir = 1; /* * First arg becomes complete file name */ progarg[n++] = file; progarg[n] = 0; validaddr(arg[1], BY2WD, 1); arg[1] += BY2WD; file = progarg[0]; progarg[0] = elem; poperror(); | |
| 1991/0614 | close(tc); | |
| 1990/0227 | goto Header; Binary: | |
| 1991/0705 | poperror(); | |
| 1990/0227 | t = (UTZERO+sizeof(Exec)+exec.text+(BY2PG-1)) & ~(BY2PG-1); | |
| 1990/1212 | d = (t + exec.data + (BY2PG-1)) & ~(BY2PG-1); | |
| 1990/1211 | bssend = t + exec.data + exec.bss; b = (bssend + (BY2PG-1)) & ~(BY2PG-1); | |
| 1990/0227 | if((t|d|b) & KZERO) | |
| 1990/11211 | error(Ebadexec); | |
| 1990/0227 | /* * Args: pass 1: count */ nbytes = 0; nargs = 0; if(indir){ argp = progarg; while(*argp){ a = *argp++; nbytes += strlen(a) + 1; nargs++; } } evenaddr(arg[1]); argp = (char**)arg[1]; validaddr((ulong)argp, BY2WD, 0); while(*argp){ a = *argp++; if(((ulong)argp&(BY2PG-1)) < BY2WD) validaddr((ulong)argp, BY2WD, 0); validaddr((ulong)a, 1, 0); | |
| 1991/0705 | nbytes += (vmemchr(a, 0, 0xFFFFFFFF) - a) + 1; | |
| 1990/1226 | nargs++; | |
| 1990/0227 | } ssize = BY2WD*(nargs+1) + ((nbytes+(BY2WD-1)) & ~(BY2WD-1)); spage = (ssize+(BY2PG-1)) >> PGSHIFT; /* * Build the stack segment, putting it in kernel virtual for the moment */ | |
| 1991/0523 | if(spage > TSTKSIZ) | |
| 1991/0522 |
| |
| 1991/0709 | errors("too many arguments"); | |
| 1991/0522 | ||
| 1991/0705 | p->seg[ESEG] = newseg(SG_STACK, TSTKTOP-USTKSIZE, USTKSIZE/BY2PG); | |
| 1990/0227 | /* * Args: pass 2: assemble; the pages will be faulted in */ argv = (char**)(TSTKTOP - ssize); charp = (char*)(TSTKTOP - nbytes); if(indir) argp = progarg; else argp = (char**)arg[1]; | |
| 1991/0705 | ||
| 1990/0227 | for(i=0; i<nargs; i++){ | |
| 1991/0705 | if(indir && *argp==0) { | |
| 1990/0227 | indir = 0; argp = (char**)arg[1]; } *argv++ = charp + (USTKTOP-TSTKTOP); n = strlen(*argp) + 1; | |
| 1991/0318 | memmove(charp, *argp++, n); | |
| 1990/0227 | charp += n; } | |
| 1991/0318 | memmove(p->text, elem, NAMELEN); | |
| 1990/0227 | ||
| 1991/0705 | if(waserror()) pexit("fatal exec error", 0); | |
| 1990/0227 | /* | |
| 1991/0705 | * Committed. Free old memory. Special segments are maintained accross exec | |
| 1990/0227 | */ | |
| 1991/0705 |
| |
| 1991/0709 | for(i = SSEG; i <= BSEG; i++) { putseg(p->seg[i]); p->seg[i] = 0; /* prevent a second free if we have an error */ } | |
| 1990/08141 | /* * Close on exec */ | |
| 1991/0705 | f = u->p->fgrp; for(i=0; i<=f->maxfd; i++) fdclose(i, CCEXEC); | |
| 1990/0227 | ||
| 1991/0705 | /* Text. Shared. Attaches to cache image if possible */ img = attachimage(SG_TEXT|SG_RONLY, tc, UTZERO, (t-UTZERO)>>PGSHIFT); ts = img->s; p->seg[TSEG] = ts; | |
| 1991/0706 | ts->flushme = 1; | |
| 1991/0705 | ts->fstart = 0; ts->flen = sizeof(Exec)+exec.text; | |
| 1990/0227 | ||
| 1991/0705 | /* Data. Shared. */ s = newseg(SG_DATA, t, (d-t)>>PGSHIFT); p->seg[DSEG] = s; | |
| 1990/0227 | ||
| 1991/0705 | /* Attached by hand */ incref(img); s->image = img; s->fstart = ts->fstart+ts->flen; s->flen = exec.data; | |
| 1990/0227 | ||
| 1991/0705 | /* BSS. Zero fill on demand */ p->seg[BSEG] = newseg(SG_BSS, d, (b-d)>>PGSHIFT); | |
| 1990/1211 | ||
| 1990/0227 | /* * Move the stack */ | |
| 1991/0705 | s = p->seg[ESEG]; | |
| 1991/0707 | p->seg[ESEG] = 0; | |
| 1991/0705 | p->seg[SSEG] = s; s->base = USTKTOP-USTKSIZE; s->top = USTKTOP; relocateseg(s, TSTKTOP-USTKTOP); | |
| 1990/0227 | ||
| 1991/0706 | close(tc); | |
| 1991/0705 | poperror(); | |
| 1991/0529 | /* | |
| 1991/0705 | * At this point, the mmu contains info about the old address | |
| 1991/0529 | * space and needs to be flushed */ | |
| 1990/0227 | flushmmu(); | |
| 1990/1212 | clearmmucache(); | |
| 1990/1226 | execpc(exec.entry); | |
| 1990/0227 | sp = (ulong*)(USTKTOP - ssize); *--sp = nargs; | |
| 1990/0614 | ((Ureg*)UREGADDR)->usp = (ulong)sp; | |
| 1990/0227 | lock(&p->debug); u->nnote = 0; u->notify = 0; u->notified = 0; | |
| 1990/1212 | procsetup(p); | |
| 1990/0227 | unlock(&p->debug); return 0; } int shargs(char *s, int n, char **ap) { int i; s += 2, n -= 2; /* skip #! */ for(i=0; s[i]!='\n'; i++) if(i == n-1) return 0; s[i] = 0; *ap = 0; i = 0; for(;;){ while(*s==' ' || *s=='\t') s++; if(*s == 0) break; i++; *ap++ = s; *ap = 0; while(*s && *s!=' ' && *s!='\t') s++; if(*s == 0) break; else *s++ = 0; } return i; } int return0(void *a) { return 0; } long syssleep(ulong *arg) { tsleep(&u->p->sleep, return0, 0, arg[0]); return 0; } | |
| 1991/0513 | long sysalarm(ulong *arg) { return procalarm(arg[0]); } | |
| 1990/0227 | long sysexits(ulong *arg) { char *status; status = (char*)arg[0]; if(status){ if(waserror()) status = "invalid exit string"; else{ validaddr((ulong)status, 1, 0); vmemchr(status, 0, ERRLEN); } | |
| 1991/0614 | poperror(); | |
| 1991/0705 | ||
| 1990/0227 | } pexit(status, 1); } long syswait(ulong *arg) { if(arg[0]){ validaddr(arg[0], sizeof(Waitmsg), 1); evenaddr(arg[0]); } return pwait((Waitmsg*)arg[0]); } long | |
| 1990/11211 | sysdeath(ulong *arg) | |
| 1990/0227 | { | |
| 1990/11211 | pprint("deprecated system call"); pexit("Suicide", 0); | |
| 1990/0227 | } long syserrstr(ulong *arg) { char buf[ERRLEN]; | |
| 1990/11211 | validaddr(arg[0], ERRLEN, 1); | |
| 1991/0318 | memmove((char*)arg[0], u->error, ERRLEN); | |
| 1990/0227 | return 0; } long sysforkpgrp(ulong *arg) { | |
| 1991/0514 | int mask; | |
| 1990/0227 | Pgrp *pg; | |
| 1991/0705 | Egrp *eg; | |
| 1990/0227 | pg = newpgrp(); | |
| 1991/0705 | eg = newegrp(); | |
| 1990/0227 | if(waserror()){ closepgrp(pg); | |
| 1991/0705 | closeegrp(eg); | |
| 1990/0227 | nexterror(); } | |
| 1991/0514 | mask = arg[0]; if(mask == FPall) mask = FPnote|FPenv|FPnamespc; memmove(pg->user, u->p->pgrp->user, NAMELEN); if(mask & FPnamespc) | |
| 1990/0227 | pgrpcpy(pg, u->p->pgrp); | |
| 1991/0514 | if(mask & FPenv) | |
| 1991/0705 | envcpy(eg, u->p->egrp); | |
| 1991/0514 | if((mask & FPnote) == 0) { u->nnote = 0; u->notified = 0; memset(u->note, 0, sizeof(u->note)); } | |
| 1991/0614 | poperror(); | |
| 1990/0227 | closepgrp(u->p->pgrp); | |
| 1991/0705 | closeegrp(u->p->egrp); | |
| 1990/0227 | u->p->pgrp = pg; | |
| 1991/0705 | u->p->egrp = eg; | |
| 1990/0227 | return pg->pgrpid; } long sysnotify(ulong *arg) { | |
| 1991/0326 | if(arg[0] != 0) validaddr(arg[0], sizeof(ulong), 0); | |
| 1990/0227 | u->notify = (int(*)(void*, char*))(arg[0]); return 0; } long sysnoted(ulong *arg) { if(u->notified == 0) | |
| 1990/11211 | error(Egreg); | |
| 1990/0227 | return 0; } long | |
| 1991/0705 | syssegbrk(ulong *arg) | |
| 1990/0227 | { | |
| 1991/0705 | Segment *s; int i; for(i = 0; i < NSEG; i++) if(s = u->p->seg[i]) { if(arg[0] >= s->base && arg[0] < s->top) { switch(s->type&SG_TYPE) { case SG_TEXT: case SG_DATA: errors("bad segment type"); default: return ibrk(arg[1], i); } } } errors("not in address space"); | |
| 1990/0227 | } | |
| 1991/0606 | ||
| 1991/0605 | long | |
| 1991/0705 | syssegattach(ulong *arg) | |
| 1991/0605 | { | |
| 1991/0705 | return segattach(u->p, arg[0], (char*)arg[1], arg[2], arg[3]); } long syssegdetach(ulong *arg) { int i; Segment *s; for(i = 0; i < NSEG; i++) if(s = u->p->seg[i]) { qlock(&s->lk); if((arg[0] >= s->base && arg[0] < s->top) || (s->top == s->base && arg[0] == s->base)) goto found; qunlock(&s->lk); } errors("not in address space"); found: if((ulong)arg >= s->base && (ulong)arg < s->top) { qunlock(&s->lk); errors("illegal address"); } u->p->seg[i] = 0; qunlock(&s->lk); putseg(s); /* Ensure we flush any entries from the lost segment */ flushmmu(); return 0; } long syssegfree(ulong *arg) { Segment *s; ulong from, len; | |
| 1991/0706 | from = PGROUND(arg[0]); s = seg(u->p, from, 1); | |
| 1991/0705 | if(s == 0) errors("not in address space"); | |
| 1991/0706 | len = arg[1]; | |
| 1991/0705 | if(from+len > s->top) { qunlock(&s->lk); errors("segment too short"); } mfreeseg(s, from, len); qunlock(&s->lk); return 0; } /* For binary compatability */ long sysbrk_(ulong *arg) { return ibrk(arg[0], BSEG); | |
| 1991/0605 | } | |