void runtime·newosproc(M *m, G *g, void *stk, void (*fn)(void)) { void *thandle; USED(stk); USED(g); // assuming g = m->g0 USED(fn); // assuming fn = mstart thandle = runtime·stdcall(runtime·CreateThread, 6, nil, (uintptr)0x20000, runtime·tstart_stdcall, m, STACK_SIZE_PARAM_IS_A_RESERVATION, nil); if(thandle == nil) { runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount(), runtime·getlasterror()); runtime·throw("runtime.newosproc"); } runtime·atomicstorep(&m->thread, thandle); }
void runtime·newosproc(M *mp, void *stk) { PthreadAttr attr; Sigset oset; Pthread tid; int32 ret; uint64 size; USED(stk); if(runtime·pthread_attr_init(&attr) != 0) runtime·throw("pthread_attr_init"); if(runtime·pthread_attr_setstack(&attr, 0, 0x200000) != 0) runtime·throw("pthread_attr_setstack"); size = 0; if(runtime·pthread_attr_getstack(&attr, (void**)&mp->g0->stack.hi, &size) != 0) runtime·throw("pthread_attr_getstack"); mp->g0->stack.lo = mp->g0->stack.hi - size; if(runtime·pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) runtime·throw("pthread_attr_setdetachstate"); // Disable signals during create, so that the new thread starts // with signals disabled. It will enable them in minit. runtime·sigprocmask(SIG_SETMASK, &sigset_all, &oset); ret = runtime·pthread_create(&tid, &attr, (void (*)(void))runtime·tstart_sysvicall, mp); runtime·sigprocmask(SIG_SETMASK, &oset, nil); if(ret != 0) { runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount(), ret); runtime·throw("runtime.newosproc"); } }
void runtime·newosproc(M *mp, void *stk) { void *thandle; USED(stk); thandle = runtime·stdcall(runtime·CreateThread, 6, nil, (uintptr)0x20000, runtime·tstart_stdcall, mp, STACK_SIZE_PARAM_IS_A_RESERVATION, nil); if(thandle == nil) { runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount(), runtime·getlasterror()); runtime·throw("runtime.newosproc"); } }
void runtime·newosproc(M *mp, G *gp, void *stk, void (*fn)(void)) { UcontextT uc; int32 ret; if(0) { runtime·printf( "newosproc stk=%p m=%p g=%p fn=%p id=%d/%d ostk=%p\n", stk, mp, gp, fn, mp->id, mp->tls[0], &mp); } mp->tls[0] = mp->id; // so 386 asm can find it runtime·getcontext(&uc); uc.uc_flags = _UC_SIGMASK | _UC_CPU; uc.uc_link = nil; uc.uc_sigmask = sigset_all; runtime·lwp_mcontext_init(&uc.uc_mcontext, stk, mp, gp, fn); ret = runtime·lwp_create(&uc, 0, &mp->procid); if(ret < 0) { runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount() - 1, -ret); runtime·throw("runtime.newosproc"); } }
void runtime·newosproc(M *m, G *g, void *stk, void (*fn)(void)) { int32 ret; int32 flags; /* * note: strace gets confused if we use CLONE_PTRACE here. */ flags = CLONE_VM /* share memory */ | CLONE_FS /* share cwd, etc */ | CLONE_FILES /* share fd table */ | CLONE_SIGHAND /* share sig handler table */ | CLONE_THREAD /* revisit - okay for now */ ; m->tls[0] = m->id; // so 386 asm can find it if(0){ runtime·printf("newosproc stk=%p m=%p g=%p fn=%p clone=%p id=%d/%d ostk=%p\n", stk, m, g, fn, runtime·clone, m->id, m->tls[0], &m); } if((ret = runtime·clone(flags, stk, m, g, fn)) < 0) { runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount(), -ret); runtime·throw("runtime.newosproc"); } }
void runtime·newosproc(M *m, G *g, void *stk, void (*fn)(void)) { int32 flags; int32 ret; flags = RFPROC | RFTHREAD | RFMEM | RFNOWAIT; if (0) { runtime·printf( "newosproc stk=%p m=%p g=%p fn=%p id=%d/%d ostk=%p\n", stk, m, g, fn, m->id, m->tls[0], &m); } m->tls[0] = m->id; // so 386 asm can find it if((ret = runtime·rfork_thread(flags, stk, m, g, fn)) < 0) { runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount() - 1, -ret); if (ret == -ENOTSUP) runtime·printf("runtime: is kern.rthreads disabled?\n"); runtime·throw("runtime.newosproc"); } }
void runtime·newosproc(M *mp, G *gp, void *stk, void (*fn)(void)) { int32 errno; Sigset oset; mp->tls[0] = mp->id; // so 386 asm can find it if(0){ runtime·printf("newosproc stk=%p m=%p g=%p fn=%p id=%d/%d ostk=%p\n", stk, mp, gp, fn, mp->id, mp->tls[0], &mp); } runtime·sigprocmask(SIG_SETMASK, &sigset_all, &oset); errno = runtime·bsdthread_create(stk, mp, gp, fn); runtime·sigprocmask(SIG_SETMASK, &oset, nil); if(errno < 0) { runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount(), -errno); runtime·throw("runtime.newosproc"); } }
void runtime·newosproc(M *m, G *g, void *stk, void (*fn)(void)) { void *thandle; USED(stk); USED(g); // assuming g = m->g0 USED(fn); // assuming fn = mstart thandle = runtime·stdcall(runtime·CreateThread, 6, (uintptr)0, (uintptr)0, runtime·tstart_stdcall, m, (uintptr)0, (uintptr)0); if(thandle == 0) { runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount(), runtime·getlasterror()); runtime·throw("runtime.newosproc"); } }
void runtime·newosproc(M *mp, void *stk) { #ifdef NOPEJUSTNOPE int32 ret; int32 flags; Sigset oset; /* * note: strace gets confused if we use CLONE_PTRACE here. */ flags = CLONE_VM /* share memory */ | CLONE_FS /* share cwd, etc */ | CLONE_FILES /* share fd table */ | CLONE_SIGHAND /* share sig handler table */ | CLONE_THREAD /* revisit - okay for now */ ; mp->tls[0] = mp->id; // so 386 asm can find it if(0){ runtime·printf("newosproc stk=%p m=%p g=%p clone=%p id=%d/%d ostk=%p\n", stk, mp, mp->g0, runtime·clone, mp->id, (int32)mp->tls[0], &mp); } // Disable signals during clone, so that the new thread starts // with signals disabled. It will enable them in minit. runtime·rtsigprocmask(SIG_SETMASK, &sigset_all, &oset, sizeof oset); ret = runtime·clone(flags, stk, mp, mp->g0, runtime·mstart); runtime·rtsigprocmask(SIG_SETMASK, &oset, nil, sizeof oset); if(ret < 0) { runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount(), -ret); runtime·throw("runtime.newosproc"); } #endif }
void runtime·newosproc(M *mp, void *stk) { Tfork param; Sigset oset; int32 ret; if(0) { runtime·printf( "newosproc stk=%p m=%p g=%p id=%d/%d ostk=%p\n", stk, mp, mp->g0, mp->id, (int32)mp->tls[0], &mp); } mp->tls[0] = mp->id; // so 386 asm can find it param.tf_tcb = (byte*)&mp->tls[0]; param.tf_tid = (int32*)&mp->procid; param.tf_stack = stk; oset = runtime·sigprocmask(SIG_SETMASK, sigset_all); ret = runtime·tfork((byte*)¶m, sizeof(param), mp, mp->g0, runtime·mstart); runtime·sigprocmask(SIG_SETMASK, oset); if(ret < 0) { runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount() - 1, -ret); if (ret == -ENOTSUP) runtime·printf("runtime: is kern.rthreads disabled?\n"); runtime·throw("runtime.newosproc"); } }
int main(int argc, char *argv[]) { /* Command-line interface for CRC RevEng. * Process options and switches in the argument list and * run the required function. */ /* default values */ model_t model = { PZERO, /* no CRC polynomial, user must specify */ PZERO, /* Init = 0 */ P_BE, /* RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h */ PZERO, /* XorOut = 0 */ PZERO, /* check value unused */ NULL /* no model name */ }; int ibperhx = 8, obperhx = 8; int rflags = 0, uflags = 0; /* search and UI flags */ unsigned long width = 0UL; int c, mode = 0, args, psets, pass; poly_t apoly, crc, qpoly = PZERO, *apolys, *pptr = NULL, *qptr = NULL; model_t pset = model, *candmods, *mptr; char *string; myname = argv[0]; /* stdin must be binary */ #ifdef _WIN32 _setmode(STDIN_FILENO, _O_BINARY); #endif /* _WIN32 */ SETBMP(); do { c=getopt(argc, argv, "?A:BDFLMP:SVXa:bcdefhi:k:lm:p:q:rstuvw:x:y"); switch(c) { case 'A': /* A: bits per output character */ case 'a': /* a: bits per character */ if((obperhx = atoi(optarg)) > BMP_BIT) { fprintf(stderr,"%s: argument to -%c must be between 1 and %d\n", myname, c, BMP_BIT); exit(EXIT_FAILURE); } if(c == 'a') ibperhx = obperhx; break; case 'b': /* b big-endian (RefIn = false, RefOut = false ) */ model.flags &= ~P_REFIN; rflags |= R_HAVERI; /* fall through: */ case 'B': /* B big-endian output (RefOut = false) */ model.flags &= ~P_REFOUT; rflags |= R_HAVERO; mnovel(&model); /* fall through: */ case 'r': /* r right-justified */ model.flags |= P_RTJUST; break; case 'c': /* c calculate CRC */ case 'D': /* D list primary model names */ case 'd': /* d dump CRC model */ case 'e': /* e echo arguments */ case 's': /* s search for algorithm */ case 'v': /* v calculate reversed CRC */ if(mode) { fprintf(stderr,"%s: more than one mode switch specified. Use %s -h for help.\n", myname, myname); exit(EXIT_FAILURE); } mode = c; break; case 'F': /* F force search */ #ifndef NOFORCE uflags |= C_FORCE; #endif break; case 'f': /* f arguments are filenames */ uflags |= C_INFILE; break; case 'h': /* h get help / usage */ case 'u': /* u get help / usage */ case '?': /* ? get help / usage */ default: usage(); exit(EXIT_FAILURE); break; case 'i': /* i: Init value */ pptr = &model.init; rflags |= R_HAVEI; goto ippx; case 'k': /* k: polynomial in Koopman notation */ pfree(&model.spoly); model.spoly = strtop(optarg, 0, 4); pkchop(&model.spoly); width = plen(model.spoly); rflags |= R_HAVEP; mnovel(&model); break; case 'l': /* l little-endian input and output */ model.flags |= P_REFIN; rflags |= R_HAVERI; /* fall through: */ case 'L': /* L little-endian output */ model.flags |= P_REFOUT; rflags |= R_HAVERO; mnovel(&model); /* fall through: */ case 't': /* t left-justified */ model.flags &= ~P_RTJUST; break; case 'm': /* m: select preset CRC model */ if(!(c = mbynam(&model, optarg))) { fprintf(stderr,"%s: preset model '%s' not found. Use %s -D to list presets.\n", myname, optarg, myname); exit(EXIT_FAILURE); } if(c < 0) uerror("no preset models available"); /* must set width so that parameter to -ipx is not zeroed */ width = plen(model.spoly); rflags |= R_HAVEP | R_HAVEI | R_HAVERI | R_HAVERO | R_HAVEX; break; case 'M': /* M non-augmenting algorithm */ model.flags &= ~P_MULXN; break; case 'P': /* P: reversed polynomial */ case 'p': /* p: polynomial */ pptr = &model.spoly; rflags &= ~R_HAVEQ; rflags |= R_HAVEP; ippx: pfree(pptr); *pptr = strtop(optarg, 0, 4); pright(pptr, width); if(c == 'P') prev(pptr); mnovel(&model); break; case 'q': /* q: range end polynomial */ pptr = &qpoly; rflags &= ~R_HAVEP; rflags |= R_HAVEQ; goto ippx; case 'S': /* s space between output characters */ model.flags |= P_SPACE; break; case 'V': /* v reverse algorithm */ /* Distinct from the -v switch as the * user will have to reverse his or her * own arguments. The user cannot dump * the model generated by -v either. */ mrev(&model); break; case 'w': /* w: CRC width = order - 1 */ width = (unsigned long) atol(optarg); break; case 'X': /* X print uppercase hex */ model.flags |= P_UPPER; break; case 'x': /* x: XorOut value */ pptr = &model.xorout; rflags |= R_HAVEX; goto ippx; case 'y': /* y little-endian byte order in files */ model.flags |= P_LTLBYT; break; case -1: /* no more options, continue */ ; } } while(c != -1); /* canonicalise the model, so the one we dump is the one we * calculate with (not with -s, spoly may be blank which will * normalise to zero and clear init and xorout.) */ if(mode != 's') mcanon(&model); switch(mode) { case 'v': /* v calculate reversed CRC */ /* Distinct from the -V switch as this causes * the arguments and output to be reversed as well. */ /* reciprocate Poly */ prcp(&model.spoly); /* mrev() does: * if(refout) prev(init); else prev(xorout); * but here the entire argument polynomial is * reflected, not just the characters, so RefIn * and RefOut are not inverted as with -V. * Consequently Init is the mirror image of the * one resulting from -V, and so we have: */ if(~model.flags & P_REFOUT) { prev(&model.init); prev(&model.xorout); } /* swap init and xorout */ apoly = model.init; model.init = model.xorout; model.xorout = apoly; /* fall through: */ case 'c': /* c calculate CRC */ /* validate inputs */ /* if(plen(model.spoly) == 0) { * fprintf(stderr,"%s: no polynomial specified for -%c (add -w WIDTH -p POLY)\n", myname, mode); * exit(EXIT_FAILURE); * } */ /* in the Williams model, xorout is applied after the refout stage. * as refout is part of ptostr(), we reverse xorout here. */ if(model.flags & P_REFOUT) prev(&model.xorout); for(; optind < argc; ++optind) { if(uflags & C_INFILE) apoly = rdpoly(argv[optind], model.flags, ibperhx); else apoly = strtop(argv[optind], model.flags, ibperhx); if(mode == 'v') prev(&apoly); crc = pcrc(apoly, model.spoly, model.init, model.xorout, model.flags); if(mode == 'v') prev(&crc); string = ptostr(crc, model.flags, obperhx); puts(string); free(string); pfree(&crc); pfree(&apoly); } break; case 'D': /* D dump all models */ args = mcount(); if(!args) uerror("no preset models available"); for(mode = 0; mode < args; ++mode) { mbynum(&model, mode); mcanon(&model); ufound(&model); } break; case 'd': /* d dump CRC model */ /* maybe we don't want to do this: * either attaching names to arbitrary models or forcing to a preset * mmatch(&model, M_OVERWR); */ if(~model.flags & P_MULXN) uerror("not a Williams model compliant algorithm"); string = mtostr(&model); puts(string); free(string); break; case 'e': /* e echo arguments */ for(; optind < argc; ++optind) { if(uflags & C_INFILE) apoly = rdpoly(argv[optind], model.flags, ibperhx); else apoly = strtop(argv[optind], model.flags, ibperhx); psum(&apoly, model.init, 0UL); string = ptostr(apoly, model.flags, obperhx); puts(string); free(string); pfree(&apoly); } break; case 's': /* s search for algorithm */ if(!width) uerror("must specify positive -k or -w before -s"); if(~model.flags & P_MULXN) uerror("cannot search for non-Williams compliant models"); praloc(&model.spoly, width); praloc(&model.init, width); praloc(&model.xorout, width); if(!plen(model.spoly)) palloc(&model.spoly, width); else width = plen(model.spoly); /* special case if qpoly is zero, search to end of range */ if(!ptst(qpoly)) rflags &= ~R_HAVEQ; /* allocate argument array */ args = argc - optind; if(!(apolys = malloc(args * sizeof(poly_t)))) uerror("cannot allocate memory for argument list"); for(pptr = apolys; optind < argc; ++optind) { if(uflags & C_INFILE) *pptr++ = rdpoly(argv[optind], model.flags, ibperhx); else *pptr++ = strtop(argv[optind], model.flags, ibperhx); } /* exit value of pptr is used hereafter! */ /* if endianness not specified, try * little-endian then big-endian. * NB: crossed-endian algorithms will not be * searched. */ /* scan against preset models */ if(~uflags & C_FORCE) { pass = 0; do { psets = mcount(); while(psets) { mbynum(&pset, --psets); /* skip if different width, or refin or refout don't match */ if(plen(pset.spoly) != width || (model.flags ^ pset.flags) & (P_REFIN | P_REFOUT)) continue; /* skip if the preset doesn't match specified parameters */ if(rflags & R_HAVEP && pcmp(&model.spoly, &pset.spoly)) continue; if(rflags & R_HAVEI && psncmp(&model.init, &pset.init)) continue; if(rflags & R_HAVEX && psncmp(&model.xorout, &pset.xorout)) continue; apoly = pclone(pset.xorout); if(pset.flags & P_REFOUT) prev(&apoly); for(qptr = apolys; qptr < pptr; ++qptr) { crc = pcrc(*qptr, pset.spoly, pset.init, apoly, 0); if(ptst(crc)) { pfree(&crc); break; } else pfree(&crc); } pfree(&apoly); if(qptr == pptr) { /* the selected model solved all arguments */ mcanon(&pset); ufound(&pset); uflags |= C_RESULT; } } mfree(&pset); /* toggle refIn/refOut and reflect arguments */ if(~rflags & R_HAVERI) { model.flags ^= P_REFIN | P_REFOUT; for(qptr = apolys; qptr < pptr; ++qptr) prevch(qptr, ibperhx); } } while(~rflags & R_HAVERI && ++pass < 2); } if(uflags & C_RESULT) { for(qptr = apolys; qptr < pptr; ++qptr) pfree(qptr); exit(EXIT_SUCCESS); } if(!(model.flags & P_REFIN) != !(model.flags & P_REFOUT)) uerror("cannot search for crossed-endian models"); pass = 0; do { mptr = candmods = reveng(&model, qpoly, rflags, args, apolys); if(mptr && plen(mptr->spoly)) uflags |= C_RESULT; while(mptr && plen(mptr->spoly)) { /* results were printed by the callback * string = mtostr(mptr); * puts(string); * free(string); */ mfree(mptr++); } free(candmods); if(~rflags & R_HAVERI) { model.flags ^= P_REFIN | P_REFOUT; for(qptr = apolys; qptr < pptr; ++qptr) prevch(qptr, ibperhx); } } while(~rflags & R_HAVERI && ++pass < 2); for(qptr = apolys; qptr < pptr; ++qptr) pfree(qptr); free(apolys); if(~uflags & C_RESULT) uerror("no models found"); break; default: /* no mode specified */ fprintf(stderr, "%s: no mode switch specified. Use %s -h for help.\n", myname, myname); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); }