tcore.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 <tos.h>
  15. #include <pool.h>
  16. #include "amd64.h"
  17. #include "ureg.h"
  18. #include "io.h"
  19. Lock nixaclock; /* NIX AC lock; held while assigning procs to cores */
  20. /*
  21. * NIX support for the time sharing core.
  22. */
  23. extern void actrapret(void);
  24. extern void acsysret(void);
  25. Mach*
  26. getac(Proc *p, int core)
  27. {
  28. Proc *up = externup();
  29. int i;
  30. Mach *mp;
  31. mp = nil;
  32. if(core == 0)
  33. panic("can't getac for a %s", rolename[NIXTC]);
  34. lock(&nixaclock);
  35. if(waserror()){
  36. unlock(&nixaclock);
  37. nexterror();
  38. }
  39. if(core > 0){
  40. if(core >= MACHMAX)
  41. error("no such core");
  42. mp = sys->machptr[core];
  43. if(mp == nil || mp->online == 0 || mp->proc != nil)
  44. error("core not online or busy");
  45. if(mp->NIX.nixtype != NIXAC)
  46. error("core is not an AC");
  47. Found:
  48. mp->proc = p;
  49. }else{
  50. for(i = 0; i < MACHMAX; i++)
  51. if((mp = sys->machptr[i]) != nil && mp->online && mp->NIX.nixtype == NIXAC)
  52. if(mp->proc == nil)
  53. goto Found;
  54. error("not enough cores");
  55. }
  56. unlock(&nixaclock);
  57. poperror();
  58. return mp;
  59. }
  60. /*
  61. * BUG:
  62. * The AC must not accept interrupts while in the kernel,
  63. * or we must be prepared for nesting them, which we are not.
  64. * This is important for note handling, because postnote()
  65. * assumes that it's ok to send an IPI to an AC, no matter its
  66. * state. The /proc interface also assumes that.
  67. *
  68. */
  69. void
  70. intrac(Proc *p)
  71. {
  72. Mach *ac;
  73. ac = p->ac;
  74. if(ac == nil){
  75. DBG("intrac: Proc.ac is nil. no ipi sent.\n");
  76. return;
  77. }
  78. /*
  79. * It's ok if the AC gets idle in the mean time.
  80. */
  81. DBG("intrac: ipi to cpu%d\n", ac->machno);
  82. apicipi(ac->apicno);
  83. }
  84. void
  85. putac(Mach *m)
  86. {
  87. mfence();
  88. m->proc = nil;
  89. }
  90. void
  91. stopac(void)
  92. {
  93. Proc *up = externup();
  94. Mach *mp;
  95. mp = up->ac;
  96. if(mp == nil)
  97. return;
  98. if(mp->proc != up)
  99. panic("stopac");
  100. lock(&nixaclock);
  101. up->ac = nil;
  102. mp->proc = nil;
  103. unlock(&nixaclock);
  104. /* TODO:
  105. * send sipi to up->ac, it would rerun squidboy(), and
  106. * wait for us to give it a function to run.
  107. */
  108. }
  109. /*
  110. * Functions starting with ac... are run in the application core.
  111. * All other functions are run by the time-sharing cores.
  112. */
  113. typedef void (*APfunc)(void);
  114. extern int notify(Ureg*);
  115. /*
  116. * run an arbitrary function with arbitrary args on an ap core
  117. * first argument is always pml4 for process
  118. * make a field and a struct for the args cache line.
  119. *
  120. * Returns the return-code for the ICC or -1 if the process was
  121. * interrupted while issuing the ICC.
  122. */
  123. int
  124. runac(Mach *mp, APfunc func, int flushtlb, void *a, int32_t n)
  125. {
  126. Proc *up = externup();
  127. uint8_t *dpg, *spg;
  128. if (n > sizeof(mp->NIX.icc->data))
  129. panic("runac: args too long");
  130. if(mp->online == 0)
  131. panic("Bad core");
  132. if(mp->proc != nil && mp->proc != up)
  133. panic("runapfunc: mach is busy with another proc?");
  134. memmove(mp->NIX.icc->data, a, n);
  135. if(flushtlb){
  136. DBG("runac flushtlb: cppml4 %#p %#p\n", mp->MMU.pml4->pa, machp()->MMU.pml4->pa);
  137. dpg = UINT2PTR(mp->MMU.pml4->va);
  138. spg = UINT2PTR(machp()->MMU.pml4->va);
  139. /* We should copy less:
  140. * memmove(dgp, spg, machp()->MMU.pml4->daddr * sizeof(PTE));
  141. */
  142. memmove(dpg, spg, PTSZ);
  143. if(0){
  144. print("runac: upac pml4 %#p\n", up->ac->MMU.pml4->pa);
  145. dumpptepg(4, up->ac->MMU.pml4->pa);
  146. }
  147. }
  148. mp->NIX.icc->flushtlb = flushtlb;
  149. mp->NIX.icc->rc = ICCOK;
  150. DBG("runac: exotic proc on cpu%d\n", mp->machno);
  151. if(waserror()){
  152. qunlock(&up->debug);
  153. nexterror();
  154. }
  155. qlock(&up->debug);
  156. up->nicc++;
  157. up->state = Exotic;
  158. up->psstate = 0;
  159. qunlock(&up->debug);
  160. poperror();
  161. mfence();
  162. mp->NIX.icc->fn = func;
  163. sched();
  164. return mp->NIX.icc->rc;
  165. }
  166. /*
  167. * Cleanup done by runacore to pretend we are going back to user space.
  168. * We won't return and won't do what syscall() would normally do.
  169. * Do it here instead.
  170. */
  171. static void
  172. fakeretfromsyscall(Ureg *ureg)
  173. {
  174. Proc *up = externup();
  175. int s;
  176. poperror(); /* as syscall() would do if we would return */
  177. if(up->procctl == Proc_tracesyscall){ /* Would this work? */
  178. up->procctl = Proc_stopme;
  179. s = splhi();
  180. procctl(up);
  181. splx(s);
  182. }
  183. up->insyscall = 0;
  184. /* if we delayed sched because we held a lock, sched now */
  185. if(up->delaysched){
  186. sched();
  187. splhi();
  188. }
  189. kexit(ureg);
  190. }
  191. /*
  192. * Move the current process to an application core.
  193. * This is performed at the end of execac(), and
  194. * we pretend to be returning to user-space, but instead we
  195. * dispatch the process to another core.
  196. * 1. We do the final bookkeeping that syscall() would do after
  197. * a return from sysexec(), because we are not returning.
  198. * 2. We dispatch the process to an AC using an ICC.
  199. *
  200. * This function won't return unless the process is reclaimed back
  201. * to the time-sharing core, and is the handler for the process
  202. * to deal with traps and system calls until the process dies.
  203. *
  204. * Remember that this function is the "line" between user and kernel
  205. * space, it's not expected to raise|handle any error.
  206. *
  207. * We install a safety error label, just in case we raise errors,
  208. * which we shouldn't. (noerrorsleft knows that for exotic processes
  209. * there is an error label pushed by us).
  210. */
  211. void
  212. runacore(void)
  213. {
  214. Proc *up = externup();
  215. Ureg *ureg;
  216. void (*fn)(void);
  217. int rc, flush, s;
  218. char *n;
  219. uint64_t t1;
  220. if(waserror())
  221. panic("runacore: error: %s\n", up->errstr);
  222. ureg = up->dbgreg;
  223. fakeretfromsyscall(ureg);
  224. fpusysrfork(ureg);
  225. procpriority(up, PriKproc, 1);
  226. rc = runac(up->ac, actouser, 1, nil, 0);
  227. procpriority(up, PriNormal, 0);
  228. for(;;){
  229. t1 = fastticks(nil);
  230. flush = 0;
  231. fn = nil;
  232. switch(rc){
  233. case ICCTRAP:
  234. s = splhi();
  235. machp()->MMU.cr2 = up->ac->MMU.cr2;
  236. DBG("runacore: trap %llu cr2 %#llx ureg %#p\n",
  237. ureg->type, machp()->MMU.cr2, ureg);
  238. switch(ureg->type){
  239. case IdtIPI:
  240. if(up->procctl || up->nnote)
  241. notify(up->dbgreg);
  242. if(up->ac == nil)
  243. goto ToTC;
  244. kexit(up->dbgreg);
  245. break;
  246. case IdtNM:
  247. case IdtMF:
  248. case IdtXF:
  249. /* these are handled in the AC;
  250. * If we get here, they left in m->NIX.icc->data
  251. * a note to be posted to the process.
  252. * Post it, and make the vector a NOP.
  253. */
  254. n = up->ac->NIX.icc->note;
  255. if(n != nil)
  256. postnote(up, 1, n, NDebug);
  257. ureg->type = IdtIPI; /* NOP */
  258. break;
  259. default:
  260. cr3put(machp()->MMU.pml4->pa);
  261. if(0 && ureg->type == IdtPF){
  262. print("before PF:\n");
  263. print("AC:\n");
  264. dumpptepg(4, up->ac->MMU.pml4->pa);
  265. print("\n%s:\n", rolename[NIXTC]);
  266. dumpptepg(4, machp()->MMU.pml4->pa);
  267. }
  268. trap(ureg);
  269. }
  270. splx(s);
  271. flush = 1;
  272. fn = actrapret;
  273. break;
  274. case ICCSYSCALL:
  275. DBG("runacore: syscall ax %#llx ureg %#p\n",
  276. ureg->ax, ureg);
  277. cr3put(machp()->MMU.pml4->pa);
  278. //syscall(ureg->ax, ureg);
  279. flush = 1;
  280. fn = acsysret;
  281. if(0)
  282. if(up->nqtrap > 2 || up->nsyscall > 1)
  283. goto ToTC;
  284. if(up->ac == nil)
  285. goto ToTC;
  286. break;
  287. default:
  288. panic("runacore: unexpected rc = %d", rc);
  289. }
  290. up->tctime += fastticks2us(fastticks(nil) - t1);
  291. procpriority(up, PriExtra, 1);
  292. rc = runac(up->ac, fn, flush, nil, 0);
  293. procpriority(up, PriNormal, 0);
  294. }
  295. ToTC:
  296. /*
  297. * to procctl, then syscall, to
  298. * be back in the TC
  299. */
  300. DBG("runacore: up %#p: return\n", up);
  301. }
  302. extern ACVctl *acvctl[];
  303. void
  304. actrapenable(int vno, char* (*f)(Ureg*, void*), void* a, char *name)
  305. {
  306. ACVctl *v;
  307. if(vno < 0 || vno >= 256)
  308. panic("actrapenable: vno %d\n", vno);
  309. v = malloc(sizeof(ACVctl));
  310. v->f = f;
  311. v->a = a;
  312. v->vno = vno;
  313. strncpy(v->name, name, KNAMELEN);
  314. v->name[KNAMELEN-1] = 0;
  315. if(acvctl[vno])
  316. panic("AC traps can't be shared");
  317. acvctl[vno] = v;
  318. }