sysproc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "tos.h"
  11. #include "../port/lib.h"
  12. #include "mem.h"
  13. #include "dat.h"
  14. #include "fns.h"
  15. #include "../port/error.h"
  16. #include "../port/edf.h"
  17. #include <trace.h>
  18. #undef DBG
  19. #define DBG if(0)print
  20. void
  21. sysrfork(Ar0* ar0, ...)
  22. {
  23. Proc *up = externup();
  24. Proc *p;
  25. int flag, i, n, pid;
  26. Fgrp *ofg;
  27. Pgrp *opg;
  28. Rgrp *org;
  29. Egrp *oeg;
  30. Mach *wm;
  31. va_list list;
  32. va_start(list, ar0);
  33. /*
  34. * int rfork(int);
  35. */
  36. flag = va_arg(list, int);
  37. va_end(list);
  38. /* Check flags before we commit */
  39. if((flag & (RFFDG|RFCFDG)) == (RFFDG|RFCFDG))
  40. error(Ebadarg);
  41. if((flag & (RFNAMEG|RFCNAMEG)) == (RFNAMEG|RFCNAMEG))
  42. error(Ebadarg);
  43. if((flag & (RFENVG|RFCENVG)) == (RFENVG|RFCENVG))
  44. error(Ebadarg);
  45. if((flag & (RFPREPAGE|RFCPREPAGE)) == (RFPREPAGE|RFCPREPAGE))
  46. error(Ebadarg);
  47. if((flag & (RFCORE|RFCCORE)) == (RFCORE|RFCCORE))
  48. error(Ebadarg);
  49. if(flag & RFCORE && up->wired != nil)
  50. error("wired proc cannot move to ac");
  51. if((flag&RFPROC) == 0) {
  52. if(flag & (RFMEM|RFNOWAIT))
  53. error(Ebadarg);
  54. if(flag & (RFFDG|RFCFDG)) {
  55. ofg = up->fgrp;
  56. if(flag & RFFDG)
  57. up->fgrp = dupfgrp(ofg);
  58. else
  59. up->fgrp = dupfgrp(nil);
  60. closefgrp(ofg);
  61. }
  62. if(flag & (RFNAMEG|RFCNAMEG)) {
  63. opg = up->pgrp;
  64. up->pgrp = newpgrp();
  65. if(flag & RFNAMEG)
  66. pgrpcpy(up->pgrp, opg);
  67. /* inherit noattach */
  68. up->pgrp->noattach = opg->noattach;
  69. closepgrp(opg);
  70. }
  71. if(flag & RFNOMNT)
  72. up->pgrp->noattach = 1;
  73. if(flag & RFREND) {
  74. org = up->rgrp;
  75. up->rgrp = newrgrp();
  76. closergrp(org);
  77. }
  78. if(flag & (RFENVG|RFCENVG)) {
  79. oeg = up->egrp;
  80. up->egrp = smalloc(sizeof(Egrp));
  81. up->egrp->r.ref = 1;
  82. if(flag & RFENVG)
  83. envcpy(up->egrp, oeg);
  84. closeegrp(oeg);
  85. }
  86. if(flag & RFNOTEG)
  87. up->noteid = incref(&noteidalloc);
  88. if(flag & (RFPREPAGE|RFCPREPAGE)){
  89. up->prepagemem = flag&RFPREPAGE;
  90. nixprepage(-1);
  91. }
  92. if(flag & RFCORE){
  93. up->ac = getac(up, -1);
  94. up->procctl = Proc_toac;
  95. }else if(flag & RFCCORE){
  96. if(up->ac != nil)
  97. up->procctl = Proc_totc;
  98. }
  99. ar0->i = 0;
  100. return;
  101. }
  102. p = newproc();
  103. if(flag & RFCORE){
  104. if(!waserror()){
  105. p->ac = getac(p, -1);
  106. p->procctl = Proc_toac;
  107. poperror();
  108. }else{
  109. print("warning: rfork: no available ac for the child, it runs in the tc\n");
  110. p->procctl = 0;
  111. }
  112. }
  113. if(up->trace)
  114. p->trace = 1;
  115. p->scallnr = up->scallnr;
  116. memmove(p->arg, up->arg, sizeof(up->arg));
  117. p->nerrlab = 0;
  118. p->slash = up->slash;
  119. p->dot = up->dot;
  120. incref(&p->dot->r);
  121. memmove(p->note, up->note, sizeof(p->note));
  122. p->privatemem = up->privatemem;
  123. p->noswap = up->noswap;
  124. p->nnote = up->nnote;
  125. p->notified = 0;
  126. p->lastnote = up->lastnote;
  127. p->notify = up->notify;
  128. p->ureg = up->ureg;
  129. p->prepagemem = up->prepagemem;
  130. p->dbgreg = 0;
  131. /* Make a new set of memory segments */
  132. n = flag & RFMEM;
  133. qlock(&p->seglock);
  134. if(waserror()){
  135. qunlock(&p->seglock);
  136. nexterror();
  137. }
  138. for(i = 0; i < NSEG; i++)
  139. if(up->seg[i])
  140. p->seg[i] = dupseg(up->seg, i, n);
  141. qunlock(&p->seglock);
  142. poperror();
  143. /* File descriptors */
  144. if(flag & (RFFDG|RFCFDG)) {
  145. if(flag & RFFDG)
  146. p->fgrp = dupfgrp(up->fgrp);
  147. else
  148. p->fgrp = dupfgrp(nil);
  149. }
  150. else {
  151. p->fgrp = up->fgrp;
  152. incref(&p->fgrp->r);
  153. }
  154. /* Process groups */
  155. if(flag & (RFNAMEG|RFCNAMEG)) {
  156. p->pgrp = newpgrp();
  157. if(flag & RFNAMEG)
  158. pgrpcpy(p->pgrp, up->pgrp);
  159. /* inherit noattach */
  160. p->pgrp->noattach = up->pgrp->noattach;
  161. }
  162. else {
  163. p->pgrp = up->pgrp;
  164. incref(&p->pgrp->r);
  165. }
  166. if(flag & RFNOMNT)
  167. up->pgrp->noattach = 1;
  168. if(flag & RFREND)
  169. p->rgrp = newrgrp();
  170. else {
  171. incref(&up->rgrp->r);
  172. p->rgrp = up->rgrp;
  173. }
  174. /* Environment group */
  175. if(flag & (RFENVG|RFCENVG)) {
  176. p->egrp = smalloc(sizeof(Egrp));
  177. p->egrp->r.ref = 1;
  178. if(flag & RFENVG)
  179. envcpy(p->egrp, up->egrp);
  180. }
  181. else {
  182. p->egrp = up->egrp;
  183. incref(&p->egrp->r);
  184. }
  185. p->hang = up->hang;
  186. p->procmode = up->procmode;
  187. /* Craft a return frame which will cause the child to pop out of
  188. * the scheduler in user mode with the return register zero
  189. */
  190. sysrforkchild(p, up);
  191. p->parent = up;
  192. p->parentpid = up->pid;
  193. if(flag&RFNOWAIT)
  194. p->parentpid = 0;
  195. else {
  196. lock(&up->exl);
  197. up->nchild++;
  198. unlock(&up->exl);
  199. }
  200. if((flag&RFNOTEG) == 0)
  201. p->noteid = up->noteid;
  202. pid = p->pid;
  203. memset(p->time, 0, sizeof(p->time));
  204. p->time[TReal] = sys->ticks;
  205. if(flag & (RFPREPAGE|RFCPREPAGE)){
  206. p->prepagemem = flag&RFPREPAGE;
  207. /*
  208. * BUG: this is prepaging our memory, not
  209. * that of the child, but at least we
  210. * will do the copy on write.
  211. */
  212. nixprepage(-1);
  213. }
  214. kstrdup(&p->text, up->text);
  215. kstrdup(&p->user, up->user);
  216. /*
  217. * since the bss/data segments are now shareable,
  218. * any mmu info about this process is now stale
  219. * (i.e. has bad properties) and has to be discarded.
  220. */
  221. mmuflush();
  222. p->basepri = up->basepri;
  223. p->priority = up->basepri;
  224. p->fixedpri = up->fixedpri;
  225. p->mp = up->mp;
  226. wm = up->wired;
  227. if(wm)
  228. procwired(p, wm->machno);
  229. p->color = up->color;
  230. ready(p);
  231. sched();
  232. ar0->i = pid;
  233. }
  234. #if 0
  235. static uint64_t
  236. vl2be(uint64_t v)
  237. {
  238. uint8_t *p;
  239. p = (uint8_t*)&v;
  240. return ((uint64_t)((p[0]<<24)|(p[1]<<16)|(p[2]<<8)|p[3])<<32)
  241. |((uint64_t)(p[4]<<24)|(p[5]<<16)|(p[6]<<8)|p[7]);
  242. }
  243. static uint32_t
  244. l2be(int32_t l)
  245. {
  246. uint8_t *cp;
  247. cp = (uint8_t*)&l;
  248. return (cp[0]<<24) | (cp[1]<<16) | (cp[2]<<8) | cp[3];
  249. }
  250. #endif
  251. /*
  252. * flags can ONLY specify that you want an AC for you, or
  253. * that you want an XC for you.
  254. */
  255. static void
  256. execac(Ar0* ar0, int flags, char *ufile, char **argv)
  257. {
  258. Proc *up = externup();
  259. Fgrp *fg;
  260. Tos *tos;
  261. Chan *chan, *ichan;
  262. Image *img;
  263. Segment *s;
  264. Ldseg *ldseg;
  265. int argc, i, n, nldseg;
  266. char *a, *elem, *file, *p;
  267. char line[64], *progarg[sizeof(line)/2+1];
  268. int32_t hdrsz;
  269. uintptr_t entry, stack;
  270. file = nil;
  271. elem = nil;
  272. switch(flags){
  273. case EXTC:
  274. case EXXC:
  275. break;
  276. case EXAC:
  277. up->ac = getac(up, -1);
  278. break;
  279. default:
  280. error("unknown execac flag");
  281. }
  282. if(waserror()){
  283. DBG("execac: failing: %s\n", up->errstr);
  284. free(file);
  285. free(elem);
  286. if(flags == EXAC && up->ac != nil)
  287. up->ac->proc = nil;
  288. up->ac = nil;
  289. nexterror();
  290. }
  291. /*
  292. * Open the file, remembering the final element and the full name.
  293. */
  294. argc = 0;
  295. file = validnamedup(ufile, 1);
  296. DBG("execac: up %#p file %s\n", up, file);
  297. if(up->trace)
  298. proctracepid(up);
  299. ichan = namec(file, Aopen, OEXEC, 0);
  300. if(waserror()){
  301. iprint("ERROR ON OPEN\n");
  302. cclose(ichan);
  303. nexterror();
  304. }
  305. kstrdup(&elem, up->genbuf);
  306. /*
  307. * Read the header.
  308. * If it's a #!, fill in progarg[] with info then read a new header
  309. * from the file indicated by the #!.
  310. * The #! line must be less than sizeof(Exec) in size,
  311. * including the terminating \n.
  312. */
  313. hdrsz = ichan->dev->read(ichan, line, sizeof line, 0);
  314. if(hdrsz < 2)
  315. error(Ebadexec);
  316. if(line[0] == '#' && line[1] == '!'){
  317. p = memchr(line, '\n', MIN(sizeof line, hdrsz));
  318. if(p == nil)
  319. error(Ebadexec);
  320. *p = '\0';
  321. argc = tokenize(line+2, progarg, nelem(progarg));
  322. if(argc == 0)
  323. error(Ebadexec);
  324. /* The original file becomes an extra arg after #! line */
  325. progarg[argc++] = file;
  326. /*
  327. * Take the #! $0 as a file to open, and replace
  328. * $0 with the original path's name.
  329. */
  330. p = progarg[0];
  331. progarg[0] = elem;
  332. chan = nil; /* in case namec errors out */
  333. USED(chan);
  334. chan = namec(p, Aopen, OEXEC, 0);
  335. }else{
  336. chan = ichan;
  337. incref(&ichan->r);
  338. }
  339. /* chan is the chan to use, initial or not. ichan is irrelevant now */
  340. cclose(ichan);
  341. poperror();
  342. /*
  343. * #! has had its chance, now we need a real binary.
  344. */
  345. nldseg = elf64ldseg(chan, &entry, &ldseg, cputype, BIGPGSZ);
  346. if(nldseg == 0){
  347. print("execac: elf64ldseg returned 0 segs!\n");
  348. error(Ebadexec);
  349. }
  350. /* TODO(aki): not sure I see the point
  351. if(up->ac != nil && up->ac != machp())
  352. up->color = corecolor(up->ac->machno);
  353. else
  354. up->color = corecolor(machp()->machno);
  355. */
  356. /*
  357. * The new stack is temporarily mapped elsewhere.
  358. * The stack contains, in descending address order:
  359. * a structure containing housekeeping and profiling data (Tos);
  360. * argument strings;
  361. * array of vectors to the argument strings with a terminating
  362. * nil (argv).
  363. * When the exec is committed, this temporary stack is relocated
  364. * to become the actual stack segment.
  365. * The architecture-dependent code which jumps to the new image
  366. * will also push a count of the argument array onto the stack (argc).
  367. */
  368. qlock(&up->seglock);
  369. int sno = -1;
  370. if(waserror()){
  371. if(sno != -1 && up->seg[sno] != nil){
  372. putseg(up->seg[sno]);
  373. up->seg[sno] = nil;
  374. }
  375. qunlock(&up->seglock);
  376. nexterror();
  377. }
  378. for(i = 0; i < NSEG; i++)
  379. if(up->seg[i] == nil)
  380. break;
  381. if(i == NSEG)
  382. error("exeac: no free segment slots");
  383. sno = i;
  384. up->seg[sno] = newseg(SG_STACK|SG_READ|SG_WRITE, TSTKTOP-USTKSIZE, USTKSIZE/BIGPGSZ);
  385. up->seg[sno]->color = up->color;
  386. /*
  387. * Stack is a pointer into the temporary stack
  388. * segment, and will move as items are pushed.
  389. */
  390. stack = TSTKTOP-sizeof(Tos);
  391. /*
  392. * First, the top-of-stack structure.
  393. */
  394. tos = (Tos*)stack;
  395. tos->cyclefreq = sys->cyclefreq;
  396. cycles((uint64_t*)&tos->pcycles);
  397. tos->pcycles = -tos->pcycles;
  398. tos->kcycles = tos->pcycles;
  399. tos->clock = 0;
  400. /*
  401. * Next push any arguments found from a #! header.
  402. */
  403. for(i = 0; i < argc; i++){
  404. n = strlen(progarg[i])+1;
  405. stack -= n;
  406. memmove(UINT2PTR(stack), progarg[i], n);
  407. }
  408. /*
  409. * Copy the strings pointed to by the syscall argument argv into
  410. * the temporary stack segment, being careful to check
  411. * the strings argv points to are valid.
  412. */
  413. for(i = 0;; i++, argv++){
  414. a = *(char**)validaddr(argv, sizeof(char**), 0);
  415. if(a == nil)
  416. break;
  417. a = validaddr(a, 1, 0);
  418. n = ((char*)vmemchr(a, 0, 0x7fffffff) - a) + 1;
  419. /*
  420. * This futzing is so argv[0] gets validated even
  421. * though it will be thrown away if this is a shell
  422. * script.
  423. */
  424. if(argc > 0 && i == 0)
  425. continue;
  426. /*
  427. * Before copying the string into the temporary stack,
  428. * which might involve a demand-page, check the string
  429. * will not overflow the bottom of the stack.
  430. */
  431. stack -= n;
  432. if(stack < TSTKTOP-USTKSIZE)
  433. error(Enovmem);
  434. p = UINT2PTR(stack);
  435. memmove(p, a, n);
  436. p[n-1] = 0;
  437. argc++;
  438. }
  439. if(argc < 1)
  440. error(Ebadexec);
  441. /*
  442. * Before pushing the argument pointers onto the temporary stack,
  443. * which might involve a demand-page, check there is room for the
  444. * terminating nil pointer, plus pointers, plus some slop for however
  445. * argc might be passed on the stack by sysexecregs (give a page
  446. * of slop, it is an overestimate, but why not).
  447. * Sysexecstack does any architecture-dependent stack alignment.
  448. * Keep a copy of the start of the argument strings before alignment
  449. * so up->args can be created later.
  450. * Although the argument vectors are being pushed onto the stack in
  451. * the temporary segment, the values must be adjusted to reflect
  452. * the segment address after it replaces the current SSEG.
  453. */
  454. a = p = UINT2PTR(stack);
  455. stack = sysexecstack(stack, argc);
  456. if(stack-(argc+2)*sizeof(char**)-BIGPGSZ < TSTKTOP-USTKSIZE) {
  457. //iprint("stck too small?\n");
  458. error(Ebadexec);
  459. }
  460. argv = (char**)stack;
  461. *--argv = nil;
  462. for(i = 0; i < argc; i++){
  463. *--argv = p + (USTKTOP-TSTKTOP);
  464. p += strlen(p) + 1;
  465. }
  466. *--argv = (void *)(uintptr_t) argc;
  467. /*
  468. * Make a good faith copy of the args in up->args using the strings
  469. * in the temporary stack segment. The length must be > 0 as it
  470. * includes the \0 on the last argument and argc was checked earlier
  471. * to be > 0. After the memmove, compensate for any UTF character
  472. * boundary before placing the terminating \0.
  473. */
  474. n = p - a;
  475. if(n <= 0)
  476. error(Egreg);
  477. if(n > 128)
  478. n = 128;
  479. p = smalloc(n);
  480. if(waserror()){
  481. free(p);
  482. nexterror();
  483. }
  484. memmove(p, a, n);
  485. while(n > 0 && (p[n-1] & 0xc0) == 0x80)
  486. n--;
  487. p[n-1] = '\0';
  488. /*
  489. * All the argument processing is now done, ready to commit.
  490. */
  491. free(up->text);
  492. up->text = elem;
  493. elem = nil;
  494. free(up->args);
  495. up->args = p;
  496. up->nargs = n;
  497. poperror(); /* p (up->args) */
  498. /*
  499. * Close on exec
  500. */
  501. fg = up->fgrp;
  502. for(i=0; i<=fg->maxfd; i++)
  503. fdclose(i, CCEXEC);
  504. /*
  505. * Free old memory, except for the temp stack (obviously)
  506. */
  507. s = up->seg[sno];
  508. for(i = 0; i < NSEG; i++) {
  509. if(up->seg[i] != s)
  510. putseg(up->seg[i]);
  511. up->seg[i] = nil;
  512. }
  513. /* put the stack in first */
  514. sno = 0;
  515. up->seg[sno++] = s;
  516. s->base = USTKTOP-USTKSIZE;
  517. s->top = USTKTOP;
  518. relocateseg(s, USTKTOP-TSTKTOP);
  519. img = nil;
  520. uintptr_t datalim;
  521. datalim = 0;
  522. for(i = 0; i < nldseg; i++){
  523. if(img == nil){
  524. img = attachimage(ldseg[i].type, chan, up->color,
  525. ldseg[i].pg0vaddr,
  526. (ldseg[i].pg0off+ldseg[i].memsz+BIGPGSZ-1)/BIGPGSZ
  527. );
  528. s = img->s;
  529. s->flushme = 1;
  530. if(img->color != up->color)
  531. up->color = img->color;
  532. unlock(&img->r.l);
  533. } else {
  534. s = newseg(ldseg[i].type, ldseg[i].pg0vaddr, (ldseg[i].pg0off+ldseg[i].memsz+BIGPGSZ-1)/BIGPGSZ);
  535. s->color = up->color;
  536. incref(&img->r);
  537. s->image = img;
  538. }
  539. s->ldseg = ldseg[i];
  540. up->seg[sno++] = s;
  541. if(datalim < ldseg[i].pg0vaddr+ldseg[i].memsz)
  542. datalim = ldseg[i].pg0vaddr+ldseg[i].memsz;
  543. }
  544. /* BSS. Zero fill on demand for TS */
  545. s = newseg(SG_BSS|SG_READ|SG_WRITE, (datalim + BIGPGSZ-1) & ~(BIGPGSZ-1), 0);
  546. up->seg[sno++] = s;
  547. s->color= up->color;
  548. for(i = 0; i < sno; i++){
  549. s = up->seg[i];
  550. DBG(
  551. "execac %d %s(%c%c%c) %p:%p va %p off %p fsz %d msz %d\n",
  552. up->pid, segtypes[s->type & SG_TYPE],
  553. (s->type & SG_READ) != 0 ? 'r' : '-',
  554. (s->type & SG_WRITE) != 0 ? 'w' : '-',
  555. (s->type & SG_EXEC) != 0 ? 'x' : '-',
  556. s->base, s->top,
  557. s->ldseg.pg0vaddr+s->ldseg.pg0off,
  558. s->ldseg.pg0fileoff+s->ldseg.pg0off,
  559. s->ldseg.filesz,
  560. s->ldseg.memsz
  561. );
  562. }
  563. /* the color of the stack was decided when we created it before,
  564. * it may have nothing to do with the color of other segments.
  565. */
  566. qunlock(&up->seglock);
  567. poperror(); /* seglock */
  568. /*
  569. * '/' processes are higher priority
  570. * aki: why bother?
  571. *
  572. * if(chan->dev->dc == L'/')
  573. * up->basepri = PriRoot;
  574. */
  575. up->priority = up->basepri;
  576. poperror(); /* chan, elem, file */
  577. cclose(chan);
  578. free(file);
  579. /*
  580. * At this point, the mmu contains info about the old address
  581. * space and needs to be flushed
  582. */
  583. mmuflush();
  584. if(up->prepagemem || flags == EXAC)
  585. nixprepage(-1);
  586. qlock(&up->debug);
  587. up->nnote = 0;
  588. up->notify = 0;
  589. up->notified = 0;
  590. up->privatemem = 0;
  591. sysprocsetup(up);
  592. qunlock(&up->debug);
  593. if(up->hang)
  594. up->procctl = Proc_stopme;
  595. /* we need to compte the value of &argv in user mode and then push that. */
  596. ar0->v = sysexecregs(entry, TSTKTOP - PTR2UINT(argv), ((void *)tos) + (USTKTOP-TSTKTOP)/sizeof(void *));
  597. if(flags == EXAC){
  598. up->procctl = Proc_toac;
  599. up->prepagemem = 1;
  600. }
  601. }
  602. void
  603. sysexecac(Ar0* ar0, ...)
  604. {
  605. int flags;
  606. char *file, **argv;
  607. va_list list;
  608. va_start(list, ar0);
  609. /*
  610. * void* execac(int flags, char* name, char* argv[]);
  611. */
  612. flags = va_arg(list, unsigned int);
  613. file = va_arg(list, char*);
  614. file = validaddr(file, 1, 0);
  615. argv = va_arg(list, char**);
  616. va_end(list);
  617. evenaddr(PTR2UINT(argv));
  618. execac(ar0, flags, file, argv);
  619. }
  620. void
  621. sysexec(Ar0* ar0, ...)
  622. {
  623. char *file, **argv;
  624. va_list list;
  625. va_start(list, ar0);
  626. /*
  627. * void* exec(char* name, char* argv[]);
  628. */
  629. file = va_arg(list, char*);
  630. file = validaddr(file, 1, 0);
  631. argv = va_arg(list, char**);
  632. va_end(list);
  633. evenaddr(PTR2UINT(argv));
  634. execac(ar0, EXTC, file, argv);
  635. }
  636. void
  637. sysr1(Ar0* ar, ...)
  638. {
  639. print("sysr1() called. recompile your binary\n");
  640. }
  641. void
  642. sysnixsyscall(Ar0* ar, ...)
  643. {
  644. print("nixsyscall() called. recompile your binary\n");
  645. }
  646. int
  647. return0(void* v)
  648. {
  649. return 0;
  650. }
  651. void
  652. syssleep(Ar0* ar0, ...)
  653. {
  654. Proc *up = externup();
  655. int64_t ms;
  656. va_list list;
  657. va_start(list, ar0);
  658. /*
  659. * int sleep(long millisecs);
  660. */
  661. ms = va_arg(list, int64_t);
  662. va_end(list);
  663. ar0->i = 0;
  664. if(ms <= 0) {
  665. if (up->edf && (up->edf->flags & Admitted))
  666. edfyield();
  667. else
  668. yield();
  669. return;
  670. }
  671. if(ms < TK2MS(1))
  672. ms = TK2MS(1);
  673. tsleep(&up->sleep, return0, 0, ms);
  674. }
  675. void
  676. sysalarm(Ar0* ar0, ...)
  677. {
  678. unsigned long ms;
  679. va_list list;
  680. va_start(list, ar0);
  681. /*
  682. * long alarm(unsigned long millisecs);
  683. * Odd argument type...
  684. */
  685. ms = va_arg(list, unsigned long);
  686. va_end(list);
  687. ar0->l = procalarm(ms);
  688. }
  689. void
  690. sysexits(Ar0* ar0, ...)
  691. {
  692. Proc *up = externup();
  693. char *status;
  694. char *inval = "invalid exit string";
  695. char buf[ERRMAX];
  696. va_list list;
  697. va_start(list, ar0);
  698. /*
  699. * void exits(char *msg);
  700. */
  701. status = va_arg(list, char*);
  702. va_end(list);
  703. if(status){
  704. if(waserror())
  705. status = inval;
  706. else{
  707. status = validaddr(status, 1, 0);
  708. if(vmemchr(status, 0, ERRMAX) == 0){
  709. memmove(buf, status, ERRMAX);
  710. buf[ERRMAX-1] = 0;
  711. status = buf;
  712. }
  713. poperror();
  714. }
  715. }
  716. pexit(status, 1);
  717. }
  718. void
  719. sys_wait(Ar0* ar0, ...)
  720. {
  721. int pid;
  722. Waitmsg w;
  723. OWaitmsg *ow;
  724. va_list list;
  725. va_start(list, ar0);
  726. /*
  727. * int wait(Waitmsg* w);
  728. *
  729. * Deprecated; backwards compatibility only.
  730. */
  731. ow = va_arg(list, OWaitmsg*);
  732. va_end(list);
  733. if(ow == nil){
  734. ar0->i = pwait(nil);
  735. return;
  736. }
  737. ow = validaddr(ow, sizeof(OWaitmsg), 1);
  738. evenaddr(PTR2UINT(ow));
  739. pid = pwait(&w);
  740. if(pid >= 0){
  741. readnum(0, ow->pid, NUMSIZE, w.pid, NUMSIZE);
  742. readnum(0, ow->time+TUser*NUMSIZE, NUMSIZE, w.time[TUser], NUMSIZE);
  743. readnum(0, ow->time+TSys*NUMSIZE, NUMSIZE, w.time[TSys], NUMSIZE);
  744. readnum(0, ow->time+TReal*NUMSIZE, NUMSIZE, w.time[TReal], NUMSIZE);
  745. strncpy(ow->msg, w.msg, sizeof(ow->msg));
  746. ow->msg[sizeof(ow->msg)-1] = '\0';
  747. }
  748. ar0->i = pid;
  749. }
  750. void
  751. sysawait(Ar0* ar0, ...)
  752. {
  753. int i;
  754. int pid;
  755. Waitmsg w;
  756. usize n;
  757. char *p;
  758. va_list list;
  759. va_start(list, ar0);
  760. /*
  761. * int await(char* s, int n);
  762. * should really be
  763. * usize await(char* s, usize n);
  764. */
  765. p = va_arg(list, char*);
  766. n = va_arg(list, int32_t);
  767. va_end(list);
  768. p = validaddr(p, n, 1);
  769. pid = pwait(&w);
  770. if(pid < 0){
  771. ar0->i = -1;
  772. return;
  773. }
  774. i = snprint(p, n, "%d %lu %lu %lu %q",
  775. w.pid,
  776. w.time[TUser], w.time[TSys], w.time[TReal],
  777. w.msg);
  778. ar0->i = i;
  779. }
  780. void
  781. werrstr(char *fmt, ...)
  782. {
  783. Proc *up = externup();
  784. va_list va;
  785. if(up == nil)
  786. return;
  787. va_start(va, fmt);
  788. vseprint(up->syserrstr, up->syserrstr+ERRMAX, fmt, va);
  789. va_end(va);
  790. }
  791. static void
  792. generrstr(char *buf, int32_t n)
  793. {
  794. Proc *up = externup();
  795. char *p, tmp[ERRMAX];
  796. if(n <= 0)
  797. error(Ebadarg);
  798. p = validaddr(buf, n, 1);
  799. if(n > sizeof tmp)
  800. n = sizeof tmp;
  801. memmove(tmp, p, n);
  802. /* make sure it's NUL-terminated */
  803. tmp[n-1] = '\0';
  804. memmove(p, up->syserrstr, n);
  805. p[n-1] = '\0';
  806. memmove(up->syserrstr, tmp, n);
  807. }
  808. void
  809. syserrstr(Ar0* ar0, ...)
  810. {
  811. char *err;
  812. usize nerr;
  813. va_list list;
  814. va_start(list, ar0);
  815. /*
  816. * int errstr(char* err, uint nerr);
  817. * should really be
  818. * usize errstr(char* err, usize nerr);
  819. * but errstr always returns 0.
  820. */
  821. err = va_arg(list, char*);
  822. nerr = va_arg(list, usize);
  823. va_end(list);
  824. generrstr(err, nerr);
  825. ar0->i = 0;
  826. }
  827. void
  828. sys_errstr(Ar0* ar0, ...)
  829. {
  830. char *p;
  831. va_list list;
  832. va_start(list, ar0);
  833. /*
  834. * int errstr(char* err);
  835. *
  836. * Deprecated; backwards compatibility only.
  837. */
  838. p = va_arg(list, char*);
  839. va_end(list);
  840. generrstr(p, 64);
  841. ar0->i = 0;
  842. }
  843. void
  844. sysnotify(Ar0* ar0, ...)
  845. {
  846. Proc *up = externup();
  847. void (*f)(void*, char*);
  848. va_list list;
  849. va_start(list, ar0);
  850. /*
  851. * int notify(void (*f)(void*, char*));
  852. */
  853. f = (void (*)(void*, char*))va_arg(list, void*);
  854. va_end(list);
  855. if(f != nil)
  856. validaddr(f, sizeof(void (*)(void*, char*)), 0);
  857. up->notify = f;
  858. ar0->i = 0;
  859. }
  860. void
  861. sysnoted(Ar0* ar0, ...)
  862. {
  863. Proc *up = externup();
  864. int v;
  865. va_list list;
  866. va_start(list, ar0);
  867. /*
  868. * int noted(int v);
  869. */
  870. v = va_arg(list, int);
  871. va_end(list);
  872. if(v != NRSTR && !up->notified)
  873. error(Egreg);
  874. ar0->i = 0;
  875. }
  876. void
  877. sysrendezvous(Ar0* ar0, ...)
  878. {
  879. Proc *up = externup();
  880. Proc *p, **l;
  881. uintptr_t tag, val;
  882. va_list list;
  883. va_start(list, ar0);
  884. /*
  885. * void* rendezvous(void*, void*);
  886. */
  887. tag = PTR2UINT(va_arg(list, void*));
  888. l = &REND(up->rgrp, tag);
  889. up->rendval = ~0;
  890. lock(&up->rgrp->r.l);
  891. for(p = *l; p; p = p->rendhash) {
  892. if(p->rendtag == tag) {
  893. *l = p->rendhash;
  894. val = p->rendval;
  895. p->rendval = PTR2UINT(va_arg(list, void*));
  896. while(p->mach != 0)
  897. ;
  898. ready(p);
  899. unlock(&up->rgrp->r.l);
  900. ar0->v = UINT2PTR(val);
  901. return;
  902. }
  903. l = &p->rendhash;
  904. }
  905. /* Going to sleep here */
  906. up->rendtag = tag;
  907. up->rendval = PTR2UINT(va_arg(list, void*));
  908. va_end(list);
  909. up->rendhash = *l;
  910. *l = up;
  911. up->state = Rendezvous;
  912. if(up->trace)
  913. proctrace(up, SLock, 0);
  914. unlock(&up->rgrp->r.l);
  915. sched();
  916. ar0->v = UINT2PTR(up->rendval);
  917. }
  918. /*
  919. * The implementation of semaphores is complicated by needing
  920. * to avoid rescheduling in syssemrelease, so that it is safe
  921. * to call from real-time processes. This means syssemrelease
  922. * cannot acquire any qlocks, only spin locks.
  923. *
  924. * Semacquire and semrelease must both manipulate the semaphore
  925. * wait list. Lock-free linked lists only exist in theory, not
  926. * in practice, so the wait list is protected by a spin lock.
  927. *
  928. * The semaphore value *addr is stored in user memory, so it
  929. * cannot be read or written while holding spin locks.
  930. *
  931. * Thus, we can access the list only when holding the lock, and
  932. * we can access the semaphore only when not holding the lock.
  933. * This makes things interesting. Note that sleep's condition function
  934. * is called while holding two locks - r and up->rlock - so it cannot
  935. * access the semaphore value either.
  936. *
  937. * An acquirer announces its intention to try for the semaphore
  938. * by putting a Sema structure onto the wait list and then
  939. * setting Sema.waiting. After one last check of semaphore,
  940. * the acquirer sleeps until Sema.waiting==0. A releaser of n
  941. * must wake up n acquirers who have Sema.waiting set. It does
  942. * this by clearing Sema.waiting and then calling wakeup.
  943. *
  944. * There are three interesting races here.
  945. * The first is that in this particular sleep/wakeup usage, a single
  946. * wakeup can rouse a process from two consecutive sleeps!
  947. * The ordering is:
  948. *
  949. * (a) set Sema.waiting = 1
  950. * (a) call sleep
  951. * (b) set Sema.waiting = 0
  952. * (a) check Sema.waiting inside sleep, return w/o sleeping
  953. * (a) try for semaphore, fail
  954. * (a) set Sema.waiting = 1
  955. * (a) call sleep
  956. * (b) call wakeup(a)
  957. * (a) wake up again
  958. *
  959. * This is okay - semacquire will just go around the loop
  960. * again. It does mean that at the top of the for(;;) loop in
  961. * semacquire, phore.waiting might already be set to 1.
  962. *
  963. * The second is that a releaser might wake an acquirer who is
  964. * interrupted before he can acquire the lock. Since
  965. * release(n) issues only n wakeup calls -- only n can be used
  966. * anyway -- if the interrupted process is not going to use his
  967. * wakeup call he must pass it on to another acquirer.
  968. *
  969. * The third race is similar to the second but more subtle. An
  970. * acquirer sets waiting=1 and then does a final canacquire()
  971. * before going to sleep. The opposite order would result in
  972. * missing wakeups that happen between canacquire and
  973. * waiting=1. (In fact, the whole point of Sema.waiting is to
  974. * avoid missing wakeups between canacquire() and sleep().) But
  975. * there can be spurious wakeups between a successful
  976. * canacquire() and the following semdequeue(). This wakeup is
  977. * not useful to the acquirer, since he has already acquired
  978. * the semaphore. Like in the previous case, though, the
  979. * acquirer must pass the wakeup call along.
  980. *
  981. * This is all rather subtle. The code below has been verified
  982. * with the spin model /sys/src/9/port/semaphore.p. The
  983. * original code anticipated the second race but not the first
  984. * or third, which were caught only with spin. The first race
  985. * is mentioned in /sys/doc/sleep.ps, but I'd forgotten about it.
  986. * It was lucky that my abstract model of sleep/wakeup still managed
  987. * to preserve that behavior.
  988. *
  989. * I remain slightly concerned about memory coherence
  990. * outside of locks. The spin model does not take
  991. * queued processor writes into account so we have to
  992. * think hard. The only variables accessed outside locks
  993. * are the semaphore value itself and the boolean flag
  994. * Sema.waiting. The value is only accessed with CAS,
  995. * whose job description includes doing the right thing as
  996. * far as memory coherence across processors. That leaves
  997. * Sema.waiting. To handle it, we call coherence() before each
  998. * read and after each write. - rsc
  999. */
  1000. /* Add semaphore p with addr a to list in seg. */
  1001. static void
  1002. semqueue(Segment* s, int* addr, Sema* p)
  1003. {
  1004. memset(p, 0, sizeof *p);
  1005. p->addr = addr;
  1006. lock(&s->sema.rend.l); /* uses s->sema.Rendez.Lock, but no one else is */
  1007. p->next = &s->sema;
  1008. p->prev = s->sema.prev;
  1009. p->next->prev = p;
  1010. p->prev->next = p;
  1011. unlock(&s->sema.rend.l);
  1012. }
  1013. /* Remove semaphore p from list in seg. */
  1014. static void
  1015. semdequeue(Segment* s, Sema* p)
  1016. {
  1017. lock(&s->sema.rend.l);
  1018. p->next->prev = p->prev;
  1019. p->prev->next = p->next;
  1020. unlock(&s->sema.rend.l);
  1021. }
  1022. /* Wake up n waiters with addr on list in seg. */
  1023. static void
  1024. semwakeup(Segment* s, int* addr, int n)
  1025. {
  1026. Sema *p;
  1027. lock(&s->sema.rend.l);
  1028. for(p = s->sema.next; p != &s->sema && n > 0; p = p->next){
  1029. if(p->addr == addr && p->waiting){
  1030. p->waiting = 0;
  1031. coherence();
  1032. wakeup(&p->rend);
  1033. n--;
  1034. }
  1035. }
  1036. unlock(&s->sema.rend.l);
  1037. }
  1038. /* Add delta to semaphore and wake up waiters as appropriate. */
  1039. static int
  1040. semrelease(Segment* s, int* addr, int delta)
  1041. {
  1042. int value;
  1043. do
  1044. value = *addr;
  1045. while(!CASW(addr, value, value+delta));
  1046. semwakeup(s, addr, delta);
  1047. return value+delta;
  1048. }
  1049. /* Try to acquire semaphore using compare-and-swap */
  1050. static int
  1051. canacquire(int* addr)
  1052. {
  1053. int value;
  1054. while((value = *addr) > 0){
  1055. if(CASW(addr, value, value-1))
  1056. return 1;
  1057. }
  1058. return 0;
  1059. }
  1060. /* Should we wake up? */
  1061. static int
  1062. semawoke(void* p)
  1063. {
  1064. coherence();
  1065. return !((Sema*)p)->waiting;
  1066. }
  1067. /* Acquire semaphore (subtract 1). */
  1068. static int
  1069. semacquire(Segment* s, int* addr, int block)
  1070. {
  1071. Proc *up = externup();
  1072. int acquired;
  1073. Sema phore;
  1074. if(canacquire(addr))
  1075. return 1;
  1076. if(!block)
  1077. return 0;
  1078. acquired = 0;
  1079. semqueue(s, addr, &phore);
  1080. for(;;){
  1081. phore.waiting = 1;
  1082. coherence();
  1083. if(canacquire(addr)){
  1084. acquired = 1;
  1085. break;
  1086. }
  1087. if(waserror())
  1088. break;
  1089. sleep(&phore.rend, semawoke, &phore);
  1090. poperror();
  1091. }
  1092. semdequeue(s, &phore);
  1093. coherence(); /* not strictly necessary due to lock in semdequeue */
  1094. if(!phore.waiting)
  1095. semwakeup(s, addr, 1);
  1096. if(!acquired)
  1097. nexterror();
  1098. return 1;
  1099. }
  1100. /* Acquire semaphore or time-out */
  1101. static int
  1102. tsemacquire(Segment* s, int* addr, int32_t ms)
  1103. {
  1104. Proc *up = externup();
  1105. int acquired;
  1106. uint32_t t;
  1107. Sema phore;
  1108. if(canacquire(addr))
  1109. return 1;
  1110. if(ms == 0)
  1111. return 0;
  1112. acquired = 0;
  1113. semqueue(s, addr, &phore);
  1114. for(;;){
  1115. phore.waiting = 1;
  1116. coherence();
  1117. if(canacquire(addr)){
  1118. acquired = 1;
  1119. break;
  1120. }
  1121. if(waserror())
  1122. break;
  1123. t = sys->ticks;
  1124. tsleep(&phore.rend, semawoke, &phore, ms);
  1125. ms -= TK2MS(sys->ticks-t);
  1126. poperror();
  1127. if(ms <= 0)
  1128. break;
  1129. }
  1130. semdequeue(s, &phore);
  1131. coherence(); /* not strictly necessary due to lock in semdequeue */
  1132. if(!phore.waiting)
  1133. semwakeup(s, addr, 1);
  1134. if(ms <= 0)
  1135. return 0;
  1136. if(!acquired)
  1137. nexterror();
  1138. return 1;
  1139. }
  1140. void
  1141. syssemacquire(Ar0* ar0, ...)
  1142. {
  1143. Proc *up = externup();
  1144. Segment *s;
  1145. int *addr, block;
  1146. va_list list;
  1147. va_start(list, ar0);
  1148. /*
  1149. * int semacquire(long* addr, int block);
  1150. * should be (and will be implemented below as) perhaps
  1151. * int semacquire(int* addr, int block);
  1152. */
  1153. addr = va_arg(list, int*);
  1154. addr = validaddr(addr, sizeof(int), 1);
  1155. evenaddr(PTR2UINT(addr));
  1156. block = va_arg(list, int);
  1157. va_end(list);
  1158. if((s = seg(up, PTR2UINT(addr), 0)) == nil)
  1159. error(Ebadarg);
  1160. if(*addr < 0)
  1161. error(Ebadarg);
  1162. ar0->i = semacquire(s, addr, block);
  1163. }
  1164. void
  1165. systsemacquire(Ar0* ar0, ...)
  1166. {
  1167. Proc *up = externup();
  1168. Segment *s;
  1169. int *addr, ms;
  1170. va_list list;
  1171. va_start(list, ar0);
  1172. /*
  1173. * int tsemacquire(long* addr, uint32_t ms);
  1174. * should be (and will be implemented below as) perhaps
  1175. * int tsemacquire(int* addr, uint32_t ms);
  1176. */
  1177. addr = va_arg(list, int*);
  1178. addr = validaddr(addr, sizeof(int), 1);
  1179. evenaddr(PTR2UINT(addr));
  1180. ms = va_arg(list, uint32_t);
  1181. va_end(list);
  1182. if((s = seg(up, PTR2UINT(addr), 0)) == nil)
  1183. error(Ebadarg);
  1184. if(*addr < 0)
  1185. error(Ebadarg);
  1186. ar0->i = tsemacquire(s, addr, ms);
  1187. }
  1188. void
  1189. syssemrelease(Ar0* ar0, ...)
  1190. {
  1191. Proc *up = externup();
  1192. Segment *s;
  1193. int *addr, delta;
  1194. va_list list;
  1195. va_start(list, ar0);
  1196. /*
  1197. * long semrelease(long* addr, long count);
  1198. * should be (and will be implemented below as) perhaps
  1199. * int semrelease(int* addr, int count);
  1200. */
  1201. addr = va_arg(list, int*);
  1202. addr = validaddr(addr, sizeof(int), 1);
  1203. evenaddr(PTR2UINT(addr));
  1204. delta = va_arg(list, int);
  1205. va_end(list);
  1206. if((s = seg(up, PTR2UINT(addr), 0)) == nil)
  1207. error(Ebadarg);
  1208. if(delta < 0 || *addr < 0)
  1209. error(Ebadarg);
  1210. ar0->i = semrelease(s, addr, delta);
  1211. }