9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] another kernel question
@ 2008-08-12 23:13 Steve Simon
  2008-08-13  1:47 ` Russ Cox
  0 siblings, 1 reply; 3+ messages in thread
From: Steve Simon @ 2008-08-12 23:13 UTC (permalink / raw)
  To: 9fans

I have found another piece of code I don't understand in the kernel.

syscalls are all fed through a single trap, and the common code which
processes them performs a waserror():

	/sys/src/9/pc/trap.c:694

A few lines down this function (after the system call has been
executed up->nerrlab is checked to ensure we have matching
poperror()s for each waserror() during the execution of said call.

this is fine for most calls, however rfork() explicitly sets up->nerrlab
to zero rather than copying it and memmove()ing up->errlab from the parent
proc to the child:

	 /sys/src/9/port/sysproc.c:90

Surely this means that rfork will always fail with a "bad errstack [19]: -1 extra"
error?

Clearly this doesn't happen but I doin't understand how why it works.

anyone?

-Steve



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [9fans] another kernel question
  2008-08-12 23:13 [9fans] another kernel question Steve Simon
@ 2008-08-13  1:47 ` Russ Cox
  2008-08-13 18:38   ` Steve Simon
  0 siblings, 1 reply; 3+ messages in thread
From: Russ Cox @ 2008-08-13  1:47 UTC (permalink / raw)
  To: 9fans

> this is fine for most calls, however rfork() explicitly sets up->nerrlab
> to zero rather than copying it and memmove()ing up->errlab from the parent
> proc to the child:
>
> 	 /sys/src/9/port/sysproc.c:90
>
> Surely this means that rfork will always fail with a "bad errstack [19]: -1 extra"
> error?

long
sysrfork(ulong *arg)
{
	...

	/* Craft a return frame which will cause the child to pop out of
	 * the scheduler in user mode with the return register zero
	 */
	forkchild(p, up->dbgreg);
	...
	ready(p);
	...
}

void
forkchild(Proc *p, Ureg *ureg)
{
	Ureg *cureg;

	/*
	 * Add 2*BY2WD to the stack to account for
	 *  - the return PC
	 *  - trap's argument (ur)
	 */
	p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Ureg)+2*BY2WD);
	p->sched.pc = (ulong)forkret;

	cureg = (Ureg*)(p->sched.sp+2*BY2WD);
	memmove(cureg, ureg, sizeof(Ureg));
	/* return value of syscall in child */
	cureg->ax = 0;

	/* Things from bottom of syscall which were never executed */
	p->psstate = 0;
	p->insyscall = 0;
}

TEXT forkret(SB), $0
	POPL	AX
	POPAL
	POPL	GS
	POPL	FS
	POPL	ES
	POPL	DS
	ADDL	$8, SP			/* pop error code and trap type */
	IRETL



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [9fans] another kernel question
  2008-08-13  1:47 ` Russ Cox
@ 2008-08-13 18:38   ` Steve Simon
  0 siblings, 0 replies; 3+ messages in thread
From: Steve Simon @ 2008-08-13 18:38 UTC (permalink / raw)
  To: 9fans

> TEXT forkret(SB), $0

very cunning...

thanks russ, I get it now.

-Steve



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-08-13 18:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-12 23:13 [9fans] another kernel question Steve Simon
2008-08-13  1:47 ` Russ Cox
2008-08-13 18:38   ` Steve Simon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).