2 #include "../port/lib.h"
6 #include "../port/error.h"
7 #include "../port/systab.h"
24 * Return user to state before notify()
27 noted(Ureg* cur, uintptr arg0)
33 if(arg0 != NRSTR && !up->notified){
35 pprint("call to noted() when not notified\n");
44 if(!okaddr(PTR2UINT(nf), sizeof(NFrame), 0)){
46 pprint("bad ureg in noted %#p\n", nf);
50 /* don't let user change system flags */
52 nur->psr &= PsrMask|PsrDfiq|PsrDirq;
53 nur->psr |= (cur->psr & ~(PsrMask|PsrDfiq|PsrDirq));
55 memmove(cur, nur, sizeof(Ureg));
60 if(!okaddr(nur->pc, BY2WD, 0) || !okaddr(nur->sp, BY2WD, 0)){
62 pprint("suicide: trap in noted\n");
69 if(!okaddr(nur->pc, BY2WD, 0) || !okaddr(nur->sp, BY2WD, 0)){
71 pprint("suicide: trap in noted\n");
80 cur->sp = PTR2UINT(nf);
83 up->lastnote.flag = NDebug;
87 if(up->lastnote.flag == NDebug)
88 pprint("suicide: %s\n", up->lastnote.msg);
89 pexit(up->lastnote.msg, up->lastnote.flag != NDebug);
94 * Call user, if necessary, with note.
95 * Pass user the Ureg struct and the note on his stack.
118 if(strncmp(n->msg, "sys:", 4) == 0){
120 if(l > ERRMAX-23) /* " pc=0x0123456789abcdef\0" */
122 snprint(n->msg + l, sizeof n->msg - l, " pc=%#lux", ureg->pc);
125 if(n->flag != NUser && (up->notified || up->notify == 0)){
127 if(n->flag == NDebug)
128 pprint("suicide: %s\n", n->msg);
129 pexit(n->msg, n->flag != NDebug);
138 if(up->notify == nil){
140 pexit(n->msg, n->flag != NDebug);
142 if(!okaddr(PTR2UINT(up->notify), 1, 0)){
144 pprint("suicide: notify function address %#p\n", up->notify);
148 sp = ureg->sp - sizeof(NFrame);
149 if(!okaddr(sp, sizeof(NFrame), 1)){
151 pprint("suicide: notify stack address %#p\n", sp);
156 memmove(&nf->ureg, ureg, sizeof(Ureg));
159 memmove(nf->msg, up->note[0].msg, ERRMAX);
161 nf->arg0 = &nf->ureg;
165 ureg->pc = PTR2UINT(up->notify);
168 memmove(&up->lastnote, &up->note[0], sizeof(Note));
169 memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note));
185 vlong startns, stopns;
188 panic("syscall: from kernel: pc %#lux r14 %#lux psr %#lux",
189 ureg->pc, ureg->r14, ureg->psr);
199 up->scallnr = scallnr;
206 if(sp < (USTKTOP-BY2PG) || sp > (USTKTOP-sizeof(Sargs)-BY2WD))
207 validaddr(sp, sizeof(Sargs)+BY2WD, 0);
208 up->s = *((Sargs*)(sp+BY2WD));
209 if(up->procctl == Proc_tracesyscall){
210 syscallfmt(scallnr, ureg->pc, (va_list)up->s.args);
212 up->procctl = Proc_stopme;
215 startns = todget(nil);
217 if(scallnr >= nsyscall){
218 pprint("bad sys call number %d pc %#lux\n",
220 postnote(up, 1, "sys: bad sys call", NDebug);
223 up->psstate = sysctab[scallnr];
225 /* iprint("%s: syscall %s\n", up->text, sysctab[scallnr]?sysctab[scallnr]:"huh?"); */
227 ret = systab[scallnr]((va_list)up->s.args);
230 /* failure: save the error buffer for errstr */
232 up->syserrstr = up->errstr;
236 print("bad errstack [%d]: %d extra\n", scallnr, up->nerrlab);
237 for(i = 0; i < NERR; i++)
238 print("sp=%#p pc=%#p\n",
239 up->errlab[i].sp, up->errlab[i].pc);
240 panic("error stack");
244 * Put return value in frame. On the x86 the syscall is
245 * just another trap and the return value from syscall is
246 * ignored. On other machines the return value is put into
247 * the results register by caller of syscall.
251 if(up->procctl == Proc_tracesyscall){
252 stopns = todget(nil);
253 sysretfmt(scallnr, (va_list)up->s.args, ret, startns, stopns);
255 up->procctl = Proc_stopme;
264 noted(ureg, up->s.args[0]);
267 if(scallnr != RFORK && (up->procctl || up->nnote))
270 /* if we delayed sched because we held a lock, sched now */
279 execregs(uintptr entry, ulong ssize, ulong nargs)
284 sp = (ulong*)(USTKTOP - ssize);
288 // memset(ureg, 0, 15*sizeof(ulong));
289 ureg->r13 = (ulong)sp;
291 //print("%lud: EXECREGS pc %#ux sp %#ux nargs %ld\n", up->pid, ureg->pc, ureg->r13, nargs);
294 * return the address of kernel/user shared data
297 return USTKTOP-sizeof(Tos);
301 sysprocsetup(Proc* p)
307 * Craft a return frame which will cause the child to pop out of
308 * the scheduler in user mode with the return register zero. Set
309 * pc to point to a l.s return function.
312 forkchild(Proc *p, Ureg *ureg)
316 //print("%lud setting up for forking child %lud\n", up->pid, p->pid);
317 p->sched.sp = (ulong)p->kstack+KSTACK-sizeof(Ureg);
318 p->sched.pc = (ulong)forkret;
320 cureg = (Ureg*)(p->sched.sp);
321 memmove(cureg, ureg, sizeof(Ureg));
323 /* syscall returns 0 for child */
326 /* Things from bottom of syscall which were never executed */