syscall.c 11 KB

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