sysproc.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003
  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 <a.out.h>
  18. #include <trace.h>
  19. #undef DBG
  20. #define DBG print
  21. /* this is ugly but we need libmach in the kernel. So this is a first pass.
  22. * FIX ME.
  23. */
  24. #include "ureg.h" /* for Elfmach struct */
  25. typedef struct Fhdr Fhdr;
  26. typedef struct Fhdr
  27. {
  28. char *name; /* identifier of executable */
  29. uint8_t type; /* file type - see codes above */
  30. uint8_t hdrsz; /* header size */
  31. uint8_t _magic; /* _MAGIC() magic */
  32. uint8_t spare;
  33. int32_t magic; /* magic number */
  34. uint64_t txtaddr; /* text address */
  35. int64_t txtoff; /* start of text in file */
  36. uint64_t dataddr; /* start of data segment */
  37. int64_t datoff; /* offset to data seg in file */
  38. int64_t symoff; /* offset of symbol table in file */
  39. uint64_t entry; /* entry point */
  40. int64_t sppcoff; /* offset of sp-pc table in file */
  41. int64_t lnpcoff; /* offset of line number-pc table in file */
  42. int32_t txtsz; /* text size */
  43. int32_t datsz; /* size of data seg */
  44. int32_t bsssz; /* size of bss */
  45. int32_t symsz; /* size of symbol table */
  46. int32_t sppcsz; /* size of sp-pc table */
  47. int32_t lnpcsz; /* size of line number-pc table */
  48. /* add the indexes of the ELF text and data segments. This is one awful hack
  49. * but we want to get this plane off the ground. We can fix it better later.
  50. */
  51. int it, id;
  52. } Fhdr;
  53. /*
  54. * Common a.out header describing all architectures
  55. */
  56. typedef struct {
  57. union{
  58. struct {
  59. Exec; /* a.out.h */
  60. uint64_t hdr[1];
  61. };
  62. E64hdr; /* elf.h */
  63. } e;
  64. int32_t dummy; /* padding to ensure extra long */
  65. } ExecHdr;
  66. typedef struct Elfmach
  67. {
  68. char *name;
  69. int mtype; /* machine type code */
  70. int32_t regsize; /* sizeof registers in bytes */
  71. int32_t fpregsize; /* sizeof fp registers in bytes */
  72. char *pc; /* pc name */
  73. char *sp; /* sp name */
  74. char *link; /* link register name */
  75. char *sbreg; /* static base register name */
  76. uint64_t sb; /* static base register value */
  77. int pgsize; /* page size */
  78. uint64_t kbase; /* kernel base address */
  79. uint64_t ktmask; /* ktzero = kbase & ~ktmask */
  80. uint64_t utop; /* user stack top */
  81. int pcquant; /* quantization of pc */
  82. int szaddr; /* sizeof(void*) */
  83. int szreg; /* sizeof(register) */
  84. int szfloat; /* sizeof(float) */
  85. int szdouble; /* sizeof(double) */
  86. } Elfmach;
  87. enum {
  88. MAMD64,
  89. FAMD64,
  90. FAMD64B,
  91. };
  92. #define REGSIZE sizeof(struct Ureg)
  93. #define FPREGSIZE 512 /* TO DO? currently only 0x1A0 used */
  94. Elfmach mamd64=
  95. {
  96. "amd64",
  97. MAMD64, /* machine type */
  98. REGSIZE, /* size of registers in bytes */
  99. FPREGSIZE, /* size of fp registers in bytes */
  100. "PC", /* name of PC */
  101. "SP", /* name of SP */
  102. 0, /* link register */
  103. "setSB", /* static base register name (bogus anyways) */
  104. 0, /* static base register value */
  105. 0x200000, /* page size */
  106. 0xfffffffff0110000ull, /* kernel base */
  107. 0xffff800000000000ull, /* kernel text mask */
  108. 0x00007ffffffff000ull, /* user stack top */
  109. 1, /* quantization of pc */
  110. 8, /* szaddr */
  111. 4, /* szreg */
  112. 4, /* szfloat */
  113. 8, /* szdouble */
  114. };
  115. /* definition of per-executable file type structures */
  116. Elfmach *elfmach;
  117. Elfmach *machkind = &mamd64;
  118. typedef struct Exectable{
  119. int32_t magic; /* big-endian magic number of file */
  120. char *name; /* executable identifier */
  121. char *dlmname; /* dynamically loadable module identifier */
  122. uint8_t type; /* Internal code */
  123. uint8_t _magic; /* _MAGIC() magic */
  124. Elfmach *elfmach; /* Per-machine data */
  125. int32_t hsize; /* header size */
  126. uint32_t (*swal)(uint32_t); /* beswal or leswal */
  127. int (*hparse)(Ar0*, Chan*, Fhdr*, ExecHdr*);
  128. } ExecTable;
  129. /* Map from mach.h */
  130. /* Structure to map a segment to a position in a file */
  131. typedef struct Map {
  132. int nsegs; /* number of segments */
  133. struct segment { /* per-segment map */
  134. char *name; /* the segment name */
  135. int fd; /* file descriptor */
  136. int inuse; /* in use - not in use */
  137. int cache; /* should cache reads? */
  138. uint64_t b; /* base */
  139. uint64_t e; /* end */
  140. int64_t f; /* offset within file */
  141. } seg[1]; /* actually n of these */
  142. } Map;
  143. static int crackhdr(Ar0 *ar0, Chan *c, Fhdr *fp, ExecHdr *d);
  144. /* Trying seek */
  145. static int64_t
  146. chanseek(Ar0 *ar0, Chan *c, int64_t offset, int whence)
  147. {
  148. uint8_t buf[sizeof(Dir)+100];
  149. Dir dir;
  150. int n;
  151. if(c->dev->dc == '|')
  152. error(Eisstream);
  153. switch(whence){
  154. case 0:
  155. if((c->qid.type & QTDIR) && offset != 0LL)
  156. error(Eisdir);
  157. c->offset = offset;
  158. break;
  159. case 1:
  160. if(c->qid.type & QTDIR)
  161. error(Eisdir);
  162. lock(c); /* lock for read/write update */
  163. offset += c->offset;
  164. c->offset = offset;
  165. unlock(c);
  166. break;
  167. case 2:
  168. if(c->qid.type & QTDIR)
  169. error(Eisdir);
  170. n = c->dev->stat(c, buf, sizeof buf);
  171. if(convM2D(buf, n, &dir, nil) == 0)
  172. error("internal error: stat error in seek");
  173. offset += dir.length;
  174. c->offset = offset;
  175. break;
  176. default:
  177. error(Ebadarg);
  178. }
  179. c->uri = 0;
  180. c->dri = 0;
  181. if (0) // FIX ME: this cclose is needed later.
  182. cclose(c);
  183. return offset;
  184. }
  185. /* libmach swap.c */
  186. /*
  187. * big-endian int8_t
  188. */
  189. uint16_t
  190. beswab(uint16_t s)
  191. {
  192. uint8_t *p;
  193. p = (uint8_t*)&s;
  194. return (p[0]<<8) | p[1];
  195. }
  196. /* big-endian int32_t */
  197. uint32_t
  198. beswal(uint32_t l)
  199. {
  200. uint8_t *p;
  201. p = (uint8_t*)&l;
  202. return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
  203. }
  204. /* big-endian int64_t */
  205. uint64_t
  206. beswav(uint64_t v)
  207. {
  208. uint8_t *p;
  209. p = (uint8_t*)&v;
  210. return ((uint64_t)p[0]<<56) | ((uint64_t)p[1]<<48) | ((uint64_t)p[2]<<40)
  211. | ((uint64_t)p[3]<<32) | ((uint64_t)p[4]<<24)
  212. | ((uint64_t)p[5]<<16) | ((uint64_t)p[6]<<8)
  213. | (uint64_t)p[7];
  214. }
  215. /*
  216. * little-endian int8_t (short)
  217. */
  218. uint16_t
  219. leswab(uint16_t s)
  220. {
  221. uint8_t *p;
  222. p = (uint8_t*)&s;
  223. return (p[1]<<8) | p[0];
  224. }
  225. /*
  226. * little-endian int32_t
  227. */
  228. uint32_t
  229. leswal(uint32_t l)
  230. {
  231. uint8_t *p;
  232. p = (uint8_t*)&l;
  233. return (p[3]<<24) | (p[2]<<16) | (p[1]<<8) | p[0];
  234. }
  235. /*
  236. * little-endian int64_t
  237. */
  238. uint64_t
  239. leswav(uint64_t v)
  240. {
  241. uint8_t *p;
  242. p = (uint8_t*)&v;
  243. return ((uint64_t)p[7]<<56) | ((uint64_t)p[6]<<48) | ((uint64_t)p[5]<<40)
  244. | ((uint64_t)p[4]<<32) | ((uint64_t)p[3]<<24)
  245. | ((uint64_t)p[2]<<16) | ((uint64_t)p[1]<<8)
  246. | (uint64_t)p[0];
  247. }
  248. /* Atomics */
  249. static void
  250. settext(Fhdr *fp, uint64_t e, uint64_t a, int32_t s, int64_t off)
  251. {
  252. fp->txtaddr = a;
  253. fp->entry = e;
  254. fp->txtsz = s;
  255. fp->txtoff = off;
  256. }
  257. static void
  258. setdata(Fhdr *fp, uint64_t a, int32_t s, int64_t off, int32_t bss)
  259. {
  260. fp->dataddr = a;
  261. fp->datsz = s;
  262. fp->datoff = off;
  263. fp->bsssz = bss;
  264. }
  265. static void
  266. setsym(Fhdr *fp, int32_t symsz, int32_t sppcsz, int32_t lnpcsz,
  267. int64_t symoff)
  268. {
  269. fp->symsz = symsz;
  270. fp->symoff = symoff;
  271. fp->sppcsz = sppcsz;
  272. fp->sppcoff = fp->symoff+fp->symsz;
  273. fp->lnpcsz = lnpcsz;
  274. fp->lnpcoff = fp->sppcoff+fp->sppcsz;
  275. }
  276. #if 0
  277. static uint64_t
  278. _round(uint64_t a, uint32_t b)
  279. {
  280. uint64_t w;
  281. w = (a/b)*b;
  282. if (a!=w)
  283. w += b;
  284. return(w);
  285. }
  286. #endif
  287. /* Convert header to canonical form */
  288. static void
  289. hswal(void *v, int n, uint32_t (*swap)(uint32_t))
  290. {
  291. uint32_t *ulp;
  292. for(ulp = v; n--; ulp++)
  293. *ulp = (*swap)(*ulp);
  294. }
  295. /* map.c */
  296. int
  297. findseg(Map *map, char *name)
  298. {
  299. int i;
  300. if (!map)
  301. return -1;
  302. for (i = 0; i < map->nsegs; i++)
  303. if (map->seg[i].inuse && !strcmp(map->seg[i].name, name))
  304. return i;
  305. return -1;
  306. }
  307. Map *
  308. newmap(Map *map, int n)
  309. {
  310. int size;
  311. size = sizeof(Map)+(n-1)*sizeof(struct segment);
  312. if (map == 0)
  313. map = malloc(size);
  314. else
  315. map = realloc(map, size);
  316. if (map == 0) {
  317. error("out of memory: %r");
  318. return 0;
  319. }
  320. memset(map, 0, size);
  321. map->nsegs = n;
  322. return map;
  323. }
  324. Map*
  325. loadmap(Map *map, int fd, Fhdr *fp)
  326. {
  327. map = newmap(map, 2);
  328. if (map == 0)
  329. return 0;
  330. map->seg[0].b = fp->txtaddr;
  331. map->seg[0].e = fp->txtaddr+fp->txtsz;
  332. map->seg[0].f = fp->txtoff;
  333. map->seg[0].fd = fd;
  334. map->seg[0].inuse = 1;
  335. map->seg[0].name = "text";
  336. map->seg[1].b = fp->dataddr;
  337. map->seg[1].e = fp->dataddr+fp->datsz;
  338. map->seg[1].f = fp->datoff;
  339. map->seg[1].fd = fd;
  340. map->seg[1].inuse = 1;
  341. map->seg[1].name = "data";
  342. return map;
  343. }
  344. /* commons */
  345. #if 0
  346. static void
  347. commonboot(Fhdr *fp)
  348. {
  349. switch(fp->type) { /* boot image */
  350. case FAMD64:
  351. fp->type = FAMD64B;
  352. fp->txtaddr = fp->entry;
  353. fp->name = "amd64 plan 9 boot image";
  354. fp->dataddr = _round(fp->txtaddr+fp->txtsz, 4096);
  355. break;
  356. default:
  357. return;
  358. }
  359. fp->hdrsz = 0; /* header stripped */
  360. }
  361. static int
  362. commonllp64(Ar0 *ar0, Chan *c, Fhdr *fp, ExecHdr *hp)
  363. {
  364. int32_t pgsize;
  365. uint64_t entry;
  366. hswal(&hp->e, sizeof(Exec)/sizeof(int32_t), beswal);
  367. if(!(hp->e.magic & HDR_MAGIC))
  368. return 0;
  369. /*
  370. * There can be more magic here if the
  371. * header ever needs more expansion.
  372. * For now just catch use of any of the
  373. * unused bits.
  374. */
  375. if((hp->e.magic & ~DYN_MAGIC)>>16)
  376. return 0;
  377. entry = beswav(hp->e.hdr[0]);
  378. pgsize = elfmach->pgsize;
  379. settext(fp, entry, pgsize+fp->hdrsz, hp->e.text, fp->hdrsz);
  380. setdata(fp, _round(pgsize+fp->txtsz+fp->hdrsz, pgsize),
  381. hp->e.data, fp->txtsz+fp->hdrsz, hp->e.bss);
  382. setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
  383. if(hp->e.magic & DYN_MAGIC) {
  384. fp->txtaddr = 0;
  385. fp->dataddr = fp->txtsz;
  386. return 1;
  387. }
  388. commonboot(fp);
  389. return 1;
  390. }
  391. #endif
  392. /* ELF */
  393. static int
  394. elf64dotout(Ar0 *ar0, Chan *c, Fhdr *fp, ExecHdr *hp)
  395. {
  396. E64hdr *ep;
  397. uint16_t (*swab)(uint16_t);
  398. uint32_t (*swal)(uint32_t);
  399. uint64_t (*swav)(uint64_t);
  400. int i, is, phsz;
  401. uint64_t uvl;
  402. ep = &hp->e;
  403. if(ep->ident[DATA] == ELFDATA2LSB) {
  404. swab = leswab;
  405. swal = leswal;
  406. swav = leswav;
  407. } else if(ep->ident[DATA] == ELFDATA2MSB) {
  408. swab = beswab;
  409. swal = beswal;
  410. swav = beswav;
  411. } else {
  412. error("bad ELF64 encoding - not big or little endian");
  413. return 0;
  414. }
  415. ep->type = swab(ep->type);
  416. ep->machine = swab(ep->machine);
  417. ep->version = swal(ep->version);
  418. if(ep->type != EXEC || ep->version != CURRENT)
  419. return 0;
  420. ep->elfentry = swav(ep->elfentry);
  421. ep->phoff = swav(ep->phoff);
  422. ep->shoff = swav(ep->shoff);
  423. ep->flags = swal(ep->flags);
  424. ep->ehsize = swab(ep->ehsize);
  425. ep->phentsize = swab(ep->phentsize);
  426. ep->phnum = swab(ep->phnum);
  427. ep->shentsize = swab(ep->shentsize);
  428. ep->shnum = swab(ep->shnum);
  429. ep->shstrndx = swab(ep->shstrndx);
  430. fp->magic = ELF_MAG;
  431. fp->hdrsz = (ep->ehsize+ep->phnum*ep->phentsize+16)&~15;
  432. elfmach = &mamd64;
  433. fp->type = FAMD64;
  434. fp->name = "amd64 ELF64 executable";
  435. if(ep->phentsize != sizeof(P64hdr)) {
  436. error("bad ELF64 header size");
  437. return 0;
  438. }
  439. phsz = sizeof(P64hdr)*ep->phnum;
  440. hp->e.ph = malloc(phsz);
  441. if(hp->e.ph == nil)
  442. return 0;
  443. chanseek(ar0, c, ep->phoff, 0);
  444. if(c->dev->read(c, hp->e.ph, phsz, c->offset) < 0){
  445. free(hp->e.ph);
  446. return 0;
  447. }
  448. for(i = 0; i < ep->phnum; i++) {
  449. hp->e.ph[i].type = swal(hp->e.ph[i].type);
  450. hp->e.ph[i].flags = swal(hp->e.ph[i].flags);
  451. hp->e.ph[i].offset = swav(hp->e.ph[i].offset);
  452. hp->e.ph[i].vaddr = swav(hp->e.ph[i].vaddr);
  453. hp->e.ph[i].paddr = swav(hp->e.ph[i].paddr);
  454. hp->e.ph[i].filesz = swav(hp->e.ph[i].filesz);
  455. hp->e.ph[i].memsz = swav(hp->e.ph[i].memsz);
  456. hp->e.ph[i].align = swav(hp->e.ph[i].align);
  457. }
  458. /* find text, data and symbols and install them */
  459. fp->it = fp->id = is = -1;
  460. for(i = 0; i < ep->phnum; i++) {
  461. if(hp->e.ph[i].type == LOAD
  462. && (hp->e.ph[i].flags & (R|X)) == (R|X) && fp->it == -1)
  463. fp->it = i;
  464. else if(hp->e.ph[i].type == LOAD
  465. && (hp->e.ph[i].flags & (R|W)) == (R|W) && fp->id == -1)
  466. fp->id = i;
  467. else if(hp->e.ph[i].type == NOPTYPE && is == -1)
  468. is = i;
  469. }
  470. if(fp->it == -1 || fp->id == -1) {
  471. error("No ELF64 TEXT or DATA sections");
  472. free(hp->e.ph);
  473. return 0;
  474. }
  475. settext(fp, ep->elfentry, hp->e.ph[fp->it].vaddr, hp->e.ph[fp->it].memsz, hp->e.ph[fp->it].offset);
  476. /* note: this comment refers to a bug in 8c. Who cares? We need to move on. */
  477. /* 8c: out of fixed registers */
  478. uvl = hp->e.ph[fp->id].memsz - hp->e.ph[fp->id].filesz;
  479. setdata(fp, hp->e.ph[fp->id].vaddr, hp->e.ph[fp->id].filesz, hp->e.ph[fp->id].offset, uvl);
  480. if(is != -1)
  481. setsym(fp, hp->e.ph[is].filesz, 0, hp->e.ph[is].memsz, hp->e.ph[is].offset);
  482. return 1;
  483. }
  484. static int
  485. elfdotout(Ar0 *ar0, Chan *c, Fhdr *fp, ExecHdr *hp)
  486. {
  487. E64hdr *ep;
  488. /* bitswap the header according to the DATA format */
  489. ep = &hp->e;
  490. if(ep->ident[CLASS] == ELFCLASS64)
  491. return elf64dotout(ar0, c, fp, hp);
  492. error("bad ELF class - not 64-bit");
  493. return 0;
  494. }
  495. ExecTable exectab[] =
  496. {
  497. { ELF_MAG, /* any ELF */
  498. "elf executable",
  499. nil,
  500. 0, /* FNONE */
  501. 0,
  502. &mamd64, /* Mach* type */
  503. sizeof(E64hdr),
  504. nil,
  505. elfdotout },
  506. { 0 },
  507. };
  508. /* End of libmach */
  509. void
  510. sysrfork(Ar0* ar0, ...)
  511. {
  512. Mach *m = machp();
  513. Proc *p;
  514. int flag, i, n, pid;
  515. Fgrp *ofg;
  516. Pgrp *opg;
  517. Rgrp *org;
  518. Egrp *oeg;
  519. Mach *wm;
  520. va_list list;
  521. va_start(list, ar0);
  522. /*
  523. * int rfork(int);
  524. */
  525. flag = va_arg(list, int);
  526. va_end(list);
  527. /* Check flags before we commit */
  528. if((flag & (RFFDG|RFCFDG)) == (RFFDG|RFCFDG))
  529. error(Ebadarg);
  530. if((flag & (RFNAMEG|RFCNAMEG)) == (RFNAMEG|RFCNAMEG))
  531. error(Ebadarg);
  532. if((flag & (RFENVG|RFCENVG)) == (RFENVG|RFCENVG))
  533. error(Ebadarg);
  534. if((flag & (RFPREPAGE|RFCPREPAGE)) == (RFPREPAGE|RFCPREPAGE))
  535. error(Ebadarg);
  536. if((flag & (RFCORE|RFCCORE)) == (RFCORE|RFCCORE))
  537. error(Ebadarg);
  538. if(flag & RFCORE && m->externup->wired != nil)
  539. error("wired proc cannot move to ac");
  540. if((flag&RFPROC) == 0) {
  541. if(flag & (RFMEM|RFNOWAIT))
  542. error(Ebadarg);
  543. if(flag & (RFFDG|RFCFDG)) {
  544. ofg = m->externup->fgrp;
  545. if(flag & RFFDG)
  546. m->externup->fgrp = dupfgrp(ofg);
  547. else
  548. m->externup->fgrp = dupfgrp(nil);
  549. closefgrp(ofg);
  550. }
  551. if(flag & (RFNAMEG|RFCNAMEG)) {
  552. opg = m->externup->pgrp;
  553. m->externup->pgrp = newpgrp();
  554. if(flag & RFNAMEG)
  555. pgrpcpy(m->externup->pgrp, opg);
  556. /* inherit noattach */
  557. m->externup->pgrp->noattach = opg->noattach;
  558. closepgrp(opg);
  559. }
  560. if(flag & RFNOMNT)
  561. m->externup->pgrp->noattach = 1;
  562. if(flag & RFREND) {
  563. org = m->externup->rgrp;
  564. m->externup->rgrp = newrgrp();
  565. closergrp(org);
  566. }
  567. if(flag & (RFENVG|RFCENVG)) {
  568. oeg = m->externup->egrp;
  569. m->externup->egrp = smalloc(sizeof(Egrp));
  570. m->externup->egrp->ref = 1;
  571. if(flag & RFENVG)
  572. envcpy(m->externup->egrp, oeg);
  573. closeegrp(oeg);
  574. }
  575. if(flag & RFNOTEG)
  576. m->externup->noteid = incref(&noteidalloc);
  577. if(flag & (RFPREPAGE|RFCPREPAGE)){
  578. m->externup->prepagemem = flag&RFPREPAGE;
  579. nixprepage(-1);
  580. }
  581. if(flag & RFCORE){
  582. m->externup->ac = getac(m->externup, -1);
  583. m->externup->procctl = Proc_toac;
  584. }else if(flag & RFCCORE){
  585. if(m->externup->ac != nil)
  586. m->externup->procctl = Proc_totc;
  587. }
  588. ar0->i = 0;
  589. return;
  590. }
  591. p = newproc();
  592. if(flag & RFCORE){
  593. if(!waserror()){
  594. p->ac = getac(p, -1);
  595. p->procctl = Proc_toac;
  596. poperror();
  597. }else{
  598. print("warning: rfork: no available ac for the child, it runs in the tc\n");
  599. p->procctl = 0;
  600. }
  601. }
  602. if(m->externup->trace)
  603. p->trace = 1;
  604. p->scallnr = m->externup->scallnr;
  605. memmove(p->arg, m->externup->arg, sizeof(m->externup->arg));
  606. p->nerrlab = 0;
  607. p->slash = m->externup->slash;
  608. p->dot = m->externup->dot;
  609. incref(p->dot);
  610. memmove(p->note, m->externup->note, sizeof(p->note));
  611. p->privatemem = m->externup->privatemem;
  612. p->noswap = m->externup->noswap;
  613. p->nnote = m->externup->nnote;
  614. p->notified = 0;
  615. p->lastnote = m->externup->lastnote;
  616. p->notify = m->externup->notify;
  617. p->ureg = m->externup->ureg;
  618. p->prepagemem = m->externup->prepagemem;
  619. p->dbgreg = 0;
  620. /* Make a new set of memory segments */
  621. n = flag & RFMEM;
  622. qlock(&p->seglock);
  623. if(waserror()){
  624. qunlock(&p->seglock);
  625. nexterror();
  626. }
  627. for(i = 0; i < NSEG; i++)
  628. if(m->externup->seg[i])
  629. p->seg[i] = dupseg(m->externup->seg, i, n);
  630. qunlock(&p->seglock);
  631. poperror();
  632. /* File descriptors */
  633. if(flag & (RFFDG|RFCFDG)) {
  634. if(flag & RFFDG)
  635. p->fgrp = dupfgrp(m->externup->fgrp);
  636. else
  637. p->fgrp = dupfgrp(nil);
  638. }
  639. else {
  640. p->fgrp = m->externup->fgrp;
  641. incref(p->fgrp);
  642. }
  643. /* Process groups */
  644. if(flag & (RFNAMEG|RFCNAMEG)) {
  645. p->pgrp = newpgrp();
  646. if(flag & RFNAMEG)
  647. pgrpcpy(p->pgrp, m->externup->pgrp);
  648. /* inherit noattach */
  649. p->pgrp->noattach = m->externup->pgrp->noattach;
  650. }
  651. else {
  652. p->pgrp = m->externup->pgrp;
  653. incref(p->pgrp);
  654. }
  655. if(flag & RFNOMNT)
  656. m->externup->pgrp->noattach = 1;
  657. if(flag & RFREND)
  658. p->rgrp = newrgrp();
  659. else {
  660. incref(m->externup->rgrp);
  661. p->rgrp = m->externup->rgrp;
  662. }
  663. /* Environment group */
  664. if(flag & (RFENVG|RFCENVG)) {
  665. p->egrp = smalloc(sizeof(Egrp));
  666. p->egrp->ref = 1;
  667. if(flag & RFENVG)
  668. envcpy(p->egrp, m->externup->egrp);
  669. }
  670. else {
  671. p->egrp = m->externup->egrp;
  672. incref(p->egrp);
  673. }
  674. p->hang = m->externup->hang;
  675. p->procmode = m->externup->procmode;
  676. /* Craft a return frame which will cause the child to pop out of
  677. * the scheduler in user mode with the return register zero
  678. */
  679. sysrforkchild(p, m->externup);
  680. p->parent = m->externup;
  681. p->parentpid = m->externup->pid;
  682. if(flag&RFNOWAIT)
  683. p->parentpid = 0;
  684. else {
  685. lock(&m->externup->exl);
  686. m->externup->nchild++;
  687. unlock(&m->externup->exl);
  688. }
  689. if((flag&RFNOTEG) == 0)
  690. p->noteid = m->externup->noteid;
  691. pid = p->pid;
  692. memset(p->time, 0, sizeof(p->time));
  693. p->time[TReal] = sys->ticks;
  694. if(flag & (RFPREPAGE|RFCPREPAGE)){
  695. p->prepagemem = flag&RFPREPAGE;
  696. /*
  697. * BUG: this is prepaging our memory, not
  698. * that of the child, but at least we
  699. * will do the copy on write.
  700. */
  701. nixprepage(-1);
  702. }
  703. kstrdup(&p->text, m->externup->text);
  704. kstrdup(&p->user, m->externup->user);
  705. /*
  706. * since the bss/data segments are now shareable,
  707. * any mmu info about this process is now stale
  708. * (i.e. has bad properties) and has to be discarded.
  709. */
  710. mmuflush();
  711. p->basepri = m->externup->basepri;
  712. p->priority = m->externup->basepri;
  713. p->fixedpri = m->externup->fixedpri;
  714. p->mp = m->externup->mp;
  715. wm = m->externup->wired;
  716. if(wm)
  717. procwired(p, wm->machno);
  718. p->color = m->externup->color;
  719. ready(p);
  720. sched();
  721. ar0->i = pid;
  722. }
  723. #if 0
  724. static uint64_t
  725. vl2be(uint64_t v)
  726. {
  727. uint8_t *p;
  728. p = (uint8_t*)&v;
  729. return ((uint64_t)((p[0]<<24)|(p[1]<<16)|(p[2]<<8)|p[3])<<32)
  730. |((uint64_t)(p[4]<<24)|(p[5]<<16)|(p[6]<<8)|p[7]);
  731. }
  732. static uint32_t
  733. l2be(int32_t l)
  734. {
  735. uint8_t *cp;
  736. cp = (uint8_t*)&l;
  737. return (cp[0]<<24) | (cp[1]<<16) | (cp[2]<<8) | cp[3];
  738. }
  739. #endif
  740. typedef struct {
  741. Exec;
  742. uint64_t hdr[1];
  743. } Hdr;
  744. /*
  745. * flags can ONLY specify that you want an AC for you, or
  746. * that you want an XC for you.
  747. *
  748. */
  749. static void
  750. execac(Ar0* ar0, int flags, char *ufile, char **argv)
  751. {
  752. ExecHdr d;
  753. Fhdr f;
  754. Mach *m = machp();
  755. Hdr hdr;
  756. Fgrp *fg;
  757. Tos *tos;
  758. Chan *chan, *ichan;
  759. Image *img;
  760. Segment *s;
  761. int argc, i, n;
  762. char *a, *elem, *file, *p;
  763. char line[sizeof(Exec)], *progarg[sizeof(Exec)/2+1];
  764. int32_t hdrsz, textsz, datasz, bsssz;
  765. uintptr_t textlim, datalim, bsslim, entry, stack;
  766. uintptr_t textaddr; //, dataaddr;
  767. // static int colorgen;
  768. file = nil;
  769. elem = nil;
  770. switch(flags){
  771. case EXTC:
  772. case EXXC:
  773. break;
  774. case EXAC:
  775. m->externup->ac = getac(m->externup, -1);
  776. break;
  777. default:
  778. error("unknown execac flag");
  779. }
  780. if(waserror()){
  781. DBG("execac: failing: %s\n", m->externup->errstr);
  782. free(file);
  783. free(elem);
  784. if(flags == EXAC && m->externup->ac != nil)
  785. m->externup->ac->proc = nil;
  786. m->externup->ac = nil;
  787. nexterror();
  788. }
  789. /*
  790. * Open the file, remembering the final element and the full name.
  791. */
  792. argc = 0;
  793. file = validnamedup(ufile, 1);
  794. DBG("execac: up %#p file %s\n", m->externup, file);
  795. if(m->externup->trace)
  796. proctracepid(m->externup);
  797. ichan = namec(file, Aopen, OEXEC, 0);
  798. if(waserror()){
  799. iprint("ERROR ON OPEN\n");
  800. cclose(ichan);
  801. nexterror();
  802. }
  803. kstrdup(&elem, m->externup->genbuf);
  804. /*
  805. * Read the header.
  806. * If it's a #!, fill in progarg[] with info then read a new header
  807. * from the file indicated by the #!.
  808. * The #! line must be less than sizeof(Exec) in size,
  809. * including the terminating \n.
  810. */
  811. hdrsz = ichan->dev->read(ichan, &hdr, sizeof(Hdr), 0);
  812. if(hdrsz < 2)
  813. error(Ebadexec);
  814. p = (char*)&hdr;
  815. if(p[0] == '#' && p[1] == '!'){
  816. p = memccpy(line, (char*)&hdr, '\n',
  817. MIN(sizeof(Exec), hdrsz));
  818. if(p == nil)
  819. error(Ebadexec);
  820. *(p-1) = '\0';
  821. argc = tokenize(line+2, progarg, nelem(progarg));
  822. if(argc == 0)
  823. error(Ebadexec);
  824. /* The original file becomes an extra arg after #! line */
  825. progarg[argc++] = file;
  826. /*
  827. * Take the #! $0 as a file to open, and replace
  828. * $0 with the original path's name.
  829. */
  830. p = progarg[0];
  831. progarg[0] = elem;
  832. chan = nil; /* in case namec errors out */
  833. USED(chan);
  834. chan = namec(p, Aopen, OEXEC, 0);
  835. hdrsz = chan->dev->read(chan, &hdr, sizeof(Hdr), 0);
  836. if(hdrsz < 2)
  837. error(Ebadexec);
  838. }else{
  839. chan = ichan;
  840. incref(ichan);
  841. }
  842. /* chan is the chan to use, initial or not. ichan is irrelevant now */
  843. cclose(ichan);
  844. poperror();
  845. /* start over. */
  846. chanseek(ar0, chan, 0, 0);
  847. /*
  848. * #! has had its chance, now we need a real binary.
  849. */
  850. crackhdr(ar0, chan, &f, &d);
  851. textsz = f.txtsz;
  852. datasz =f.datsz;
  853. bsssz = f.bsssz;
  854. entry = f.entry;
  855. textaddr = f.txtaddr;
  856. //dataaddr = f.dataaddr;
  857. textlim = UTROUND(textaddr+hdrsz+textsz);
  858. datalim = BIGPGROUND(textlim+datasz);
  859. bsslim = BIGPGROUND(textlim+datasz+bsssz);
  860. /*
  861. * Check the binary header for consistency,
  862. * e.g. the entry point is within the text segment and
  863. * the segments don't overlap each other.
  864. */
  865. if(entry < textaddr+hdrsz || entry >= textaddr+hdrsz+textsz)
  866. error(Ebadexec);
  867. if(textsz >= textlim || datasz > datalim || bsssz > bsslim
  868. || textlim >= USTKTOP || datalim >= USTKTOP || bsslim >= USTKTOP
  869. || datalim < textlim || bsslim < datalim)
  870. error(Ebadexec);
  871. if(m->externup->ac != nil && m->externup->ac != m)
  872. m->externup->color = corecolor(m->externup->ac->machno);
  873. else
  874. m->externup->color = corecolor(m->machno);
  875. /*
  876. * The new stack is created in ESEG, temporarily mapped elsewhere.
  877. * The stack contains, in descending address order:
  878. * a structure containing housekeeping and profiling data (Tos);
  879. * argument strings;
  880. * array of vectors to the argument strings with a terminating
  881. * nil (argv).
  882. * When the exec is committed, this temporary stack in ESEG will
  883. * become SSEG.
  884. * The architecture-dependent code which jumps to the new image
  885. * will also push a count of the argument array onto the stack (argc).
  886. */
  887. qlock(&m->externup->seglock);
  888. if(waserror()){
  889. if(m->externup->seg[ESEG] != nil){
  890. putseg(m->externup->seg[ESEG]);
  891. m->externup->seg[ESEG] = nil;
  892. }
  893. qunlock(&m->externup->seglock);
  894. nexterror();
  895. }
  896. m->externup->seg[ESEG] = newseg(SG_STACK, TSTKTOP-USTKSIZE, USTKSIZE/BIGPGSZ);
  897. m->externup->seg[ESEG]->color = m->externup->color;
  898. /*
  899. * Stack is a pointer into the temporary stack
  900. * segment, and will move as items are pushed.
  901. */
  902. stack = TSTKTOP-sizeof(Tos);
  903. /*
  904. * First, the top-of-stack structure.
  905. */
  906. tos = (Tos*)stack;
  907. tos->cyclefreq = m->cyclefreq;
  908. cycles((uint64_t*)&tos->pcycles);
  909. tos->pcycles = -tos->pcycles;
  910. tos->kcycles = tos->pcycles;
  911. tos->clock = 0;
  912. /*
  913. * Next push any arguments found from a #! header.
  914. */
  915. for(i = 0; i < argc; i++){
  916. n = strlen(progarg[i])+1;
  917. stack -= n;
  918. memmove(UINT2PTR(stack), progarg[i], n);
  919. }
  920. /*
  921. * Copy the strings pointed to by the syscall argument argv into
  922. * the temporary stack segment, being careful to check
  923. * the strings argv points to are valid.
  924. */
  925. for(i = 0;; i++, argv++){
  926. a = *(char**)validaddr(argv, sizeof(char**), 0);
  927. if(a == nil)
  928. break;
  929. a = validaddr(a, 1, 0);
  930. n = ((char*)vmemchr(a, 0, 0x7fffffff) - a) + 1;
  931. /*
  932. * This futzing is so argv[0] gets validated even
  933. * though it will be thrown away if this is a shell
  934. * script.
  935. */
  936. if(argc > 0 && i == 0)
  937. continue;
  938. /*
  939. * Before copying the string into the temporary stack,
  940. * which might involve a demand-page, check the string
  941. * will not overflow the bottom of the stack.
  942. */
  943. stack -= n;
  944. if(stack < TSTKTOP-USTKSIZE)
  945. error(Enovmem);
  946. p = UINT2PTR(stack);
  947. memmove(p, a, n);
  948. p[n-1] = 0;
  949. argc++;
  950. }
  951. if(argc < 1)
  952. error(Ebadexec);
  953. /*
  954. * Before pushing the argument pointers onto the temporary stack,
  955. * which might involve a demand-page, check there is room for the
  956. * terminating nil pointer, plus pointers, plus some slop for however
  957. * argc might be passed on the stack by sysexecregs (give a page
  958. * of slop, it is an overestimate, but why not).
  959. * Sysexecstack does any architecture-dependent stack alignment.
  960. * Keep a copy of the start of the argument strings before alignment
  961. * so m->externup->args can be created later.
  962. * Although the argument vectors are being pushed onto the stack in
  963. * the temporary segment, the values must be adjusted to reflect
  964. * the segment address after it replaces the current SSEG.
  965. */
  966. a = p = UINT2PTR(stack);
  967. stack = sysexecstack(stack, argc);
  968. if(stack-(argc+1)*sizeof(char**)-BIGPGSZ < TSTKTOP-USTKSIZE) {
  969. //iprint("stck too small?\n");
  970. error(Ebadexec);
  971. }
  972. argv = (char**)stack;
  973. *--argv = nil;
  974. for(i = 0; i < argc; i++){
  975. *--argv = p + (USTKTOP-TSTKTOP);
  976. p += strlen(p) + 1;
  977. }
  978. /*
  979. * Make a good faith copy of the args in m->externup->args using the strings
  980. * in the temporary stack segment. The length must be > 0 as it
  981. * includes the \0 on the last argument and argc was checked earlier
  982. * to be > 0. After the memmove, compensate for any UTF character
  983. * boundary before placing the terminating \0.
  984. */
  985. n = p - a;
  986. if(n <= 0)
  987. error(Egreg);
  988. if(n > 128)
  989. n = 128;
  990. p = smalloc(n);
  991. if(waserror()){
  992. free(p);
  993. nexterror();
  994. }
  995. memmove(p, a, n);
  996. while(n > 0 && (p[n-1] & 0xc0) == 0x80)
  997. n--;
  998. p[n-1] = '\0';
  999. /*
  1000. * All the argument processing is now done, ready to commit.
  1001. */
  1002. free(m->externup->text);
  1003. m->externup->text = elem;
  1004. elem = nil;
  1005. free(m->externup->args);
  1006. m->externup->args = p;
  1007. m->externup->nargs = n;
  1008. poperror(); /* p (m->externup->args) */
  1009. /*
  1010. * Close on exec
  1011. */
  1012. fg = m->externup->fgrp;
  1013. for(i=0; i<=fg->maxfd; i++)
  1014. fdclose(i, CCEXEC);
  1015. /*
  1016. * Free old memory.
  1017. * Special segments maintained across exec.
  1018. */
  1019. for(i = SSEG; i <= HSEG; i++) {
  1020. putseg(m->externup->seg[i]);
  1021. m->externup->seg[i] = nil; /* in case of error */
  1022. }
  1023. for(i = HSEG+1; i< NSEG; i++) {
  1024. s = m->externup->seg[i];
  1025. if(s && (s->type&SG_CEXEC)) {
  1026. putseg(s);
  1027. m->externup->seg[i] = nil;
  1028. }
  1029. }
  1030. /* Text. Shared. Attaches to cache image if possible
  1031. * but prepaged if EXAC
  1032. */
  1033. // TODO: Just use the program header instead of these other things.
  1034. img = attachimage(SG_TEXT|SG_RONLY, chan, m->externup->color, textaddr, (textlim-textaddr)/BIGPGSZ);
  1035. s = img->s;
  1036. s->ph = d.e.ph[f.it];
  1037. m->externup->seg[TSEG] = s;
  1038. s->flushme = 1;
  1039. if(img->color != m->externup->color){
  1040. m->externup->color = img->color;
  1041. }
  1042. unlock(img);
  1043. /* Data. Shared. */
  1044. s = newseg(SG_DATA, textlim, (datalim-textlim)/BIGPGSZ);
  1045. m->externup->seg[DSEG] = s;
  1046. s->color = m->externup->color;
  1047. /* Attached by hand */
  1048. incref(img);
  1049. s->image = img;
  1050. s->ph = d.e.ph[f.id];
  1051. /* BSS. Zero fill on demand for TS */
  1052. m->externup->seg[BSEG] = newseg(SG_BSS, datalim, (bsslim-datalim)/BIGPGSZ);
  1053. m->externup->seg[BSEG]->color= m->externup->color;
  1054. /*
  1055. * Move the stack
  1056. */
  1057. s = m->externup->seg[ESEG];
  1058. m->externup->seg[ESEG] = nil;
  1059. /* MMAP region. Put it at 512GiB for now. */
  1060. m->externup->seg[ESEG] = newseg(SG_MMAP, 512 * GiB, 1);
  1061. print("mmap ESEG is %p\n", m->externup->seg);
  1062. m->externup->seg[ESEG]->color= m->externup->color;
  1063. m->externup->seg[SSEG] = s;
  1064. /* the color of the stack was decided when we created it before,
  1065. * it may have nothing to do with the color of other segments.
  1066. */
  1067. qunlock(&m->externup->seglock);
  1068. poperror(); /* seglock */
  1069. s->base = USTKTOP-USTKSIZE;
  1070. s->top = USTKTOP;
  1071. relocateseg(s, USTKTOP-TSTKTOP);
  1072. /*
  1073. * '/' processes are higher priority.
  1074. */
  1075. if(chan->dev->dc == L'/')
  1076. m->externup->basepri = PriRoot;
  1077. m->externup->priority = m->externup->basepri;
  1078. poperror(); /* chan, elem, file */
  1079. cclose(chan);
  1080. free(file);
  1081. /*
  1082. * At this point, the mmu contains info about the old address
  1083. * space and needs to be flushed
  1084. */
  1085. mmuflush();
  1086. if(m->externup->prepagemem || flags == EXAC)
  1087. nixprepage(-1);
  1088. qlock(&m->externup->debug);
  1089. m->externup->nnote = 0;
  1090. m->externup->notify = 0;
  1091. m->externup->notified = 0;
  1092. m->externup->privatemem = 0;
  1093. sysprocsetup(m->externup);
  1094. qunlock(&m->externup->debug);
  1095. if(m->externup->hang)
  1096. m->externup->procctl = Proc_stopme;
  1097. ar0->v = sysexecregs(entry, TSTKTOP - PTR2UINT(argv), argv + (USTKTOP-TSTKTOP)/sizeof(void *), argc, ((void *)tos) + (USTKTOP-TSTKTOP)/sizeof(void *));
  1098. if(flags == EXAC){
  1099. m->externup->procctl = Proc_toac;
  1100. m->externup->prepagemem = 1;
  1101. }
  1102. DBG("execac up %#p done\n"
  1103. "textsz %lx datasz %lx bsssz %lx hdrsz %lx\n"
  1104. "textlim %ullx datalim %ullx bsslim %ullx\n", m->externup,
  1105. textsz, datasz, bsssz, hdrsz, textlim, datalim, bsslim);
  1106. }
  1107. void
  1108. sysexecac(Ar0* ar0, ...)
  1109. {
  1110. int flags;
  1111. char *file, **argv;
  1112. va_list list;
  1113. va_start(list, ar0);
  1114. /*
  1115. * void* execac(int flags, char* name, char* argv[]);
  1116. */
  1117. flags = va_arg(list, unsigned int);
  1118. file = va_arg(list, char*);
  1119. file = validaddr(file, 1, 0);
  1120. argv = va_arg(list, char**);
  1121. va_end(list);
  1122. evenaddr(PTR2UINT(argv));
  1123. execac(ar0, flags, file, argv);
  1124. }
  1125. static int
  1126. crackhdr(Ar0 *ar0, Chan *c, Fhdr *fp, ExecHdr *d)
  1127. {
  1128. ExecTable *mp;
  1129. int nb, ret;
  1130. uint32_t magic;
  1131. fp->type = 0; /* FNONE */
  1132. nb = c->dev->read(c, (char *)&d->e, sizeof(d->e), c->offset);
  1133. if (nb <= 0)
  1134. error("crackhdr: header read failed");
  1135. ret = 0;
  1136. magic = beswal(d->e.magic); /* big-endian */
  1137. for (mp = exectab; mp->magic; mp++) {
  1138. if (nb < mp->hsize) {
  1139. continue;
  1140. }
  1141. /*
  1142. * The magic number has morphed into something
  1143. * with fields (the straw was DYN_MAGIC) so now
  1144. * a flag is needed in Fhdr to distinguish _MAGIC()
  1145. * magic numbers from foreign magic numbers.
  1146. *
  1147. * This code is creaking a bit and if it has to
  1148. * be modified/extended much more it's probably
  1149. * time to step back and redo it all.
  1150. */
  1151. if(mp->_magic){
  1152. if(mp->magic != (magic & ~DYN_MAGIC))
  1153. continue;
  1154. if ((magic & DYN_MAGIC) && mp->dlmname != nil)
  1155. fp->name = mp->dlmname;
  1156. else
  1157. fp->name = mp->name;
  1158. }
  1159. else{
  1160. if(mp->magic != magic)
  1161. continue;
  1162. fp->name = mp->name;
  1163. }
  1164. fp->type = mp->type;
  1165. fp->hdrsz = mp->hsize; /* will be zero on bootables */
  1166. fp->_magic = mp->_magic;
  1167. fp->magic = magic;
  1168. machkind = mp->elfmach;
  1169. if(mp->swal != nil)
  1170. hswal(d, sizeof(d->e)/sizeof(uint32_t), mp->swal);
  1171. ret = mp->hparse(ar0, c, fp, d);
  1172. chanseek(ar0, c, mp->hsize, 0); /* seek to end of header */
  1173. break;
  1174. }
  1175. if(mp->magic == 0) {
  1176. error("Sysproc: unknown header type");
  1177. }
  1178. return ret;
  1179. }
  1180. void
  1181. sysexec(Ar0* ar0, ...)
  1182. {
  1183. char *file, **argv;
  1184. va_list list;
  1185. va_start(list, ar0);
  1186. /*
  1187. * void* exec(char* name, char* argv[]);
  1188. */
  1189. file = va_arg(list, char*);
  1190. file = validaddr(file, 1, 0);
  1191. argv = va_arg(list, char**);
  1192. va_end(list);
  1193. evenaddr(PTR2UINT(argv));
  1194. execac(ar0, EXTC, file, argv);
  1195. }
  1196. void
  1197. sysr1(Ar0* ar, ...)
  1198. {
  1199. print("sysr1() called. recompile your binary\n");
  1200. }
  1201. void
  1202. sysnixsyscall(Ar0* ar, ...)
  1203. {
  1204. print("nixsyscall() called. recompile your binary\n");
  1205. }
  1206. int
  1207. return0(void* v)
  1208. {
  1209. return 0;
  1210. }
  1211. void
  1212. syssleep(Ar0* ar0, ...)
  1213. {
  1214. Mach *m = machp();
  1215. int32_t ms;
  1216. va_list list;
  1217. va_start(list, ar0);
  1218. /*
  1219. * int sleep(long millisecs);
  1220. */
  1221. ms = va_arg(list, int32_t);
  1222. va_end(list);
  1223. ar0->i = 0;
  1224. if(ms <= 0) {
  1225. if (m->externup->edf && (m->externup->edf->flags & Admitted))
  1226. edfyield();
  1227. else
  1228. yield();
  1229. return;
  1230. }
  1231. if(ms < TK2MS(1))
  1232. ms = TK2MS(1);
  1233. tsleep(&m->externup->sleep, return0, 0, ms);
  1234. }
  1235. void
  1236. sysalarm(Ar0* ar0, ...)
  1237. {
  1238. unsigned long ms;
  1239. va_list list;
  1240. va_start(list, ar0);
  1241. /*
  1242. * long alarm(unsigned long millisecs);
  1243. * Odd argument type...
  1244. */
  1245. ms = va_arg(list, unsigned long);
  1246. va_end(list);
  1247. ar0->l = procalarm(ms);
  1248. }
  1249. void
  1250. sysexits(Ar0* ar0, ...)
  1251. {
  1252. Mach *m = machp();
  1253. char *status;
  1254. char *inval = "invalid exit string";
  1255. char buf[ERRMAX];
  1256. va_list list;
  1257. va_start(list, ar0);
  1258. /*
  1259. * void exits(char *msg);
  1260. */
  1261. status = va_arg(list, char*);
  1262. va_end(list);
  1263. if(status){
  1264. if(waserror())
  1265. status = inval;
  1266. else{
  1267. status = validaddr(status, 1, 0);
  1268. if(vmemchr(status, 0, ERRMAX) == 0){
  1269. memmove(buf, status, ERRMAX);
  1270. buf[ERRMAX-1] = 0;
  1271. status = buf;
  1272. }
  1273. poperror();
  1274. }
  1275. }
  1276. pexit(status, 1);
  1277. }
  1278. void
  1279. sys_wait(Ar0* ar0, ...)
  1280. {
  1281. int pid;
  1282. Waitmsg w;
  1283. OWaitmsg *ow;
  1284. va_list list;
  1285. va_start(list, ar0);
  1286. /*
  1287. * int wait(Waitmsg* w);
  1288. *
  1289. * Deprecated; backwards compatibility only.
  1290. */
  1291. ow = va_arg(list, OWaitmsg*);
  1292. va_end(list);
  1293. if(ow == nil){
  1294. ar0->i = pwait(nil);
  1295. return;
  1296. }
  1297. ow = validaddr(ow, sizeof(OWaitmsg), 1);
  1298. evenaddr(PTR2UINT(ow));
  1299. pid = pwait(&w);
  1300. if(pid >= 0){
  1301. readnum(0, ow->pid, NUMSIZE, w.pid, NUMSIZE);
  1302. readnum(0, ow->time+TUser*NUMSIZE, NUMSIZE, w.time[TUser], NUMSIZE);
  1303. readnum(0, ow->time+TSys*NUMSIZE, NUMSIZE, w.time[TSys], NUMSIZE);
  1304. readnum(0, ow->time+TReal*NUMSIZE, NUMSIZE, w.time[TReal], NUMSIZE);
  1305. strncpy(ow->msg, w.msg, sizeof(ow->msg));
  1306. ow->msg[sizeof(ow->msg)-1] = '\0';
  1307. }
  1308. ar0->i = pid;
  1309. }
  1310. void
  1311. sysawait(Ar0* ar0, ...)
  1312. {
  1313. int i;
  1314. int pid;
  1315. Waitmsg w;
  1316. usize n;
  1317. char *p;
  1318. va_list list;
  1319. va_start(list, ar0);
  1320. /*
  1321. * int await(char* s, int n);
  1322. * should really be
  1323. * usize await(char* s, usize n);
  1324. */
  1325. p = va_arg(list, char*);
  1326. n = va_arg(list, int32_t);
  1327. va_end(list);
  1328. p = validaddr(p, n, 1);
  1329. pid = pwait(&w);
  1330. if(pid < 0){
  1331. ar0->i = -1;
  1332. return;
  1333. }
  1334. i = snprint(p, n, "%d %lud %lud %lud %q",
  1335. w.pid,
  1336. w.time[TUser], w.time[TSys], w.time[TReal],
  1337. w.msg);
  1338. ar0->i = i;
  1339. }
  1340. void
  1341. werrstr(char *fmt, ...)
  1342. {
  1343. Mach *m = machp();
  1344. va_list va;
  1345. if(m->externup == nil)
  1346. return;
  1347. va_start(va, fmt);
  1348. vseprint(m->externup->syserrstr, m->externup->syserrstr+ERRMAX, fmt, va);
  1349. va_end(va);
  1350. }
  1351. static void
  1352. generrstr(char *buf, int32_t n)
  1353. {
  1354. Mach *m = machp();
  1355. char *p, tmp[ERRMAX];
  1356. if(n <= 0)
  1357. error(Ebadarg);
  1358. p = validaddr(buf, n, 1);
  1359. if(n > sizeof tmp)
  1360. n = sizeof tmp;
  1361. memmove(tmp, p, n);
  1362. /* make sure it's NUL-terminated */
  1363. tmp[n-1] = '\0';
  1364. memmove(p, m->externup->syserrstr, n);
  1365. p[n-1] = '\0';
  1366. memmove(m->externup->syserrstr, tmp, n);
  1367. }
  1368. void
  1369. syserrstr(Ar0* ar0, ...)
  1370. {
  1371. char *err;
  1372. usize nerr;
  1373. va_list list;
  1374. va_start(list, ar0);
  1375. /*
  1376. * int errstr(char* err, uint nerr);
  1377. * should really be
  1378. * usize errstr(char* err, usize nerr);
  1379. * but errstr always returns 0.
  1380. */
  1381. err = va_arg(list, char*);
  1382. nerr = va_arg(list, usize);
  1383. va_end(list);
  1384. generrstr(err, nerr);
  1385. ar0->i = 0;
  1386. }
  1387. void
  1388. sys_errstr(Ar0* ar0, ...)
  1389. {
  1390. char *p;
  1391. va_list list;
  1392. va_start(list, ar0);
  1393. /*
  1394. * int errstr(char* err);
  1395. *
  1396. * Deprecated; backwards compatibility only.
  1397. */
  1398. p = va_arg(list, char*);
  1399. va_end(list);
  1400. generrstr(p, 64);
  1401. ar0->i = 0;
  1402. }
  1403. void
  1404. sysnotify(Ar0* ar0, ...)
  1405. {
  1406. Mach *m = machp();
  1407. void (*f)(void*, char*);
  1408. va_list list;
  1409. va_start(list, ar0);
  1410. /*
  1411. * int notify(void (*f)(void*, char*));
  1412. */
  1413. f = (void (*)(void*, char*))va_arg(list, void*);
  1414. va_end(list);
  1415. if(f != nil)
  1416. validaddr(f, sizeof(void (*)(void*, char*)), 0);
  1417. m->externup->notify = f;
  1418. ar0->i = 0;
  1419. }
  1420. void
  1421. sysnoted(Ar0* ar0, ...)
  1422. {
  1423. Mach *m = machp();
  1424. int v;
  1425. va_list list;
  1426. va_start(list, ar0);
  1427. /*
  1428. * int noted(int v);
  1429. */
  1430. v = va_arg(list, int);
  1431. va_end(list);
  1432. if(v != NRSTR && !m->externup->notified)
  1433. error(Egreg);
  1434. ar0->i = 0;
  1435. }
  1436. void
  1437. sysrendezvous(Ar0* ar0, ...)
  1438. {
  1439. Mach *m = machp();
  1440. Proc *p, **l;
  1441. uintptr_t tag, val;
  1442. va_list list;
  1443. va_start(list, ar0);
  1444. /*
  1445. * void* rendezvous(void*, void*);
  1446. */
  1447. tag = PTR2UINT(va_arg(list, void*));
  1448. l = &REND(m->externup->rgrp, tag);
  1449. m->externup->rendval = ~0;
  1450. lock(m->externup->rgrp);
  1451. for(p = *l; p; p = p->rendhash) {
  1452. if(p->rendtag == tag) {
  1453. *l = p->rendhash;
  1454. val = p->rendval;
  1455. p->rendval = PTR2UINT(va_arg(list, void*));
  1456. while(p->mach != 0)
  1457. ;
  1458. ready(p);
  1459. unlock(m->externup->rgrp);
  1460. ar0->v = UINT2PTR(val);
  1461. return;
  1462. }
  1463. l = &p->rendhash;
  1464. }
  1465. /* Going to sleep here */
  1466. m->externup->rendtag = tag;
  1467. m->externup->rendval = PTR2UINT(va_arg(list, void*));
  1468. va_end(list);
  1469. m->externup->rendhash = *l;
  1470. *l = m->externup;
  1471. m->externup->state = Rendezvous;
  1472. if(m->externup->trace)
  1473. proctrace(m->externup, SLock, 0);
  1474. unlock(m->externup->rgrp);
  1475. sched();
  1476. ar0->v = UINT2PTR(m->externup->rendval);
  1477. }
  1478. /*
  1479. * The implementation of semaphores is complicated by needing
  1480. * to avoid rescheduling in syssemrelease, so that it is safe
  1481. * to call from real-time processes. This means syssemrelease
  1482. * cannot acquire any qlocks, only spin locks.
  1483. *
  1484. * Semacquire and semrelease must both manipulate the semaphore
  1485. * wait list. Lock-free linked lists only exist in theory, not
  1486. * in practice, so the wait list is protected by a spin lock.
  1487. *
  1488. * The semaphore value *addr is stored in user memory, so it
  1489. * cannot be read or written while holding spin locks.
  1490. *
  1491. * Thus, we can access the list only when holding the lock, and
  1492. * we can access the semaphore only when not holding the lock.
  1493. * This makes things interesting. Note that sleep's condition function
  1494. * is called while holding two locks - r and m->externup->rlock - so it cannot
  1495. * access the semaphore value either.
  1496. *
  1497. * An acquirer announces its intention to try for the semaphore
  1498. * by putting a Sema structure onto the wait list and then
  1499. * setting Sema.waiting. After one last check of semaphore,
  1500. * the acquirer sleeps until Sema.waiting==0. A releaser of n
  1501. * must wake up n acquirers who have Sema.waiting set. It does
  1502. * this by clearing Sema.waiting and then calling wakeup.
  1503. *
  1504. * There are three interesting races here.
  1505. * The first is that in this particular sleep/wakeup usage, a single
  1506. * wakeup can rouse a process from two consecutive sleeps!
  1507. * The ordering is:
  1508. *
  1509. * (a) set Sema.waiting = 1
  1510. * (a) call sleep
  1511. * (b) set Sema.waiting = 0
  1512. * (a) check Sema.waiting inside sleep, return w/o sleeping
  1513. * (a) try for semaphore, fail
  1514. * (a) set Sema.waiting = 1
  1515. * (a) call sleep
  1516. * (b) call wakeup(a)
  1517. * (a) wake up again
  1518. *
  1519. * This is okay - semacquire will just go around the loop
  1520. * again. It does mean that at the top of the for(;;) loop in
  1521. * semacquire, phore.waiting might already be set to 1.
  1522. *
  1523. * The second is that a releaser might wake an acquirer who is
  1524. * interrupted before he can acquire the lock. Since
  1525. * release(n) issues only n wakeup calls -- only n can be used
  1526. * anyway -- if the interrupted process is not going to use his
  1527. * wakeup call he must pass it on to another acquirer.
  1528. *
  1529. * The third race is similar to the second but more subtle. An
  1530. * acquirer sets waiting=1 and then does a final canacquire()
  1531. * before going to sleep. The opposite order would result in
  1532. * missing wakeups that happen between canacquire and
  1533. * waiting=1. (In fact, the whole point of Sema.waiting is to
  1534. * avoid missing wakeups between canacquire() and sleep().) But
  1535. * there can be spurious wakeups between a successful
  1536. * canacquire() and the following semdequeue(). This wakeup is
  1537. * not useful to the acquirer, since he has already acquired
  1538. * the semaphore. Like in the previous case, though, the
  1539. * acquirer must pass the wakeup call along.
  1540. *
  1541. * This is all rather subtle. The code below has been verified
  1542. * with the spin model /sys/src/9/port/semaphore.p. The
  1543. * original code anticipated the second race but not the first
  1544. * or third, which were caught only with spin. The first race
  1545. * is mentioned in /sys/doc/sleep.ps, but I'd forgotten about it.
  1546. * It was lucky that my abstract model of sleep/wakeup still managed
  1547. * to preserve that behavior.
  1548. *
  1549. * I remain slightly concerned about memory coherence
  1550. * outside of locks. The spin model does not take
  1551. * queued processor writes into account so we have to
  1552. * think hard. The only variables accessed outside locks
  1553. * are the semaphore value itself and the boolean flag
  1554. * Sema.waiting. The value is only accessed with CAS,
  1555. * whose job description includes doing the right thing as
  1556. * far as memory coherence across processors. That leaves
  1557. * Sema.waiting. To handle it, we call coherence() before each
  1558. * read and after each write. - rsc
  1559. */
  1560. /* Add semaphore p with addr a to list in seg. */
  1561. static void
  1562. semqueue(Segment* s, int* addr, Sema* p)
  1563. {
  1564. memset(p, 0, sizeof *p);
  1565. p->addr = addr;
  1566. lock(&s->sema); /* uses s->sema.Rendez.Lock, but no one else is */
  1567. p->next = &s->sema;
  1568. p->prev = s->sema.prev;
  1569. p->next->prev = p;
  1570. p->prev->next = p;
  1571. unlock(&s->sema);
  1572. }
  1573. /* Remove semaphore p from list in seg. */
  1574. static void
  1575. semdequeue(Segment* s, Sema* p)
  1576. {
  1577. lock(&s->sema);
  1578. p->next->prev = p->prev;
  1579. p->prev->next = p->next;
  1580. unlock(&s->sema);
  1581. }
  1582. /* Wake up n waiters with addr on list in seg. */
  1583. static void
  1584. semwakeup(Segment* s, int* addr, int n)
  1585. {
  1586. Sema *p;
  1587. lock(&s->sema);
  1588. for(p = s->sema.next; p != &s->sema && n > 0; p = p->next){
  1589. if(p->addr == addr && p->waiting){
  1590. p->waiting = 0;
  1591. coherence();
  1592. wakeup(p);
  1593. n--;
  1594. }
  1595. }
  1596. unlock(&s->sema);
  1597. }
  1598. /* Add delta to semaphore and wake up waiters as appropriate. */
  1599. static int
  1600. semrelease(Segment* s, int* addr, int delta)
  1601. {
  1602. int value;
  1603. do
  1604. value = *addr;
  1605. while(!CASW(addr, value, value+delta));
  1606. semwakeup(s, addr, delta);
  1607. return value+delta;
  1608. }
  1609. /* Try to acquire semaphore using compare-and-swap */
  1610. static int
  1611. canacquire(int* addr)
  1612. {
  1613. int value;
  1614. while((value = *addr) > 0){
  1615. if(CASW(addr, value, value-1))
  1616. return 1;
  1617. }
  1618. return 0;
  1619. }
  1620. /* Should we wake up? */
  1621. static int
  1622. semawoke(void* p)
  1623. {
  1624. coherence();
  1625. return !((Sema*)p)->waiting;
  1626. }
  1627. /* Acquire semaphore (subtract 1). */
  1628. static int
  1629. semacquire(Segment* s, int* addr, int block)
  1630. {
  1631. Mach *m = machp();
  1632. int acquired;
  1633. Sema phore;
  1634. if(canacquire(addr))
  1635. return 1;
  1636. if(!block)
  1637. return 0;
  1638. acquired = 0;
  1639. semqueue(s, addr, &phore);
  1640. for(;;){
  1641. phore.waiting = 1;
  1642. coherence();
  1643. if(canacquire(addr)){
  1644. acquired = 1;
  1645. break;
  1646. }
  1647. if(waserror())
  1648. break;
  1649. sleep(&phore, semawoke, &phore);
  1650. poperror();
  1651. }
  1652. semdequeue(s, &phore);
  1653. coherence(); /* not strictly necessary due to lock in semdequeue */
  1654. if(!phore.waiting)
  1655. semwakeup(s, addr, 1);
  1656. if(!acquired)
  1657. nexterror();
  1658. return 1;
  1659. }
  1660. /* Acquire semaphore or time-out */
  1661. static int
  1662. tsemacquire(Segment* s, int* addr, int32_t ms)
  1663. {
  1664. Mach *m = machp();
  1665. int acquired;
  1666. uint32_t t;
  1667. Sema phore;
  1668. if(canacquire(addr))
  1669. return 1;
  1670. if(ms == 0)
  1671. return 0;
  1672. acquired = 0;
  1673. semqueue(s, addr, &phore);
  1674. for(;;){
  1675. phore.waiting = 1;
  1676. coherence();
  1677. if(canacquire(addr)){
  1678. acquired = 1;
  1679. break;
  1680. }
  1681. if(waserror())
  1682. break;
  1683. t = sys->ticks;
  1684. tsleep(&phore, semawoke, &phore, ms);
  1685. ms -= TK2MS(sys->ticks-t);
  1686. poperror();
  1687. if(ms <= 0)
  1688. break;
  1689. }
  1690. semdequeue(s, &phore);
  1691. coherence(); /* not strictly necessary due to lock in semdequeue */
  1692. if(!phore.waiting)
  1693. semwakeup(s, addr, 1);
  1694. if(ms <= 0)
  1695. return 0;
  1696. if(!acquired)
  1697. nexterror();
  1698. return 1;
  1699. }
  1700. void
  1701. syssemacquire(Ar0* ar0, ...)
  1702. {
  1703. Mach *m = machp();
  1704. Segment *s;
  1705. int *addr, block;
  1706. va_list list;
  1707. va_start(list, ar0);
  1708. /*
  1709. * int semacquire(long* addr, int block);
  1710. * should be (and will be implemented below as) perhaps
  1711. * int semacquire(int* addr, int block);
  1712. */
  1713. addr = va_arg(list, int*);
  1714. addr = validaddr(addr, sizeof(int), 1);
  1715. evenaddr(PTR2UINT(addr));
  1716. block = va_arg(list, int);
  1717. va_end(list);
  1718. if((s = seg(m->externup, PTR2UINT(addr), 0)) == nil)
  1719. error(Ebadarg);
  1720. if(*addr < 0)
  1721. error(Ebadarg);
  1722. ar0->i = semacquire(s, addr, block);
  1723. }
  1724. void
  1725. systsemacquire(Ar0* ar0, ...)
  1726. {
  1727. Mach *m = machp();
  1728. Segment *s;
  1729. int *addr, ms;
  1730. va_list list;
  1731. va_start(list, ar0);
  1732. /*
  1733. * int tsemacquire(long* addr, uint32_t ms);
  1734. * should be (and will be implemented below as) perhaps
  1735. * int tsemacquire(int* addr, uint32_t ms);
  1736. */
  1737. addr = va_arg(list, int*);
  1738. addr = validaddr(addr, sizeof(int), 1);
  1739. evenaddr(PTR2UINT(addr));
  1740. ms = va_arg(list, uint32_t);
  1741. va_end(list);
  1742. if((s = seg(m->externup, PTR2UINT(addr), 0)) == nil)
  1743. error(Ebadarg);
  1744. if(*addr < 0)
  1745. error(Ebadarg);
  1746. ar0->i = tsemacquire(s, addr, ms);
  1747. }
  1748. void
  1749. syssemrelease(Ar0* ar0, ...)
  1750. {
  1751. Mach *m = machp();
  1752. Segment *s;
  1753. int *addr, delta;
  1754. va_list list;
  1755. va_start(list, ar0);
  1756. /*
  1757. * long semrelease(long* addr, long count);
  1758. * should be (and will be implemented below as) perhaps
  1759. * int semrelease(int* addr, int count);
  1760. */
  1761. addr = va_arg(list, int*);
  1762. addr = validaddr(addr, sizeof(int), 1);
  1763. evenaddr(PTR2UINT(addr));
  1764. delta = va_arg(list, int);
  1765. va_end(list);
  1766. if((s = seg(m->externup, PTR2UINT(addr), 0)) == nil)
  1767. error(Ebadarg);
  1768. if(delta < 0 || *addr < 0)
  1769. error(Ebadarg);
  1770. ar0->i = semrelease(s, addr, delta);
  1771. }