2018-02-01 01:26:34 +00:00
|
|
|
/*
|
2020-03-29 15:04:03 +00:00
|
|
|
* Copyright (c) 2018, 2020 Ariadne Conill <ariadne@dereferenced.org>
|
2018-02-01 01:26:34 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
|
2020-12-06 09:04:22 +00:00
|
|
|
ALIAS(swapcontext, libucontext_swapcontext)
|
2021-01-08 09:30:50 +00:00
|
|
|
ALIAS(__swapcontext, libucontext_swapcontext)
|
2020-03-29 15:04:03 +00:00
|
|
|
|
2020-12-06 09:04:22 +00:00
|
|
|
FUNC(libucontext_swapcontext)
|
2018-02-01 01:26:34 +00:00
|
|
|
/* load address of the ucontext structure */
|
|
|
|
movl 4(%esp), %eax
|
|
|
|
|
|
|
|
/* EAX is not a preserved register */
|
2020-03-29 15:04:03 +00:00
|
|
|
movl $0, REG_OFFSET(REG_EAX)(%eax)
|
2018-02-01 01:26:34 +00:00
|
|
|
|
|
|
|
/* copy all of the current registers into the ucontext structure */
|
2020-03-29 15:04:03 +00:00
|
|
|
movl %ecx, REG_OFFSET(REG_ECX)(%eax)
|
|
|
|
movl %ebx, REG_OFFSET(REG_EBX)(%eax)
|
|
|
|
movl %edx, REG_OFFSET(REG_EDX)(%eax)
|
|
|
|
movl %edi, REG_OFFSET(REG_EDI)(%eax)
|
|
|
|
movl %esi, REG_OFFSET(REG_ESI)(%eax)
|
|
|
|
movl %ebp, REG_OFFSET(REG_EBP)(%eax)
|
2018-02-01 01:26:34 +00:00
|
|
|
|
|
|
|
/* the first argument on the stack is the jump target (%eip), so we store it in the EIP
|
|
|
|
register in the ucontext structure. */
|
|
|
|
movl (%esp), %ecx
|
2020-03-29 15:04:03 +00:00
|
|
|
movl %ecx, REG_OFFSET(REG_EIP)(%eax)
|
2018-02-01 01:26:34 +00:00
|
|
|
|
|
|
|
/* take the stack pointer address (%esp) offsetting by 4 to skip over the jump target. */
|
|
|
|
leal 4(%esp), %ecx
|
2020-03-29 15:04:03 +00:00
|
|
|
movl %ecx, REG_OFFSET(REG_ESP)(%eax)
|
2018-02-01 01:26:34 +00:00
|
|
|
|
|
|
|
/* finally, save the FS segment register */
|
|
|
|
xorl %ecx, %ecx
|
|
|
|
movw %fs, %cx
|
2020-03-29 15:04:03 +00:00
|
|
|
movl %ecx, REG_OFFSET(REG_FS)(%eax)
|
2018-02-01 01:26:34 +00:00
|
|
|
|
|
|
|
/* load address of the ucontext structure */
|
|
|
|
movl 8(%esp), %eax
|
|
|
|
|
|
|
|
/* set up the FS segment register */
|
2020-03-29 15:04:03 +00:00
|
|
|
movl REG_OFFSET(REG_FS)(%eax), %ecx
|
2018-02-01 01:26:34 +00:00
|
|
|
movw %cx, %fs
|
|
|
|
|
|
|
|
/* fetch the new EIP */
|
2020-03-29 15:04:03 +00:00
|
|
|
movl REG_OFFSET(REG_EIP)(%eax), %ecx
|
2018-02-01 01:26:34 +00:00
|
|
|
|
|
|
|
/* set up the new stack pointer */
|
2020-03-29 15:04:03 +00:00
|
|
|
movl REG_OFFSET(REG_ESP)(%eax), %esp
|
2018-02-01 01:26:34 +00:00
|
|
|
|
|
|
|
/* push the return address onto the stack */
|
|
|
|
pushl %ecx
|
|
|
|
|
|
|
|
/* set all of the registers */
|
2020-03-29 15:04:03 +00:00
|
|
|
movl REG_OFFSET(REG_EBX)(%eax), %ebx
|
|
|
|
movl REG_OFFSET(REG_ECX)(%eax), %ecx
|
|
|
|
movl REG_OFFSET(REG_EDX)(%eax), %edx
|
|
|
|
movl REG_OFFSET(REG_EBP)(%eax), %ebp
|
|
|
|
movl REG_OFFSET(REG_EDI)(%eax), %edi
|
|
|
|
movl REG_OFFSET(REG_ESI)(%eax), %esi
|
|
|
|
movl REG_OFFSET(REG_EAX)(%eax), %eax
|
2018-02-01 01:26:34 +00:00
|
|
|
|
|
|
|
ret
|
2020-12-06 09:04:22 +00:00
|
|
|
END(libucontext_swapcontext)
|