syscall.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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;
  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. a0 = ureg->a0;
  205. a1 = ureg->a1;
  206. a2 = ureg->a2;
  207. a3 = ureg->a3;
  208. a4 = ureg->a4;
  209. a5 = ureg->a5;
  210. Proc *up = externup();
  211. if (0) iprint("Syscall %d, %lx, %lx, %lx %lx %lx %lx\n", scallnr, a0, a1, a2, a3, a4, a5);
  212. char *e;
  213. uintptr_t sp;
  214. int s;
  215. int64_t startns, stopns;
  216. Ar0 ar0;
  217. static Ar0 zar0;
  218. panic("test userureg");
  219. //if(!userureg(ureg))
  220. //panic("syscall: cs %#llx\n", ureg->cs);
  221. cycles(&up->kentry);
  222. machp()->syscall++;
  223. up->nsyscall++;
  224. up->nqsyscall++;
  225. up->insyscall = 1;
  226. up->pc = ureg->ip;
  227. up->dbgreg = ureg;
  228. sp = ureg->sp;
  229. startns = stopns = 0;
  230. if (0) hi("so far syscall!\n");
  231. if (up->pid == 0 || printallsyscalls) {
  232. syscallfmt('E', scallnr, nil, startns, stopns, a0, a1, a2, a3, a4, a5);
  233. if(up->syscalltrace) {
  234. print("E %s\n", up->syscalltrace);
  235. free(up->syscalltrace);
  236. up->syscalltrace = nil;
  237. }
  238. }
  239. if(up->procctl == Proc_tracesyscall){
  240. /*
  241. * Redundant validaddr. Do we care?
  242. * Tracing syscalls is not exactly a fast path...
  243. * Beware, validaddr currently does a pexit rather
  244. * than an error if there's a problem; that might
  245. * change in the future.
  246. */
  247. if(sp < (USTKTOP-BIGPGSZ) || sp > (USTKTOP-sizeof(up->arg)-BY2SE))
  248. validaddr(UINT2PTR(sp), sizeof(up->arg)+BY2SE, 0);
  249. syscallfmt('E', scallnr, &ar0, startns, stopns, a0, a1, a2, a3, a4, a5);
  250. up->procctl = Proc_stopme;
  251. procctl(up);
  252. if(up->syscalltrace)
  253. free(up->syscalltrace);
  254. up->syscalltrace = nil;
  255. startns = todget(nil);
  256. }
  257. if (0) hi("more syscall!\n");
  258. up->scallnr = scallnr;
  259. if(scallnr == RFORK)
  260. fpusysrfork(ureg);
  261. spllo();
  262. sp = ureg->sp;
  263. up->nerrlab = 0;
  264. ar0 = zar0;
  265. if(!waserror()){
  266. if(scallnr >= nsyscall || systab[scallnr].f == nil){
  267. pprint("bad sys call number %d pc %#llx\n",
  268. scallnr, ureg->ip);
  269. postnote(up, 1, "sys: bad sys call", NDebug);
  270. error(Ebadarg);
  271. }
  272. if(sp < (USTKTOP-BIGPGSZ) || sp > (USTKTOP-sizeof(up->arg)-BY2SE))
  273. validaddr(UINT2PTR(sp), sizeof(up->arg)+BY2SE, 0);
  274. memmove(up->arg, UINT2PTR(sp+BY2SE), sizeof(up->arg));
  275. up->psstate = systab[scallnr].n;
  276. if (0) hi("call syscall!\n");
  277. systab[scallnr].f(&ar0, a0, a1, a2, a3, a4, a5);
  278. if (0) hi("it returned!\n");
  279. poperror();
  280. }
  281. else{
  282. /* failure: save the error buffer for errstr */
  283. e = up->syserrstr;
  284. up->syserrstr = up->errstr;
  285. up->errstr = e;
  286. if(DBGFLG && up->pid == 1)
  287. iprint("%s: syscall %s error %s\n",
  288. up->text, systab[scallnr].n, up->syserrstr);
  289. ar0 = systab[scallnr].r;
  290. }
  291. /*
  292. * NIX: for the execac() syscall, what follows is done within
  293. * the system call, because it never returns.
  294. * See acore.c:/^retfromsyscall
  295. */
  296. noerrorsleft();
  297. /*
  298. * Put return value in frame.
  299. */
  300. ureg->a0 = ar0.p;
  301. if (up->pid == 0 || printallsyscalls) {
  302. stopns = todget(nil);
  303. syscallfmt('X', scallnr, &ar0, startns, stopns, a0, a1, a2, a3, a4, a5);
  304. if(up->syscalltrace) {
  305. print("X %s\n", up->syscalltrace);
  306. free(up->syscalltrace);
  307. up->syscalltrace = nil;
  308. }
  309. }
  310. if(up->procctl == Proc_tracesyscall){
  311. uint8_t what = 'X';
  312. stopns = todget(nil);
  313. up->procctl = Proc_stopme;
  314. if (scallnr == RFORK && a0 & RFPROC && ar0.i > 0)
  315. what = 'F';
  316. syscallfmt(what, scallnr, &ar0, startns, stopns, a0, a1, a2, a3, a4, a5);
  317. s = splhi();
  318. procctl(up);
  319. splx(s);
  320. if(up->syscalltrace)
  321. free(up->syscalltrace);
  322. up->syscalltrace = nil;
  323. }else if(up->procctl == Proc_totc || up->procctl == Proc_toac)
  324. procctl(up);
  325. if (0) hi("past sysretfmt\n");
  326. up->insyscall = 0;
  327. up->psstate = 0;
  328. if(scallnr == NOTED)
  329. noted(ureg, a0);
  330. if (0) hi("now to splhi\n");
  331. splhi();
  332. if(scallnr != RFORK && (up->procctl || up->nnote))
  333. notify(ureg);
  334. /* if we delayed sched because we held a lock, sched now */
  335. if(up->delaysched){
  336. sched();
  337. splhi();
  338. }
  339. kexit(ureg);
  340. if (0) hi("done kexit\n");
  341. }
  342. uintptr_t
  343. sysexecstack(uintptr_t stack, int argc)
  344. {
  345. uintptr_t sp;
  346. /*
  347. * Given a current bottom-of-stack and a count
  348. * of pointer arguments to be pushed onto it followed
  349. * by an integer argument count, return a suitably
  350. * aligned new bottom-of-stack which will satisfy any
  351. * hardware stack-alignment contraints.
  352. * Rounding the stack down to be aligned with the
  353. * natural size of a pointer variable usually suffices,
  354. * but some architectures impose further restrictions,
  355. * e.g. 32-bit SPARC, where the stack must be 8-byte
  356. * aligned although pointers and integers are 32-bits.
  357. */
  358. USED(argc);
  359. sp = STACKALIGN(stack);
  360. /* but we need to align the stack to 16 bytes, not 8, once
  361. * nil
  362. * argv
  363. * argc
  364. * are pushed. So if we have odd arguments, we need an odd-8-byte
  365. * aligned stack; else, an even aligned stack.
  366. */
  367. if (argc & 1)
  368. sp -= sp & 8 ? 0 : 8;
  369. else
  370. sp -= sp & 8 ? 8 : 0;
  371. //print("For %d args, sp is now %p\n", argc, sp);
  372. return sp;
  373. }
  374. void*
  375. sysexecregs(uintptr_t entry, uint32_t ssize, void *tos)
  376. {
  377. Proc *up = externup();
  378. uintptr_t *sp;
  379. Ureg *ureg;
  380. // We made sure it was correctly aligned in sysexecstack, above.
  381. if (ssize & 0xf) {
  382. print("your stack is wrong: stacksize is not 16-byte aligned: %d\n", ssize);
  383. panic("misaligned stack in sysexecregs");
  384. }
  385. sp = (uintptr_t*)(USTKTOP - ssize);
  386. ureg = up->dbgreg;
  387. ureg->sp = PTR2UINT(sp);
  388. ureg->ip = entry;
  389. //ureg->type = 64; /* fiction for acid */
  390. panic("sysexecregs");
  391. //ureg->dx = (uintptr_t)tos;
  392. /*
  393. * return the address of kernel/user shared data
  394. * (e.g. clock stuff)
  395. */
  396. return UINT2PTR(USTKTOP-sizeof(Tos));
  397. }
  398. void
  399. sysprocsetup(Proc* p)
  400. {
  401. fpusysprocsetup(p);
  402. }
  403. void
  404. sysrforkchild(Proc* child, Proc* parent)
  405. {
  406. Ureg *cureg;
  407. // If STACKPAD is 1 things go very bad very quickly.
  408. // But it is the right value ...
  409. #define STACKPAD 1 /* for return PC? */
  410. /*
  411. * Add STACKPAD*BY2SE to the stack to account for
  412. * - the return PC
  413. * (NOT NOW) - trap's arguments (syscallnr, ureg)
  414. */
  415. child->sched.sp = PTR2UINT(child->kstack+KSTACK-((sizeof(Ureg)+STACKPAD*BY2SE)));
  416. child->sched.pc = PTR2UINT(sysrforkret);
  417. cureg = (Ureg*)(child->sched.sp+STACKPAD*BY2SE);
  418. memmove(cureg, parent->dbgreg, sizeof(Ureg));
  419. /* Things from bottom of syscall which were never executed */
  420. child->psstate = 0;
  421. child->insyscall = 0;
  422. //iprint("Child SP set tp %p\n", (void *)child->sched.sp);
  423. fpusysrforkchild(child, parent);
  424. }