syscall.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 (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. /* it should be unsigned. FIXME */
  209. void
  210. syscall(int badscallnr, Ureg *ureg)
  211. {
  212. // can only handle 4 args right now.
  213. uintptr_t a0, a1, a2, a3;
  214. uintptr_t a4, a5 = 0;
  215. a0 = ureg->di;
  216. a1 = ureg->si;
  217. a2 = ureg->dx;
  218. a3 = ureg->r10;
  219. a4 = ureg->r8;
  220. Proc *up = externup();
  221. unsigned int scallnr = (unsigned int) badscallnr;
  222. if (0) iprint("Syscall %d, %lx, %lx, %lx %lx %lx\n", scallnr, a0, a1, a2, a3, a4);
  223. char *e;
  224. uintptr_t sp;
  225. int s;
  226. int64_t startns, stopns;
  227. Ar0 ar0;
  228. static Ar0 zar0;
  229. if(!userureg(ureg))
  230. panic("syscall: cs %#llux\n", ureg->cs);
  231. cycles(&up->kentry);
  232. machp()->syscall++;
  233. up->nsyscall++;
  234. up->nqsyscall++;
  235. up->insyscall = 1;
  236. up->pc = ureg->ip;
  237. up->dbgreg = ureg;
  238. sp = ureg->sp;
  239. startns = 0;
  240. if (0) hi("so far syscall!\n");
  241. if (printallsyscalls) {
  242. syscallfmt(scallnr, a0, a1, a2, a3, a4, a5);
  243. if(up->syscalltrace) {
  244. print("E %s\n", up->syscalltrace);
  245. free(up->syscalltrace);
  246. up->syscalltrace = nil;
  247. }
  248. }
  249. if(up->procctl == Proc_tracesyscall){
  250. /*
  251. * Redundant validaddr. Do we care?
  252. * Tracing syscalls is not exactly a fast path...
  253. * Beware, validaddr currently does a pexit rather
  254. * than an error if there's a problem; that might
  255. * change in the future.
  256. */
  257. if(sp < (USTKTOP-BIGPGSZ) || sp > (USTKTOP-sizeof(up->arg)-BY2SE))
  258. validaddr(UINT2PTR(sp), sizeof(up->arg)+BY2SE, 0);
  259. syscallfmt(scallnr, a0, a1, a2, a3, a4, a5);
  260. up->procctl = Proc_stopme;
  261. procctl(up);
  262. if(up->syscalltrace)
  263. free(up->syscalltrace);
  264. up->syscalltrace = nil;
  265. startns = todget(nil);
  266. }
  267. if (0) hi("more syscall!\n");
  268. up->scallnr = scallnr;
  269. if(scallnr == RFORK)
  270. fpusysrfork(ureg);
  271. spllo();
  272. sp = ureg->sp;
  273. up->nerrlab = 0;
  274. ar0 = zar0;
  275. if(!waserror()){
  276. if(scallnr >= nsyscall || systab[scallnr].f == nil){
  277. pprint("bad sys call number %d pc %#llux\n",
  278. scallnr, ureg->ip);
  279. postnote(up, 1, "sys: bad sys call", NDebug);
  280. error(Ebadarg);
  281. }
  282. if(sp < (USTKTOP-BIGPGSZ) || sp > (USTKTOP-sizeof(up->arg)-BY2SE))
  283. validaddr(UINT2PTR(sp), sizeof(up->arg)+BY2SE, 0);
  284. memmove(up->arg, UINT2PTR(sp+BY2SE), sizeof(up->arg));
  285. up->psstate = systab[scallnr].n;
  286. if (0) hi("call syscall!\n");
  287. systab[scallnr].f(&ar0, a0, a1, a2, a3, a4, a5);
  288. if (0) hi("it returned!\n");
  289. if(scallnr == SYSR1){
  290. /*
  291. * BUG: must go when ron binaries go.
  292. * NIX: Returning from execac().
  293. * This means that the process is back to the
  294. * time sharing core. However, the process did
  295. * already return from the system call, when dispatching
  296. * the user code to the AC. The only thing left is to
  297. * return. The user registers should be ok, because
  298. * up->dbgreg has been the user context for the process.
  299. */
  300. return;
  301. }
  302. poperror();
  303. }
  304. else{
  305. /* failure: save the error buffer for errstr */
  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 (printallsyscalls) {
  325. stopns = todget(nil);
  326. sysretfmt(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. stopns = todget(nil);
  335. up->procctl = Proc_stopme;
  336. sysretfmt(scallnr, &ar0, startns, stopns, a0, a1, a2, a3, a4, a5);
  337. s = splhi();
  338. procctl(up);
  339. splx(s);
  340. if(up->syscalltrace)
  341. free(up->syscalltrace);
  342. up->syscalltrace = nil;
  343. }else if(up->procctl == Proc_totc || up->procctl == Proc_toac)
  344. procctl(up);
  345. if (0) hi("past sysretfmt\n");
  346. up->insyscall = 0;
  347. up->psstate = 0;
  348. if(scallnr == NOTED)
  349. noted(ureg, a0);
  350. if (0) hi("now to splhi\n");
  351. splhi();
  352. if(scallnr != RFORK && (up->procctl || up->nnote))
  353. notify(ureg);
  354. /* if we delayed sched because we held a lock, sched now */
  355. if(up->delaysched){
  356. sched();
  357. splhi();
  358. }
  359. kexit(ureg);
  360. if (0) hi("done kexit\n");
  361. }
  362. uintptr_t
  363. sysexecstack(uintptr_t stack, int argc)
  364. {
  365. uintptr_t sp;
  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. sp = STACKALIGN(stack);
  380. /* but we need to align the stack to 16 bytes, not 8, once
  381. * nil
  382. * argv
  383. * argc
  384. * are pushed. So if we have odd arguments, we need an odd-8-byte
  385. * aligned stack; else, an even aligned stack.
  386. */
  387. if (argc & 1)
  388. sp -= sp & 8 ? 0 : 8;
  389. else
  390. sp -= sp & 8 ? 8 : 0;
  391. //print("For %d args, sp is now %p\n", argc, sp);
  392. return sp;
  393. }
  394. void*
  395. sysexecregs(uintptr_t entry, uint32_t ssize, void *tos)
  396. {
  397. Proc *up = externup();
  398. uintptr_t *sp;
  399. Ureg *ureg;
  400. // We made sure it was correctly aligned in sysexecstack, above.
  401. if (ssize & 0xf) {
  402. print("your stack is wrong: stacksize is not 16-byte aligned: %d\n", ssize);
  403. panic("misaligned stack in sysexecregs");
  404. }
  405. sp = (uintptr_t*)(USTKTOP - ssize);
  406. ureg = up->dbgreg;
  407. ureg->sp = PTR2UINT(sp);
  408. ureg->ip = entry;
  409. ureg->type = 64; /* fiction for acid */
  410. ureg->dx = (uintptr_t)tos;
  411. /*
  412. * return the address of kernel/user shared data
  413. * (e.g. clock stuff)
  414. */
  415. return UINT2PTR(USTKTOP-sizeof(Tos));
  416. }
  417. void
  418. sysprocsetup(Proc* p)
  419. {
  420. fpusysprocsetup(p);
  421. }
  422. void
  423. sysrforkchild(Proc* child, Proc* parent)
  424. {
  425. Ureg *cureg;
  426. // If STACKPAD is 1 things go very bad very quickly.
  427. // But it is the right value ...
  428. #define STACKPAD 1 /* for return PC? */
  429. /*
  430. * Add STACKPAD*BY2SE to the stack to account for
  431. * - the return PC
  432. * (NOT NOW) - trap's arguments (syscallnr, ureg)
  433. */
  434. child->sched.sp = PTR2UINT(child->kstack+KSTACK-((sizeof(Ureg)+STACKPAD*BY2SE)));
  435. child->sched.pc = PTR2UINT(sysrforkret);
  436. cureg = (Ureg*)(child->sched.sp+STACKPAD*BY2SE);
  437. memmove(cureg, parent->dbgreg, sizeof(Ureg));
  438. /* Things from bottom of syscall which were never executed */
  439. child->psstate = 0;
  440. child->insyscall = 0;
  441. //iprint("Child SP set tp %p\n", (void *)child->sched.sp);
  442. fpusysrforkchild(child, parent);
  443. }