Browse Source

Clean up setlabel/gotolabel (#1046)

Get rid of `STACKPAD` in syscall.c.

Signed-off-by: Dan Cross <cross@gajendra.net>

Co-authored-by: Dan Cross <cross@gajendra.net>
Dan Cross 3 years ago
parent
commit
777dd92b6b
3 changed files with 26 additions and 38 deletions
  1. 2 1
      sys/src/9/amd64/dat.h
  2. 18 24
      sys/src/9/amd64/l64v.S
  3. 6 13
      sys/src/9/amd64/syscall.c

+ 2 - 1
sys/src/9/amd64/dat.h

@@ -102,7 +102,8 @@ struct Label
 {
 	uintptr_t	sp;
 	uintptr_t	pc;
-	uintptr_t	regs[14];
+	uintptr_t	fp;
+	uintptr_t	_pad[13];
 };
 
 struct Fxsave {

+ 18 - 24
sys/src/9/amd64/l64v.S

@@ -414,38 +414,32 @@ _cas32r0:
 	RET
 
 /*
- * Label consists of a stack pointer and a programme counter
- * 0(%rdi) is the SP, 8(%rdi) is the PC
+ * Label consists of a stack pointer, a program counter, and
+ * a frame pointer.
+ *  0(%rdi) is the SP,
+ *  8(%rdi) is the PC,
+ * 16(%rdi) is the FP.
  */
 .global gotolabel
 gotolabel:
-	MOVQ	%rdi, %rax
 	MOVQ	0(%rdi), %rsp
-
-	// Can't kill this quite yet.
-	MOVQ	(16+5*8)(%rdi), %rBP
-
-	MOVQ	8(%rax), %rax			/* put return PC on the stack */
-						/* NOTE: replaces previous caller? */
-	MOVQ	%rax, (%rSP)
+	MOVQ	8(%rdi), %r11
+	MOVQ	16(%rdi), %rbp
 	MOVQ	$1, %rax			/* return 1 */
-	RET
+	JMP	*%r11
 
-	/* save all registers on this stack, the save stack
-	* in the label struct.
-	*/
+/*
+ * save all registers on this stack, the save stack
+ * in the label struct.
+ */
 .global slim_setlabel
 slim_setlabel:
-	// %rax is trashable.
-	MOVQ	0(%rSP), %rax			/* store return PC */
-	MOVQ	%rax, 8(%rdi)
-
-	// Can't kill this quite yet.
-	MOVQ	%rBP, (16+5*8)(%rdi)
-
-	MOVQ	%rSP, 0(%rdi)	/* store SP */
-	MOVL	$0, %eax	/* return 0 */
-	RET
+	MOVQ	%rbp, 16(%rdi)
+	POPQ	%r11		/* Save return PC */
+	MOVQ	%r11, 8(%rdi)
+	MOVQ	%rsp, 0(%rdi)	/* store SP */
+	XORL	%eax, %eax	/* return 0 */
+	JMP	*%r11
 
 .global hardhalt
 hardhalt:

+ 6 - 13
sys/src/9/amd64/syscall.c

@@ -203,12 +203,12 @@ notify(Ureg* ureg)
 	nf->arg0 = &nf->ureg;
 	ureg->di = (uintptr)nf->arg0;
 	ureg->si = (uintptr)nf->arg1;
-	//print("Setting di to %p and si to %p\n", ureg->di, ureg->si);
 	ureg->bp = PTR2UINT(nf->arg0);
 	nf->ip = 0;
 
 	ureg->sp = sp;
 	ureg->ip = PTR2UINT(up->notify);
+	//print("NOTIFY: Setting di to %p and si to %p, sp=%#P, pc=%#P\n", ureg->di, ureg->si,ureg->sp, ureg->ip);
 	up->notified = 1;
 	up->nnote--;
 	memmove(&up->lastnote, &note, sizeof(Note));
@@ -532,21 +532,14 @@ sysprocsetup(Proc* p)
 void
 sysrforkchild(Proc* child, Proc* parent)
 {
-	Ureg *cureg;
-// If STACKPAD is 1 things go very bad very quickly.
-// But it is the right value ...
-#define STACKPAD 1 /* for return PC? */
-	/*
-	 * Add STACKPAD*BY2SE to the stack to account for
-	 *  - the return PC
-	 *  (NOT NOW) - trap's arguments (syscallnr, ureg)
-	 */
-	child->sched.sp = PTR2UINT(child->kstack+KSTACK-((sizeof(Ureg)+STACKPAD*BY2SE)));
-	child->sched.pc = PTR2UINT(sysrforkret);
+	char *cureg;
 
-	cureg = (Ureg*)(child->sched.sp+STACKPAD*BY2SE);
+	cureg = child->kstack+KSTACK-sizeof(Ureg);
 	memmove(cureg, parent->dbgreg, sizeof(Ureg));
 
+	child->sched.sp = PTR2UINT(cureg);
+	child->sched.pc = PTR2UINT(sysrforkret);
+
 	/* Things from bottom of syscall which were never executed */
 	child->psstate = 0;
 	child->insyscall = 0;