From cc595c7c9908c33e644a41e2d96004c647e25c84 Mon Sep 17 00:00:00 2001 From: Giacomo Tesio Date: Sun, 21 May 2017 00:15:51 +0200 Subject: [PATCH] kernel: save and restore all registers on syscall --- sys/src/kern/amd64/dat.h | 2 ++ sys/src/kern/amd64/l64vsyscall.S | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/sys/src/kern/amd64/dat.h b/sys/src/kern/amd64/dat.h index 7ae0376..a8bb4f4 100644 --- a/sys/src/kern/amd64/dat.h +++ b/sys/src/kern/amd64/dat.h @@ -198,6 +198,8 @@ struct Mach uint64_t machno; /* physical id of processor */ uint64_t splpc; /* pc of last caller to splhi */ Proc* proc; /* current process on this processor */ + uint64_t tmp0; /* for syscallentry */ + uint64_t tmp1; /* for syscallentry */ int apicno; int online; diff --git a/sys/src/kern/amd64/l64vsyscall.S b/sys/src/kern/amd64/l64vsyscall.S index 500715c..a6fc8a4 100644 --- a/sys/src/kern/amd64/l64vsyscall.S +++ b/sys/src/kern/amd64/l64vsyscall.S @@ -28,10 +28,14 @@ touser: syscallentry: swapgs - movq %gs:0, %r15 /* m-> (movq GS:0x0, R15) */ + xchgq %gs:0, %r15 + movq %r14, 24(%r15) /* save %r14 to m->tmp0 */ movq 16(%r15), %r14 /* m->proc (set up->) */ + movq %r13, 32(%r15) /* save %r13 to m->tmp1 */ movq 16*8(%r14), %r13 /* m->proc->kstack (after the Label) */ xchgq %r13, %rsp + movq 24(%r15), %r14 /* restore %r14 */ + addq $KSTACK, %rsp /* build Ureg */ @@ -41,6 +45,10 @@ syscallentry: pushq $SSEL(SiUCS, SsRPL3) /* old code segment */ pushq %rcx /* old ip */ + movq %r14, 24(%r15) /* restore %r14 from m->tmp0 */ + movq %r13, 32(%r15) /* restore %r13 from m->tmp1 */ + xchgq %gs:0, %r15 /* restore %r15 */ + subq $(17*8), %rsp movq %rax, (0*8)(%rsp) // ureg.ax @@ -61,6 +69,10 @@ syscallentry: // (15*8)(%rsp) // ureg.type // (16*8)(%rsp) // ureg.error + /* prepare m-> and up-> for the kernel */ + movq %gs:0, %r15 + movq 16(%r15), %r14 + movq %rsp, %rsi /* Ureg* */ // system call number is in %rax, as per linux. movq %rax, %rdi