syscall.c 11 KB

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