Skip to content

Commit

Permalink
kernel: skip return address of <autogenerated> function wrapper in tr…
Browse files Browse the repository at this point in the history
…apret before IRET instruction

NOTE: this commit will ONLY work for Go 1.17, it is not backwards
compatible with Go 1.16. We should probably find a way to handle
both (e.g. build tags or a general approach).

To give context, an <autogenerated> function wrapper is generated
in Go 1.17 for kernel.trapret.

	Dump of assembler code for function github.com/icexin/eggos/kernel.trapret<autogenerated>:
	=> 0x00000000002bbf60 <+0>:   call   0x2b9e40 <github.com/icexin/eggos/kernel.trapret>
		0x00000000002bbf65 <+5>:   xorps  %xmm15,%xmm15
		0x00000000002bbf69 <+9>:   mov    %fs:0xfffffffffffffff8,%r14
		0x00000000002bbf72 <+18>:  ret

In this autogenerated function, a call to the real kernel.trapret
is made, which places a return address on the stack. However, the
trapret function will use IRET to return, so to handle this case,
we skip the return address pushed onto the stack by the autogenerated
function by adding 8 (ptr size) to rsp.

We should definitely try to find a better way to handle this situation
as it seems very fragile and may break in the future if Go changes
their autogenerated functions.

So, once more, this is mostly to get intuition into the problem domain,
it is not meant as a solution that should be merged into eggos.
  • Loading branch information
mewmew committed Feb 12, 2022
1 parent 8cdbd4f commit ef26ff1
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions kernel/trap.s
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,13 @@ TEXT ·trapret(SB), NOSPLIT, $0

ADDQ $16, SP // skip trapno and errcode

ADDQ $8, SP // NOTE: skip return address of <autogenerated> function (added in Go 1.17).

// Dump of assembler code for function github.com/icexin/eggos/kernel.trapret<autogenerated>:
// => 0x00000000002bbf60 <+0>: call 0x2b9e40 <github.com/icexin/eggos/kernel.trapret>
// 0x00000000002bbf65 <+5>: xorps %xmm15,%xmm15
// 0x00000000002bbf69 <+9>: mov %fs:0xfffffffffffffff8,%r14
// 0x00000000002bbf72 <+18>: ret

IRETQ

0 comments on commit ef26ff1

Please sign in to comment.