sysseg.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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 "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. Segment* (*_globalsegattach)(Proc*, char*);
  16. static Lock physseglock;
  17. int
  18. addphysseg(Physseg* new)
  19. {
  20. Physseg *ps;
  21. /*
  22. * Check not already entered and there is room
  23. * for a new entry and the terminating null entry.
  24. */
  25. lock(&physseglock);
  26. for(ps = physseg; ps->name; ps++){
  27. if(strcmp(ps->name, new->name) == 0){
  28. unlock(&physseglock);
  29. return -1;
  30. }
  31. }
  32. if(ps-physseg >= nphysseg-2){
  33. unlock(&physseglock);
  34. return -1;
  35. }
  36. if(new->pgszi < 0)
  37. new->pgszi = getpgszi(2*MiB); /* 2M pages by default */
  38. if(new->pgszi < 0)
  39. panic("addphysseg");
  40. *ps = *new;
  41. unlock(&physseglock);
  42. return 0;
  43. }
  44. int
  45. isphysseg(char *name)
  46. {
  47. int rv;
  48. Physseg *ps;
  49. lock(&physseglock);
  50. rv = 0;
  51. for(ps = physseg; ps->name; ps++){
  52. if(strcmp(ps->name, name) == 0){
  53. rv = 1;
  54. break;
  55. }
  56. }
  57. unlock(&physseglock);
  58. return rv;
  59. }
  60. /* Needs to be non-static for BGP support */
  61. uintptr_t
  62. ibrk(uintptr_t addr, int seg)
  63. {
  64. Proc *up = externup();
  65. Segment *s, *ns;
  66. uintptr_t newtop, rtop;
  67. int32_t newsize;
  68. int i, mapsize;
  69. Pte **map;
  70. uintmem pgsz;
  71. s = up->seg[seg];
  72. if(s == 0)
  73. error(Ebadarg);
  74. if(addr == 0)
  75. return s->top;
  76. qlock(&s->lk);
  77. if(waserror()) {
  78. qunlock(&s->lk);
  79. nexterror();
  80. }
  81. /* We may start with the bss overlapping the data */
  82. if(addr < s->base)
  83. addr = s->base;
  84. pgsz = sys->pgsz[s->pgszi];
  85. newtop = ROUNDUP(addr, pgsz);
  86. newsize = (newtop-s->base)/pgsz;
  87. if(newtop < s->top) {
  88. mfreeseg(s, newtop, (s->top-newtop)/pgsz);
  89. s->top = newtop;
  90. s->size = newsize;
  91. poperror();
  92. qunlock(&s->lk);
  93. mmuflush();
  94. return newtop;
  95. }
  96. if(newsize > (SEGMAPSIZE*s->ptepertab))
  97. error(Enovmem);
  98. for(i = 0; i < NSEG; i++) {
  99. ns = up->seg[i];
  100. if(ns == 0 || ns == s)
  101. continue;
  102. if(newtop >= ns->base && newtop < ns->top)
  103. error(Esoverlap);
  104. }
  105. /*
  106. * TODO(aki): reintroduce 1G page support:
  107. * brk the bss up to the 1G boundary, and create
  108. * a segment placed at that boundary, using 1G pages if it can.
  109. * This is both back compatible, transparent, and permits using 1G pages.
  110. */
  111. rtop = newtop;
  112. mapsize = HOWMANY(newsize, s->ptepertab);
  113. if(mapsize > s->mapsize){
  114. map = smalloc(mapsize*sizeof(Pte*));
  115. memmove(map, s->map, s->mapsize*sizeof(Pte*));
  116. if(s->map != s->ssegmap)
  117. free(s->map);
  118. s->map = map;
  119. s->mapsize = mapsize;
  120. }
  121. s->top = newtop;
  122. s->size = newsize;
  123. poperror();
  124. qunlock(&s->lk);
  125. return rtop;
  126. }
  127. void
  128. syssegbrk(Ar0* ar0, ...)
  129. {
  130. Proc *up = externup();
  131. int i;
  132. uintptr_t addr;
  133. Segment *s;
  134. va_list list;
  135. va_start(list, ar0);
  136. /*
  137. * int segbrk(void*, void*);
  138. * should be
  139. * void* segbrk(void* saddr, void* addr);
  140. */
  141. addr = PTR2UINT(va_arg(list, void*));
  142. if(addr == 0){
  143. for(i = 0; i < NSEG; i++){
  144. if(up->seg[i] != nil && (up->seg[i]->type&SG_TYPE) == SG_BSS){
  145. ar0->v = UINT2PTR(up->seg[i]->top);
  146. return;
  147. }
  148. }
  149. return;
  150. }
  151. for(i = 0; i < NSEG; i++) {
  152. s = up->seg[i];
  153. if(s == nil)
  154. continue;
  155. /* Ok to extend an empty segment */
  156. if(addr < s->base || addr > s->top)
  157. continue;
  158. if(addr == s->top && (s->base < s->top))
  159. continue;
  160. switch(s->type&SG_TYPE) {
  161. case SG_LOAD:
  162. case SG_TEXT:
  163. case SG_DATA:
  164. case SG_STACK:
  165. error(Ebadarg);
  166. default:
  167. addr = PTR2UINT(va_arg(list, void*));
  168. ar0->v = UINT2PTR(ibrk(addr, i));
  169. return;
  170. }
  171. }
  172. va_end(list);
  173. error(Ebadarg);
  174. }
  175. void
  176. sysbrk_(Ar0* ar0, ...)
  177. {
  178. Proc *up = externup();
  179. uintptr_t addr;
  180. va_list list;
  181. va_start(list, ar0);
  182. int i;
  183. /*
  184. * int brk(void*);
  185. *
  186. * Deprecated; should be for backwards compatibility only.
  187. */
  188. addr = PTR2UINT(va_arg(list, void*));
  189. va_end(list);
  190. for(i = 0; i < NSEG; i++){
  191. if(up->seg[i] != nil && (up->seg[i]->type&SG_TYPE) == SG_BSS){
  192. ibrk(addr, i);
  193. ar0->i = 0;
  194. return;
  195. }
  196. }
  197. ar0->i = -1;
  198. }
  199. static uintptr_t
  200. segattach(Proc* p, int attr, char* name, uintptr_t va, usize len)
  201. {
  202. Proc *up = externup();
  203. int sno;
  204. Segment *s, *os;
  205. Physseg *ps;
  206. /* BUG: Only ok for now */
  207. if((va != 0 && va < UTZERO) || (va & KZERO) == KZERO)
  208. error("virtual address in kernel");
  209. vmemchr(name, 0, ~0);
  210. qlock(&p->seglock);
  211. if(waserror()){
  212. qunlock(&p->seglock);
  213. nexterror();
  214. }
  215. for(sno = 0; sno < NSEG; sno++)
  216. if(p->seg[sno] == nil)
  217. break;
  218. if(sno == NSEG)
  219. error("too many segments in process");
  220. /*
  221. * first look for a global segment with the
  222. * same name
  223. */
  224. if(_globalsegattach != nil){
  225. s = (*_globalsegattach)(p, name);
  226. if(s != nil){
  227. p->seg[sno] = s;
  228. if(p == up && up->prepagemem)
  229. nixprepage(sno);
  230. va = s->base;
  231. goto clean_out;
  232. }
  233. }
  234. for(ps = physseg; ps->name != nil; ps++)
  235. if(strcmp(name, ps->name) == 0)
  236. break;
  237. if(ps->name == nil)
  238. error("segment not found");
  239. if(va == 0 && ps->gva != 0){
  240. va = ps->gva;
  241. if(len == 0)
  242. len = ps->size*BIGPGSZ;
  243. }
  244. if(len == 0)
  245. error("zero length");
  246. len = BIGPGROUND(len);
  247. if(len == 0)
  248. error("length overflow");
  249. /*
  250. * Find a hole in the address space.
  251. * Starting at the highest possible stack address - len,
  252. * check for an overlapping segment, and repeat at the
  253. * base of that segment - len until either a hole is found
  254. * or the address space is exhausted.
  255. */
  256. if(va == 0) {
  257. va = USTKTOP-len;
  258. for(;;) {
  259. os = isoverlap(p, va, len);
  260. if(os == nil)
  261. break;
  262. va = os->base;
  263. if(len > va)
  264. error("cannot fit segment at virtual address");
  265. va -= len;
  266. }
  267. }
  268. va = va&~(BIGPGSZ-1);
  269. if(isoverlap(p, va, len) != nil)
  270. error(Esoverlap);
  271. if((len/BIGPGSZ) > ps->size)
  272. error("len > segment size");
  273. attr &= ~SG_TYPE; /* Turn off what is not allowed */
  274. attr |= ps->attr; /* Copy in defaults */
  275. s = newseg(attr, va, len/BIGPGSZ);
  276. s->pseg = ps;
  277. p->seg[sno] = s;
  278. if(p == up && up->prepagemem)
  279. nixprepage(sno);
  280. clean_out:
  281. poperror();
  282. qunlock(&p->seglock);
  283. return va;
  284. }
  285. void
  286. syssegattach(Ar0* ar0, ...)
  287. {
  288. Proc *up = externup();
  289. int attr;
  290. char *name;
  291. uintptr_t va;
  292. usize len;
  293. va_list list;
  294. va_start(list, ar0);
  295. /*
  296. * long segattach(int, char*, void*, uint32_t);
  297. * should be
  298. * void* segattach(int, char*, void*, usize);
  299. */
  300. attr = va_arg(list, int);
  301. name = va_arg(list, char*);
  302. va = PTR2UINT(va_arg(list, void*));
  303. len = va_arg(list, usize);
  304. va_end(list);
  305. ar0->v = UINT2PTR(segattach(up, attr, validaddr(name, 1, 0), va, len));
  306. }
  307. void
  308. syssegdetach(Ar0* ar0, ...)
  309. {
  310. Proc *up = externup();
  311. int i;
  312. uintptr_t addr;
  313. Segment *s;
  314. va_list list;
  315. va_start(list, ar0);
  316. /*
  317. * int segdetach(void*);
  318. */
  319. addr = PTR2UINT(va_arg(list, void*));
  320. va_end(list);
  321. qlock(&up->seglock);
  322. if(waserror()){
  323. qunlock(&up->seglock);
  324. nexterror();
  325. }
  326. s = nil;
  327. for(i = 0; i < NSEG; i++)
  328. if((s = up->seg[i]) != nil){
  329. qlock(&s->lk);
  330. if((addr >= s->base && addr < s->top) ||
  331. (s->top == s->base && addr == s->base))
  332. goto found;
  333. qunlock(&s->lk);
  334. }
  335. error(Ebadarg);
  336. found:
  337. /*
  338. * Can't detach the initial stack segment
  339. * because the clock writes profiling info
  340. * there. So let's not detach any stacks.
  341. * TODO(aki): does it really?
  342. */
  343. if((s->type & SG_TYPE) == SG_STACK){
  344. qunlock(&s->lk);
  345. error(Ebadarg);
  346. }
  347. up->seg[i] = 0;
  348. qunlock(&s->lk);
  349. putseg(s);
  350. qunlock(&up->seglock);
  351. poperror();
  352. /* Ensure we flush any entries from the lost segment */
  353. mmuflush();
  354. ar0->i = 0;
  355. }
  356. void
  357. syssegfree(Ar0* ar0, ...)
  358. {
  359. Proc *up = externup();
  360. Segment *s;
  361. uintptr_t from, to;
  362. usize len;
  363. va_list list;
  364. va_start(list, ar0);
  365. /*
  366. * int segfree(void*, uint32_t);
  367. * should be
  368. * int segfree(void*, usize);
  369. */
  370. from = PTR2UINT(va_arg(list, void*));
  371. s = seg(up, from, 1);
  372. if(s == nil)
  373. error(Ebadarg);
  374. len = va_arg(list, usize);
  375. to = (from + len) & ~(BIGPGSZ-1);
  376. if(to < from || to > s->top){
  377. qunlock(&s->lk);
  378. error(Ebadarg);
  379. }
  380. from = BIGPGROUND(from);
  381. va_end(list);
  382. mfreeseg(s, from, (to - from) / BIGPGSZ);
  383. qunlock(&s->lk);
  384. mmuflush();
  385. ar0->i = 0;
  386. }
  387. static void
  388. pteflush(Pte *pte, int s, int e)
  389. {
  390. int i;
  391. Page *p;
  392. for(i = s; i < e; i++) {
  393. p = pte->pages[i];
  394. if(pagedout(p) == 0)
  395. memset(p->cachectl, PG_TXTFLUSH, sizeof(p->cachectl));
  396. }
  397. }
  398. void
  399. syssegflush(Ar0* ar0, ...)
  400. {
  401. Proc *up = externup();
  402. Segment *s;
  403. uintptr_t addr;
  404. Pte *pte;
  405. usize chunk, l, len, pe, ps;
  406. va_list list;
  407. va_start(list, ar0);
  408. /*
  409. * int segflush(void*, uint32_t);
  410. * should be
  411. * int segflush(void*, usize);
  412. */
  413. addr = PTR2UINT(va_arg(list, void*));
  414. len = va_arg(list, usize);
  415. va_end(list);
  416. while(len > 0) {
  417. s = seg(up, addr, 1);
  418. if(s == nil)
  419. error(Ebadarg);
  420. s->flushme = 1;
  421. more:
  422. l = len;
  423. if(addr+l > s->top)
  424. l = s->top - addr;
  425. ps = addr-s->base;
  426. pte = s->map[ps/PTEMAPMEM];
  427. ps &= PTEMAPMEM-1;
  428. pe = PTEMAPMEM;
  429. if(pe-ps > l){
  430. pe = ps + l;
  431. pe = (pe+BIGPGSZ-1)&~(BIGPGSZ-1);
  432. }
  433. if(pe == ps) {
  434. qunlock(&s->lk);
  435. error(Ebadarg);
  436. }
  437. if(pte)
  438. pteflush(pte, ps/BIGPGSZ, pe/BIGPGSZ);
  439. chunk = pe-ps;
  440. len -= chunk;
  441. addr += chunk;
  442. if(len > 0 && addr < s->top)
  443. goto more;
  444. qunlock(&s->lk);
  445. }
  446. mmuflush();
  447. ar0->i = 0;
  448. }