syscall.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. #include "sys.h"
  16. #include <tos.h>
  17. #include "amd64.h"
  18. #include "ureg.h"
  19. extern int nosmp;
  20. typedef struct {
  21. uintptr_t ip;
  22. Ureg* arg0;
  23. char* arg1;
  24. char msg[ERRMAX];
  25. Ureg* old;
  26. Ureg ureg;
  27. } NFrame;
  28. /*
  29. * Return user to state before notify()
  30. */
  31. void
  32. noted(Ureg* cur, uintptr_t arg0)
  33. {
  34. Proc *up = externup();
  35. NFrame *nf;
  36. Note note;
  37. Ureg *nur;
  38. qlock(&up->debug);
  39. if(arg0 != NRSTR && !up->notified){
  40. qunlock(&up->debug);
  41. pprint("suicide: call to noted when not notified\n");
  42. pexit("Suicide", 0);
  43. }
  44. up->notified = 0;
  45. fpunoted();
  46. nf = up->ureg;
  47. /* sanity clause */
  48. if(!okaddr(PTR2UINT(nf), sizeof(NFrame), 0)){
  49. qunlock(&up->debug);
  50. pprint("suicide: bad ureg %#p in noted\n", nf);
  51. pexit("Suicide", 0);
  52. }
  53. /*
  54. * Check the segment selectors are all valid.
  55. */
  56. nur = &nf->ureg;
  57. if(nur->cs != SSEL(SiUCS, SsRPL3) || nur->ss != SSEL(SiUDS, SsRPL3)) {
  58. qunlock(&up->debug);
  59. pprint("suicide: bad segment selector (cs %p want %p, ss %p want %p), in noted\n",
  60. nur->cs, SSEL(SiUCS, SsRPL3),
  61. nur->ss, SSEL(SiUDS, SsRPL3)
  62. );
  63. pexit("Suicide", 0);
  64. }
  65. /* don't let user change system flags */
  66. nur->flags &= (Of|Df|Sf|Zf|Af|Pf|Cf);
  67. nur->flags |= cur->flags & ~(Of|Df|Sf|Zf|Af|Pf|Cf);
  68. memmove(cur, nur, sizeof(Ureg));
  69. switch((int)arg0){
  70. case NCONT:
  71. case NRSTR:
  72. if(!okaddr(nur->ip, BY2SE, 0) || !okaddr(nur->sp, BY2SE, 0)){
  73. qunlock(&up->debug);
  74. pprint("suicide: trap in noted pc=%#p sp=%#p\n",
  75. nur->ip, nur->sp);
  76. pexit("Suicide", 0);
  77. }
  78. up->ureg = nf->old;
  79. qunlock(&up->debug);
  80. break;
  81. case NSAVE:
  82. if(!okaddr(nur->ip, BY2SE, 0) || !okaddr(nur->sp, BY2SE, 0)){
  83. qunlock(&up->debug);
  84. pprint("suicide: trap in noted pc=%#p sp=%#p\n",
  85. nur->ip, nur->sp);
  86. pexit("Suicide", 0);
  87. }
  88. qunlock(&up->debug);
  89. splhi();
  90. nf->arg1 = nf->msg;
  91. nf->arg0 = &nf->ureg;
  92. cur->bp = PTR2UINT(nf->arg0);
  93. nf->ip = 0;
  94. cur->sp = PTR2UINT(nf);
  95. break;
  96. default:
  97. memmove(&note, &up->lastnote, sizeof(Note));
  98. qunlock(&up->debug);
  99. pprint("suicide: bad arg %#p in noted: %s\n", arg0, note.msg);
  100. pexit(note.msg, 0);
  101. break;
  102. case NDFLT:
  103. memmove(&note, &up->lastnote, sizeof(Note));
  104. qunlock(&up->debug);
  105. if(note.flag == NDebug)
  106. pprint("suicide: %s\n", note.msg);
  107. pexit(note.msg, note.flag != NDebug);
  108. break;
  109. }
  110. }
  111. /*
  112. * Call user, if necessary, with note.
  113. * Pass user the Ureg struct and the note on his stack.
  114. */
  115. int
  116. notify(Ureg* ureg)
  117. {
  118. Proc *up = externup();
  119. int l;
  120. Mpl pl;
  121. Note note;
  122. uintptr_t sp;
  123. NFrame *nf;
  124. /*
  125. * Calls procctl splhi, see comment in procctl for the reasoning.
  126. */
  127. if(up->procctl)
  128. procctl(up);
  129. if(up->nnote == 0)
  130. return 0;
  131. fpunotify(ureg);
  132. pl = spllo();
  133. qlock(&up->debug);
  134. up->notepending = 0;
  135. memmove(&note, &up->note[0], sizeof(Note));
  136. if(strncmp(note.msg, "sys:", 4) == 0){
  137. l = strlen(note.msg);
  138. if(l > ERRMAX-sizeof(" pc=0x0123456789abcdef"))
  139. l = ERRMAX-sizeof(" pc=0x0123456789abcdef");
  140. sprint(note.msg+l, " pc=%#p", ureg->ip);
  141. }
  142. if(note.flag != NUser && (up->notified || up->notify == nil)){
  143. qunlock(&up->debug);
  144. if(note.flag == NDebug)
  145. pprint("suicide: %s\n", note.msg);
  146. pexit(note.msg, note.flag != NDebug);
  147. }
  148. if(up->notified){
  149. qunlock(&up->debug);
  150. splhi();
  151. return 0;
  152. }
  153. if(up->notify == nil){
  154. qunlock(&up->debug);
  155. pexit(note.msg, note.flag != NDebug);
  156. }
  157. if(!okaddr(PTR2UINT(up->notify), sizeof(ureg->ip), 0)){
  158. qunlock(&up->debug);
  159. pprint("suicide: bad function address %#p in notify\n",
  160. up->notify);
  161. pexit("Suicide", 0);
  162. }
  163. sp = ureg->sp - ROUNDUP(sizeof(NFrame), 16) - 128; // amd64 red zone, also wanted by go stack traces
  164. if(!okaddr(sp, sizeof(NFrame), 1)){
  165. qunlock(&up->debug);
  166. pprint("suicide: bad stack address %#p in notify\n", sp);
  167. pexit("Suicide", 0);
  168. }
  169. nf = UINT2PTR(sp);
  170. memmove(&nf->ureg, ureg, sizeof(Ureg));
  171. nf->old = up->ureg;
  172. up->ureg = nf; /* actually the NFrame, for noted */
  173. memmove(nf->msg, note.msg, ERRMAX);
  174. nf->arg1 = nf->msg;
  175. nf->arg0 = &nf->ureg;
  176. ureg->di = (uintptr)nf->arg0;
  177. ureg->si = (uintptr)nf->arg1;
  178. //print("Setting di to %p and si to %p\n", ureg->di, ureg->si);
  179. ureg->bp = PTR2UINT(nf->arg0);
  180. nf->ip = 0;
  181. ureg->sp = sp;
  182. ureg->ip = PTR2UINT(up->notify);
  183. up->notified = 1;
  184. up->nnote--;
  185. memmove(&up->lastnote, &note, sizeof(Note));
  186. memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note));
  187. qunlock(&up->debug);
  188. splx(pl);
  189. return 1;
  190. }
  191. void
  192. noerrorsleft(void)
  193. {
  194. Proc *up = externup();
  195. int i;
  196. if(up->nerrlab){
  197. /* NIX processes will have a waserror in their handler */
  198. if(up->ac != nil && up->nerrlab == 1)
  199. return;
  200. print("bad errstack: %d extra\n", up->nerrlab);
  201. for(i = 0; i < NERR; i++)
  202. print("sp=%#p pc=%#p\n",
  203. up->errlab[i].sp, up->errlab[i].pc);
  204. panic("error stack");
  205. }
  206. }
  207. int printallsyscalls;
  208. void
  209. syscall(unsigned int scallnr, Ureg *ureg)
  210. {
  211. // can only handle 6 args right now.
  212. uintptr_t a0, a1, a2, a3;
  213. uintptr_t a4, a5;
  214. a0 = ureg->di;
  215. a1 = ureg->si;
  216. a2 = ureg->dx;
  217. a3 = ureg->r10;
  218. a4 = ureg->r8;
  219. a5 = ureg->r9;
  220. Proc *up = externup();
  221. if (0) iprint("Syscall %d, %lx, %lx, %lx %lx %lx %lx\n", scallnr, a0, a1, a2, a3, a4, a5);
  222. char *e;
  223. uintptr_t sp;
  224. int s;
  225. int64_t startns, stopns;
  226. Ar0 ar0;
  227. static Ar0 zar0;
  228. if(!userureg(ureg))
  229. panic("syscall: cs %#llx\n", ureg->cs);
  230. cycles(&up->kentry);
  231. machp()->syscall++;
  232. up->nsyscall++;
  233. up->nqsyscall++;
  234. up->insyscall = 1;
  235. up->pc = ureg->ip;
  236. up->dbgreg = ureg;
  237. sp = ureg->sp;
  238. startns = stopns = 0;
  239. if (0) hi("so far syscall!\n");
  240. if (up->pid == 0 || printallsyscalls) {
  241. syscallfmt('E', scallnr, nil, startns, stopns, a0, a1, a2, a3, a4, a5);
  242. if(up->syscalltrace) {
  243. print("E %s\n", up->syscalltrace);
  244. free(up->syscalltrace);
  245. up->syscalltrace = nil;
  246. }
  247. }
  248. if(up->procctl == Proc_tracesyscall){
  249. /*
  250. * Redundant validaddr. Do we care?
  251. * Tracing syscalls is not exactly a fast path...
  252. * Beware, validaddr currently does a pexit rather
  253. * than an error if there's a problem; that might
  254. * change in the future.
  255. */
  256. if(sp < (USTKTOP-BIGPGSZ) || sp > (USTKTOP-sizeof(up->arg)-BY2SE))
  257. validaddr(UINT2PTR(sp), sizeof(up->arg)+BY2SE, 0);
  258. syscallfmt('E', scallnr, &ar0, startns, stopns, a0, a1, a2, a3, a4, a5);
  259. up->procctl = Proc_stopme;
  260. procctl(up);
  261. if(up->syscalltrace)
  262. free(up->syscalltrace);
  263. up->syscalltrace = nil;
  264. startns = todget(nil);
  265. }
  266. if (0) hi("more syscall!\n");
  267. up->scallnr = scallnr;
  268. if(scallnr == RFORK)
  269. fpusysrfork(ureg);
  270. spllo();
  271. sp = ureg->sp;
  272. up->nerrlab = 0;
  273. ar0 = zar0;
  274. if(!waserror()){
  275. if(scallnr >= nsyscall || systab[scallnr].f == nil){
  276. pprint("bad sys call number %d pc %#llx\n",
  277. scallnr, ureg->ip);
  278. postnote(up, 1, "sys: bad sys call", NDebug);
  279. error(Ebadarg);
  280. }
  281. if(sp < (USTKTOP-BIGPGSZ) || sp > (USTKTOP-sizeof(up->arg)-BY2SE))
  282. validaddr(UINT2PTR(sp), sizeof(up->arg)+BY2SE, 0);
  283. memmove(up->arg, UINT2PTR(sp+BY2SE), sizeof(up->arg));
  284. up->psstate = systab[scallnr].n;
  285. if (0) hi("call syscall!\n");
  286. systab[scallnr].f(&ar0, a0, a1, a2, a3, a4, a5);
  287. if (0) hi("it returned!\n");
  288. if(scallnr == SYSR1){
  289. /*
  290. * BUG: must go when ron binaries go.
  291. * NIX: Returning from execac().
  292. * This means that the process is back to the
  293. * time sharing core. However, the process did
  294. * already return from the system call, when dispatching
  295. * the user code to the AC. The only thing left is to
  296. * return. The user registers should be ok, because
  297. * up->dbgreg has been the user context for the process.
  298. */
  299. return;
  300. }
  301. poperror();
  302. }
  303. else{
  304. /* failure: save the error buffer for errstr */
  305. print("waserror errstr '%s' syserrstr '%s'\n", up->errstr, up->syserrstr);
  306. e = up->syserrstr;
  307. up->syserrstr = up->errstr;
  308. up->errstr = e;
  309. if(DBGFLG && up->pid == 1)
  310. iprint("%s: syscall %s error %s\n",
  311. up->text, systab[scallnr].n, up->syserrstr);
  312. ar0 = systab[scallnr].r;
  313. }
  314. /*
  315. * NIX: for the execac() syscall, what follows is done within
  316. * the system call, because it never returns.
  317. * See acore.c:/^retfromsyscall
  318. */
  319. noerrorsleft();
  320. /*
  321. * Put return value in frame.
  322. */
  323. ureg->ax = ar0.p;
  324. if (up->pid == 0 || printallsyscalls) {
  325. stopns = todget(nil);
  326. syscallfmt('X', scallnr, &ar0, startns, stopns, a0, a1, a2, a3, a4, a5);
  327. if(up->syscalltrace) {
  328. print("X %s\n", up->syscalltrace);
  329. free(up->syscalltrace);
  330. up->syscalltrace = nil;
  331. }
  332. }
  333. if(up->procctl == Proc_tracesyscall){
  334. uint8_t what = 'X';
  335. stopns = todget(nil);
  336. up->procctl = Proc_stopme;
  337. if (scallnr == RFORK && a0 & RFPROC && ar0.i > 0)
  338. what = 'F';
  339. syscallfmt(what, scallnr, &ar0, startns, stopns, a0, a1, a2, a3, a4, a5);
  340. s = splhi();
  341. procctl(up);
  342. splx(s);
  343. if(up->syscalltrace)
  344. free(up->syscalltrace);
  345. up->syscalltrace = nil;
  346. }else if(up->procctl == Proc_totc || up->procctl == Proc_toac)
  347. procctl(up);
  348. if (0) hi("past sysretfmt\n");
  349. up->insyscall = 0;
  350. up->psstate = 0;
  351. if(scallnr == NOTED)
  352. noted(ureg, a0);
  353. if (0) hi("now to splhi\n");
  354. splhi();
  355. if(scallnr != RFORK && (up->procctl || up->nnote))
  356. notify(ureg);
  357. /* if we delayed sched because we held a lock, sched now */
  358. if(up->delaysched){
  359. sched();
  360. splhi();
  361. }
  362. kexit(ureg);
  363. if (0) hi("done kexit\n");
  364. }
  365. uintptr_t
  366. sysexecstack(uintptr_t stack, int argc)
  367. {
  368. uintptr_t sp;
  369. /*
  370. * Given a current bottom-of-stack and a count
  371. * of pointer arguments to be pushed onto it followed
  372. * by an integer argument count, return a suitably
  373. * aligned new bottom-of-stack which will satisfy any
  374. * hardware stack-alignment contraints.
  375. * Rounding the stack down to be aligned with the
  376. * natural size of a pointer variable usually suffices,
  377. * but some architectures impose further restrictions,
  378. * e.g. 32-bit SPARC, where the stack must be 8-byte
  379. * aligned although pointers and integers are 32-bits.
  380. */
  381. USED(argc);
  382. sp = STACKALIGN(stack);
  383. /* but we need to align the stack to 16 bytes, not 8, once
  384. * nil
  385. * argv
  386. * argc
  387. * are pushed. So if we have odd arguments, we need an odd-8-byte
  388. * aligned stack; else, an even aligned stack.
  389. */
  390. if (argc & 1)
  391. sp -= sp & 8 ? 0 : 8;
  392. else
  393. sp -= sp & 8 ? 8 : 0;
  394. //print("For %d args, sp is now %p\n", argc, sp);
  395. return sp;
  396. }
  397. void*
  398. sysexecregs(uintptr_t entry, uint32_t ssize, void *tos)
  399. {
  400. Proc *up = externup();
  401. uintptr_t *sp;
  402. Ureg *ureg;
  403. // We made sure it was correctly aligned in sysexecstack, above.
  404. if (ssize & 0xf) {
  405. print("your stack is wrong: stacksize is not 16-byte aligned: %d\n", ssize);
  406. panic("misaligned stack in sysexecregs");
  407. }
  408. sp = (uintptr_t*)(USTKTOP - ssize);
  409. ureg = up->dbgreg;
  410. ureg->sp = PTR2UINT(sp);
  411. ureg->ip = entry;
  412. ureg->type = 64; /* fiction for acid */
  413. ureg->dx = (uintptr_t)tos;
  414. /*
  415. * return the address of kernel/user shared data
  416. * (e.g. clock stuff)
  417. */
  418. return UINT2PTR(USTKTOP-sizeof(Tos));
  419. }
  420. void
  421. sysprocsetup(Proc* p)
  422. {
  423. fpusysprocsetup(p);
  424. }
  425. void
  426. sysrforkchild(Proc* child, Proc* parent)
  427. {
  428. Ureg *cureg;
  429. // If STACKPAD is 1 things go very bad very quickly.
  430. // But it is the right value ...
  431. #define STACKPAD 1 /* for return PC? */
  432. /*
  433. * Add STACKPAD*BY2SE to the stack to account for
  434. * - the return PC
  435. * (NOT NOW) - trap's arguments (syscallnr, ureg)
  436. */
  437. child->sched.sp = PTR2UINT(child->kstack+KSTACK-((sizeof(Ureg)+STACKPAD*BY2SE)));
  438. child->sched.pc = PTR2UINT(sysrforkret);
  439. cureg = (Ureg*)(child->sched.sp+STACKPAD*BY2SE);
  440. memmove(cureg, parent->dbgreg, sizeof(Ureg));
  441. /* Things from bottom of syscall which were never executed */
  442. child->psstate = 0;
  443. child->insyscall = 0;
  444. //iprint("Child SP set tp %p\n", (void *)child->sched.sp);
  445. fpusysrforkchild(child, parent);
  446. }