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((uintptr)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 = (uintptr)nf;
81 cur->r0 = (uintptr)nf->arg0;
84 up->lastnote.flag = NDebug;
88 if(up->lastnote.flag == NDebug)
89 pprint("suicide: %s\n", up->lastnote.msg);
90 pexit(up->lastnote.msg, up->lastnote.flag != NDebug);
95 * Call user, if necessary, with note.
96 * Pass user the Ureg struct and the note on his stack.
119 if(strncmp(n->msg, "sys:", 4) == 0){
121 if(l > ERRMAX-23) /* " pc=0x0123456789abcdef\0" */
123 snprint(n->msg + l, sizeof n->msg - l, " pc=%#lux", ureg->pc);
126 if(n->flag != NUser && (up->notified || up->notify == 0)){
128 if(n->flag == NDebug)
129 pprint("suicide: %s\n", n->msg);
130 pexit(n->msg, n->flag != NDebug);
139 if(up->notify == nil){
141 pexit(n->msg, n->flag != NDebug);
143 if(!okaddr((uintptr)up->notify, 1, 0)){
145 pprint("suicide: notify function address %#p\n", up->notify);
149 sp = ureg->sp - sizeof(NFrame);
150 if(!okaddr(sp, sizeof(NFrame), 1)){
152 pprint("suicide: notify stack address %#p\n", sp);
157 memmove(&nf->ureg, ureg, sizeof(Ureg));
160 memmove(nf->msg, up->note[0].msg, ERRMAX);
162 nf->arg0 = &nf->ureg;
166 ureg->pc = (uintptr)up->notify;
167 ureg->r0 = (uintptr)nf->arg0;
171 memmove(&up->lastnote, &up->note[0], sizeof(Note));
172 memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note));
190 panic("syscall: from kernel: pc %#lux r14 %#lux psr %#lux",
191 ureg->pc, ureg->r14, ureg->psr);
197 if(up->procctl == Proc_tracesyscall){
198 up->procctl = Proc_stopme;
203 up->scallnr = scallnr;
210 if(scallnr >= nsyscall){
211 pprint("bad sys call number %d pc %#lux\n",
213 postnote(up, 1, "sys: bad sys call", NDebug);
217 if(sp < (USTKTOP-BY2PG) || sp > (USTKTOP-sizeof(Sargs)-BY2WD))
218 validaddr(sp, sizeof(Sargs)+BY2WD, 0);
220 up->s = *((Sargs*)(sp+BY2WD));
221 up->psstate = sysctab[scallnr];
223 /* iprint("%s: syscall %s\n", up->text, sysctab[scallnr]?sysctab[scallnr]:"huh?"); */
225 ret = systab[scallnr]((va_list)up->s.args);
228 /* failure: save the error buffer for errstr */
230 up->syserrstr = up->errstr;
234 print("bad errstack [%d]: %d extra\n", scallnr, up->nerrlab);
235 for(i = 0; i < NERR; i++)
236 print("sp=%#p pc=%#p\n",
237 up->errlab[i].sp, up->errlab[i].pc);
238 panic("error stack");
242 * Put return value in frame. On the x86 the syscall is
243 * just another trap and the return value from syscall is
244 * ignored. On other machines the return value is put into
245 * the results register by caller of syscall.
249 if(up->procctl == Proc_tracesyscall){
251 up->procctl = Proc_stopme;
260 noted(ureg, *(ulong*)(sp+BY2WD));
263 if(scallnr != RFORK && (up->procctl || up->nnote))
266 /* if we delayed sched because we held a lock, sched now */
276 execregs(uintptr entry, ulong ssize, ulong nargs)
281 sp = (ulong*)(USTKTOP - ssize);
285 // memset(ureg, 0, 15*sizeof(ulong));
286 ureg->r13 = (ulong)sp;
288 //print("%lud: EXECREGS pc %#ux sp %#ux nargs %ld\n", up->pid, ureg->pc, ureg->r13, nargs);
291 * return the address of kernel/user shared data
294 return USTKTOP-sizeof(Tos);
298 sysprocsetup(Proc* p)
304 * Craft a return frame which will cause the child to pop out of
305 * the scheduler in user mode with the return register zero. Set
306 * pc to point to a l.s return function.
309 forkchild(Proc *p, Ureg *ureg)
313 //print("%lud setting up for forking child %lud\n", up->pid, p->pid);
314 p->sched.sp = (ulong)p->kstack+KSTACK-sizeof(Ureg);
315 p->sched.pc = (ulong)forkret;
317 cureg = (Ureg*)(p->sched.sp);
318 memmove(cureg, ureg, sizeof(Ureg));
320 /* syscall returns 0 for child */