forked from ariadne/libucontext
x86_64: modernize implementation
parent
2411cdfb54
commit
e00902b5b1
|
@ -0,0 +1,36 @@
|
||||||
|
#ifndef __ARCH_X86_64_DEFS_H
|
||||||
|
#define __ARCH_X86_64_DEFS_H
|
||||||
|
|
||||||
|
#ifndef _GNU_SOURCE
|
||||||
|
# define REG_R8 (0)
|
||||||
|
# define REG_R9 (1)
|
||||||
|
# define REG_R10 (2)
|
||||||
|
# define REG_R11 (3)
|
||||||
|
# define REG_R12 (4)
|
||||||
|
# define REG_R13 (5)
|
||||||
|
# define REG_R14 (6)
|
||||||
|
# define REG_R15 (7)
|
||||||
|
# define REG_RDI (8)
|
||||||
|
# define REG_RSI (9)
|
||||||
|
# define REG_RBP (10)
|
||||||
|
# define REG_RBX (11)
|
||||||
|
# define REG_RDX (12)
|
||||||
|
# define REG_RAX (13)
|
||||||
|
# define REG_RCX (14)
|
||||||
|
# define REG_RSP (15)
|
||||||
|
# define REG_RIP (16)
|
||||||
|
# define REG_EFL (17)
|
||||||
|
# define REG_CSGSFS (18)
|
||||||
|
# define REG_ERR (19)
|
||||||
|
# define REG_TRAPNO (20)
|
||||||
|
# define REG_OLDMASK (21)
|
||||||
|
# define REG_CR2 (22)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define MCONTEXT_GREGS (40)
|
||||||
|
|
||||||
|
#define SZ_REG (8)
|
||||||
|
|
||||||
|
#include "common-defs.h"
|
||||||
|
|
||||||
|
#endif
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018 Ariadne Conill <ariadne@dereferenced.org>
|
* Copyright (c) 2018, 2020 Ariadne Conill <ariadne@dereferenced.org>
|
||||||
*
|
*
|
||||||
* Permission to use, copy, modify, and/or distribute this software for any
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
@ -10,39 +10,39 @@
|
||||||
* from the use of this software.
|
* from the use of this software.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
.globl __getcontext;
|
#include "defs.h"
|
||||||
__getcontext:
|
|
||||||
|
ALIAS(getcontext, __getcontext)
|
||||||
|
|
||||||
|
FUNC(__getcontext)
|
||||||
/* copy all of the current registers into the ucontext structure */
|
/* copy all of the current registers into the ucontext structure */
|
||||||
movq %r8, 40(%rdi)
|
movq %r8, REG_OFFSET(REG_R8)(%rdi)
|
||||||
movq %r9, 48(%rdi)
|
movq %r9, REG_OFFSET(REG_R9)(%rdi)
|
||||||
movq %r10, 56(%rdi)
|
movq %r10, REG_OFFSET(REG_R10)(%rdi)
|
||||||
movq %r11, 64(%rdi)
|
movq %r11, REG_OFFSET(REG_R11)(%rdi)
|
||||||
movq %r12, 72(%rdi)
|
movq %r12, REG_OFFSET(REG_R12)(%rdi)
|
||||||
movq %r13, 80(%rdi)
|
movq %r13, REG_OFFSET(REG_R13)(%rdi)
|
||||||
movq %r14, 88(%rdi)
|
movq %r14, REG_OFFSET(REG_R14)(%rdi)
|
||||||
movq %r15, 96(%rdi)
|
movq %r15, REG_OFFSET(REG_R15)(%rdi)
|
||||||
movq %rdi, 104(%rdi)
|
movq %rdi, REG_OFFSET(REG_RDI)(%rdi)
|
||||||
movq %rsi, 112(%rdi)
|
movq %rsi, REG_OFFSET(REG_RSI)(%rdi)
|
||||||
movq %rbp, 120(%rdi)
|
movq %rbp, REG_OFFSET(REG_RBP)(%rdi)
|
||||||
movq %rbx, 128(%rdi)
|
movq %rbx, REG_OFFSET(REG_RBX)(%rdi)
|
||||||
movq %rdx, 136(%rdi)
|
movq %rdx, REG_OFFSET(REG_RDX)(%rdi)
|
||||||
movq $1, 144(%rdi) /* $1 is %rax */
|
movq %rax, REG_OFFSET(REG_RAX)(%rdi)
|
||||||
movq %rcx, 152(%rdi)
|
movq %rcx, REG_OFFSET(REG_RCX)(%rdi)
|
||||||
|
|
||||||
/* the first argument on the stack is the jump target (%rip), so we store it in the RIP
|
/* the first argument on the stack is the jump target (%rip), so we store it in the RIP
|
||||||
register in the ucontext structure. */
|
register in the ucontext structure. */
|
||||||
movq (%rsp), %rcx
|
movq (%rsp), %rcx
|
||||||
movq %rcx, 168(%rdi)
|
movq %rcx, REG_OFFSET(REG_RIP)(%rdi)
|
||||||
|
|
||||||
/* finally take the stack pointer address (%rsp) offsetting by 8 to skip over the jump
|
/* finally take the stack pointer address (%rsp) offsetting by 8 to skip over the jump
|
||||||
target. */
|
target. */
|
||||||
leaq 8(%rsp), %rcx
|
leaq 8(%rsp), %rcx
|
||||||
movq %rcx, 160(%rdi)
|
movq %rcx, REG_OFFSET(REG_RSP)(%rdi)
|
||||||
|
|
||||||
/* we're all done here, return 0 */
|
/* we're all done here, return 0 */
|
||||||
xorl %eax, %eax
|
xorl %eax, %eax
|
||||||
ret
|
ret
|
||||||
|
END(__getcontext)
|
||||||
|
|
||||||
.weak getcontext;
|
|
||||||
getcontext = __getcontext;
|
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "defs.h"
|
||||||
|
|
||||||
extern void __start_context(void);
|
extern void __start_context(void);
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018 Ariadne Conill <ariadne@dereferenced.org>
|
* Copyright (c) 2018, 2020 Ariadne Conill <ariadne@dereferenced.org>
|
||||||
*
|
*
|
||||||
* Permission to use, copy, modify, and/or distribute this software for any
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
@ -10,35 +10,36 @@
|
||||||
* from the use of this software.
|
* from the use of this software.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
.globl __setcontext;
|
#include "defs.h"
|
||||||
__setcontext:
|
|
||||||
|
ALIAS(setcontext, __setcontext)
|
||||||
|
|
||||||
|
FUNC(__setcontext)
|
||||||
/* set all of the registers */
|
/* set all of the registers */
|
||||||
movq 40(%rdi), %r8
|
movq REG_OFFSET(REG_R8)(%rdi), %r8
|
||||||
movq 48(%rdi), %r9
|
movq REG_OFFSET(REG_R9)(%rdi), %r9
|
||||||
movq 56(%rdi), %r10
|
movq REG_OFFSET(REG_R10)(%rdi), %r10
|
||||||
movq 64(%rdi), %r11
|
movq REG_OFFSET(REG_R11)(%rdi), %r11
|
||||||
movq 72(%rdi), %r12
|
movq REG_OFFSET(REG_R12)(%rdi), %r12
|
||||||
movq 80(%rdi), %r13
|
movq REG_OFFSET(REG_R13)(%rdi), %r13
|
||||||
movq 88(%rdi), %r14
|
movq REG_OFFSET(REG_R14)(%rdi), %r14
|
||||||
movq 96(%rdi), %r15
|
movq REG_OFFSET(REG_R15)(%rdi), %r15
|
||||||
movq 112(%rdi), %rsi
|
movq REG_OFFSET(REG_RSI)(%rdi), %rsi
|
||||||
movq 120(%rdi), %rbp
|
movq REG_OFFSET(REG_RBP)(%rdi), %rbp
|
||||||
movq 128(%rdi), %rbx
|
movq REG_OFFSET(REG_RBX)(%rdi), %rbx
|
||||||
movq 136(%rdi), %rdx
|
movq REG_OFFSET(REG_RDX)(%rdi), %rdx
|
||||||
movq 144(%rdi), %rax
|
movq REG_OFFSET(REG_RAX)(%rdi), %rax
|
||||||
movq 152(%rdi), %rcx
|
movq REG_OFFSET(REG_RCX)(%rdi), %rcx
|
||||||
movq 160(%rdi), %rsp
|
movq REG_OFFSET(REG_RSP)(%rdi), %rsp
|
||||||
|
|
||||||
/* set the jump target by pushing it to the stack.
|
/* set the jump target by pushing it to the stack.
|
||||||
ret will pop the new %rip from the stack, causing us to jump there. */
|
ret will pop the new %rip from the stack, causing us to jump there. */
|
||||||
pushq 168(%rdi)
|
pushq REG_OFFSET(REG_RIP)(%rdi)
|
||||||
|
|
||||||
/* finally, set %rdi correctly. */
|
/* finally, set %rdi correctly. */
|
||||||
movq 104(%rdi), %rdi
|
movq REG_OFFSET(REG_RDI)(%rdi), %rdi
|
||||||
|
|
||||||
/* we're all done here, return 0 */
|
/* we're all done here, return 0 */
|
||||||
xorl %eax, %eax
|
xorl %eax, %eax
|
||||||
ret
|
ret
|
||||||
|
END(__setcontext)
|
||||||
.weak setcontext;
|
|
||||||
setcontext = __setcontext;
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018 Ariadne Conill <ariadne@dereferenced.org>
|
* Copyright (c) 2018, 2020 Ariadne Conill <ariadne@dereferenced.org>
|
||||||
*
|
*
|
||||||
* Permission to use, copy, modify, and/or distribute this software for any
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
@ -10,8 +10,9 @@
|
||||||
* from the use of this software.
|
* from the use of this software.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
.globl __start_context;
|
#include "defs.h"
|
||||||
__start_context:
|
|
||||||
|
FUNC(__start_context)
|
||||||
/* get the proper context into position and test for NULL */
|
/* get the proper context into position and test for NULL */
|
||||||
movq %rbx, %rsp
|
movq %rbx, %rsp
|
||||||
movq (%rsp), %rdi
|
movq (%rsp), %rdi
|
||||||
|
@ -30,3 +31,4 @@ no_linked_context:
|
||||||
|
|
||||||
/* something is really hosed, call hlt to force termination */
|
/* something is really hosed, call hlt to force termination */
|
||||||
hlt
|
hlt
|
||||||
|
END(__start_context)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018 Ariadne Conill <ariadne@dereferenced.org>
|
* Copyright (c) 2018, 2020 Ariadne Conill <ariadne@dereferenced.org>
|
||||||
*
|
*
|
||||||
* Permission to use, copy, modify, and/or distribute this software for any
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
@ -10,65 +10,65 @@
|
||||||
* from the use of this software.
|
* from the use of this software.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
.globl __swapcontext;
|
#include "defs.h"
|
||||||
__swapcontext:
|
|
||||||
|
ALIAS(swapcontext, __swapcontext)
|
||||||
|
|
||||||
|
FUNC(__swapcontext)
|
||||||
/* copy all of the current registers into the ucontext structure pointed by
|
/* copy all of the current registers into the ucontext structure pointed by
|
||||||
the first argument */
|
the first argument */
|
||||||
movq %r8, 40(%rdi)
|
movq %r8, REG_OFFSET(REG_R8)(%rdi)
|
||||||
movq %r9, 48(%rdi)
|
movq %r9, REG_OFFSET(REG_R9)(%rdi)
|
||||||
movq %r10, 56(%rdi)
|
movq %r10, REG_OFFSET(REG_R10)(%rdi)
|
||||||
movq %r11, 64(%rdi)
|
movq %r11, REG_OFFSET(REG_R11)(%rdi)
|
||||||
movq %r12, 72(%rdi)
|
movq %r12, REG_OFFSET(REG_R12)(%rdi)
|
||||||
movq %r13, 80(%rdi)
|
movq %r13, REG_OFFSET(REG_R13)(%rdi)
|
||||||
movq %r14, 88(%rdi)
|
movq %r14, REG_OFFSET(REG_R14)(%rdi)
|
||||||
movq %r15, 96(%rdi)
|
movq %r15, REG_OFFSET(REG_R15)(%rdi)
|
||||||
movq %rdi, 104(%rdi)
|
movq %rdi, REG_OFFSET(REG_RDI)(%rdi)
|
||||||
movq %rsi, 112(%rdi)
|
movq %rsi, REG_OFFSET(REG_RSI)(%rdi)
|
||||||
movq %rbp, 120(%rdi)
|
movq %rbp, REG_OFFSET(REG_RBP)(%rdi)
|
||||||
movq %rbx, 128(%rdi)
|
movq %rbx, REG_OFFSET(REG_RBX)(%rdi)
|
||||||
movq %rdx, 136(%rdi)
|
movq %rdx, REG_OFFSET(REG_RDX)(%rdi)
|
||||||
movq $1, 144(%rdi) /* $1 is %rax */
|
movq %rax, REG_OFFSET(REG_RAX)(%rdi)
|
||||||
movq %rcx, 152(%rdi)
|
movq %rcx, REG_OFFSET(REG_RCX)(%rdi)
|
||||||
|
|
||||||
/* the first argument on the stack is the jump target (%rip), so we store it in the RIP
|
/* the first argument on the stack is the jump target (%rip), so we store it in the RIP
|
||||||
register in the ucontext structure. */
|
register in the ucontext structure. */
|
||||||
movq (%rsp), %rcx
|
movq (%rsp), %rcx
|
||||||
movq %rcx, 168(%rdi)
|
movq %rcx, REG_OFFSET(REG_RIP)(%rdi)
|
||||||
|
|
||||||
/* finally take the stack pointer address (%rsp) offsetting by 8 to skip over the jump
|
/* finally take the stack pointer address (%rsp) offsetting by 8 to skip over the jump
|
||||||
target. */
|
target. */
|
||||||
leaq 8(%rsp), %rcx
|
leaq 8(%rsp), %rcx
|
||||||
movq %rcx, 160(%rdi)
|
movq %rcx, REG_OFFSET(REG_RSP)(%rdi)
|
||||||
|
|
||||||
/* set all of the registers to their new states, stored in the second
|
/* set all of the registers to their new states, stored in the second
|
||||||
ucontext structure */
|
ucontext structure */
|
||||||
movq 40(%rsi), %r8
|
movq REG_OFFSET(REG_R8)(%rsi), %r8
|
||||||
movq 48(%rsi), %r9
|
movq REG_OFFSET(REG_R9)(%rsi), %r9
|
||||||
movq 56(%rsi), %r10
|
movq REG_OFFSET(REG_R10)(%rsi), %r10
|
||||||
movq 64(%rsi), %r11
|
movq REG_OFFSET(REG_R11)(%rsi), %r11
|
||||||
movq 72(%rsi), %r12
|
movq REG_OFFSET(REG_R12)(%rsi), %r12
|
||||||
movq 80(%rsi), %r13
|
movq REG_OFFSET(REG_R13)(%rsi), %r13
|
||||||
movq 88(%rsi), %r14
|
movq REG_OFFSET(REG_R14)(%rsi), %r14
|
||||||
movq 96(%rsi), %r15
|
movq REG_OFFSET(REG_R15)(%rsi), %r15
|
||||||
movq 104(%rsi), %rdi
|
movq REG_OFFSET(REG_RDI)(%rsi), %rdi
|
||||||
movq 120(%rsi), %rbp
|
movq REG_OFFSET(REG_RBP)(%rsi), %rbp
|
||||||
movq 128(%rsi), %rbx
|
movq REG_OFFSET(REG_RBX)(%rsi), %rbx
|
||||||
movq 136(%rsi), %rdx
|
movq REG_OFFSET(REG_RDX)(%rsi), %rdx
|
||||||
movq 144(%rsi), %rax
|
movq REG_OFFSET(REG_RAX)(%rsi), %rax
|
||||||
movq 152(%rsi), %rcx
|
movq REG_OFFSET(REG_RCX)(%rsi), %rcx
|
||||||
movq 160(%rsi), %rsp
|
movq REG_OFFSET(REG_RSP)(%rsi), %rsp
|
||||||
|
|
||||||
/* set the jump target by pushing it to the stack.
|
/* set the jump target by pushing it to the stack.
|
||||||
ret will pop the new %rip from the stack, causing us to jump there. */
|
ret will pop the new %rip from the stack, causing us to jump there. */
|
||||||
pushq 168(%rsi)
|
pushq REG_OFFSET(REG_RIP)(%rsi)
|
||||||
|
|
||||||
/* finally, set %rsi correctly since we do not need it anymore. */
|
/* finally, set %rsi correctly since we do not need it anymore. */
|
||||||
movq 112(%rsi), %rsi
|
movq REG_OFFSET(REG_RSI)(%rsi), %rsi
|
||||||
|
|
||||||
/* we're all done here, return 0 */
|
/* we're all done here, return 0 */
|
||||||
xorl %eax, %eax
|
xorl %eax, %eax
|
||||||
ret
|
ret
|
||||||
|
END(__swapcontext)
|
||||||
|
|
||||||
.weak swapcontext;
|
|
||||||
swapcontext = __swapcontext;
|
|
||||||
|
|
Loading…
Reference in New Issue