x86: modernize

master
Ariadne Conill 2020-03-29 15:04:03 +00:00
parent d8cfe83e34
commit d0ccf2f96f
6 changed files with 83 additions and 79 deletions

View File

@ -1,18 +1,26 @@
#ifndef __ARCH_X86_DEFS_H
#define __ARCH_X86_DEFS_H
#define OFFSET_REG_GS 20
#define OFFSET_REG_FS 24
#define OFFSET_REG_ES 28
#define OFFSET_REG_DS 32
#define OFFSET_REG_EDI 36
#define OFFSET_REG_ESI 40
#define OFFSET_REG_EBP 44
#define OFFSET_REG_ESP 48
#define OFFSET_REG_EBX 52
#define OFFSET_REG_EDX 56
#define OFFSET_REG_ECX 60
#define OFFSET_REG_EAX 64
#define OFFSET_REG_EIP 76
#ifndef REG_GS
# define REG_GS (0)
# define REG_FS (1)
# define REG_ES (2)
# define REG_DS (3)
# define REG_EDI (4)
# define REG_ESI (5)
# define REG_EBP (6)
# define REG_ESP (7)
# define REG_EBX (8)
# define REG_EDX (9)
# define REG_ECX (10)
# define REG_EAX (11)
# define REG_EIP (14)
#endif
#define REG_SZ (4)
#define MCONTEXT_GREGS (20)
#include "common-defs.h"
#endif

View File

@ -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
* purpose with or without fee is hereby granted, provided that the above
@ -12,40 +12,38 @@
#include "defs.h"
.globl __getcontext;
__getcontext:
ALIAS(getcontext, __getcontext)
FUNC(__getcontext)
/* load address of the ucontext structure */
movl 4(%esp), %eax
/* EAX is not a preserved register */
movl $0, OFFSET_REG_EAX(%eax)
movl $0, REG_OFFSET(REG_EAX)(%eax)
/* copy all of the current registers into the ucontext structure */
movl %ecx, OFFSET_REG_ECX(%eax)
movl %ebx, OFFSET_REG_EBX(%eax)
movl %edx, OFFSET_REG_EDX(%eax)
movl %edi, OFFSET_REG_EDI(%eax)
movl %esi, OFFSET_REG_ESI(%eax)
movl %ebp, OFFSET_REG_EBP(%eax)
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)
/* 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
movl %ecx, OFFSET_REG_EIP(%eax)
movl %ecx, REG_OFFSET(REG_EIP)(%eax)
/* take the stack pointer address (%esp) offsetting by 4 to skip over the jump target. */
leal 4(%esp), %ecx
movl %ecx, OFFSET_REG_ESP(%eax)
movl %ecx, REG_OFFSET(REG_ESP)(%eax)
/* finally, save the FS segment register */
xorl %ecx, %ecx
movw %fs, %cx
movl %ecx, OFFSET_REG_FS(%eax)
movl %ecx, REG_OFFSET(REG_FS)(%eax)
/* we're all done here, return 0 */
xorl %eax, %eax
ret
.weak getcontext;
getcontext = __getcontext;
END(__getcontext)

View File

@ -41,7 +41,6 @@ __makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
argp = sp;
*argp++ = (uintptr_t) &__start_context;
*argp++ = (uintptr_t) ucp->uc_link;
va_start(va, argc);
@ -49,6 +48,8 @@ __makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
*argp++ = va_arg (va, greg_t);
va_end(va);
*argp++ = (uintptr_t) ucp->uc_link;
}

View File

@ -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
* purpose with or without fee is hereby granted, provided that the above
@ -12,35 +12,33 @@
#include "defs.h"
.globl __setcontext;
__setcontext:
ALIAS(setcontext, __setcontext)
FUNC(__setcontext)
/* load address of the ucontext structure */
movl 4(%esp), %eax
/* set up the FS segment register */
movl OFFSET_REG_FS(%eax), %ecx
movl REG_OFFSET(REG_FS)(%eax), %ecx
movw %cx, %fs
/* fetch the new EIP */
movl OFFSET_REG_EIP(%eax), %ecx
movl REG_OFFSET(REG_EIP)(%eax), %ecx
/* set up the new stack pointer */
movl OFFSET_REG_ESP(%eax), %esp
movl REG_OFFSET(REG_ESP)(%eax), %esp
/* push the return address onto the stack */
pushl %ecx
/* set all of the registers */
movl OFFSET_REG_EBX(%eax), %ebx
movl OFFSET_REG_ECX(%eax), %ecx
movl OFFSET_REG_EDX(%eax), %edx
movl OFFSET_REG_EBP(%eax), %ebp
movl OFFSET_REG_EDI(%eax), %edi
movl OFFSET_REG_ESI(%eax), %esi
movl OFFSET_REG_EAX(%eax), %eax
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
ret
.weak setcontext;
setcontext = __setcontext;
END(__setcontext)

View File

@ -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
* purpose with or without fee is hereby granted, provided that the above
@ -10,8 +10,9 @@
* from the use of this software.
*/
.globl __start_context;
__start_context:
#include "defs.h"
FUNC(__start_context)
/* get the proper context into position and test for NULL */
leal (%esp,%ebx,4), %esp
cmpl $0, (%esp)
@ -34,7 +35,7 @@ no_linked_context:
/* something is really hosed, call hlt to force termination */
hlt
__i686.get_pc_thunk.bx:
mov (%esp), %ebx
ret
END(__start_context)

View File

@ -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
* purpose with or without fee is hereby granted, provided that the above
@ -12,63 +12,61 @@
#include "defs.h"
.globl __swapcontext;
__swapcontext:
ALIAS(swapcontext, __swapcontext)
FUNC(__swapcontext)
/* load address of the ucontext structure */
movl 4(%esp), %eax
/* EAX is not a preserved register */
movl $0, OFFSET_REG_EAX(%eax)
movl $0, REG_OFFSET(REG_EAX)(%eax)
/* copy all of the current registers into the ucontext structure */
movl %ecx, OFFSET_REG_ECX(%eax)
movl %ebx, OFFSET_REG_EBX(%eax)
movl %edx, OFFSET_REG_EDX(%eax)
movl %edi, OFFSET_REG_EDI(%eax)
movl %esi, OFFSET_REG_ESI(%eax)
movl %ebp, OFFSET_REG_EBP(%eax)
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)
/* 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
movl %ecx, OFFSET_REG_EIP(%eax)
movl %ecx, REG_OFFSET(REG_EIP)(%eax)
/* take the stack pointer address (%esp) offsetting by 4 to skip over the jump target. */
leal 4(%esp), %ecx
movl %ecx, OFFSET_REG_ESP(%eax)
movl %ecx, REG_OFFSET(REG_ESP)(%eax)
/* finally, save the FS segment register */
xorl %ecx, %ecx
movw %fs, %cx
movl %ecx, OFFSET_REG_FS(%eax)
movl %ecx, REG_OFFSET(REG_FS)(%eax)
/* load address of the ucontext structure */
movl 8(%esp), %eax
/* set up the FS segment register */
movl OFFSET_REG_FS(%eax), %ecx
movl REG_OFFSET(REG_FS)(%eax), %ecx
movw %cx, %fs
/* fetch the new EIP */
movl OFFSET_REG_EIP(%eax), %ecx
movl REG_OFFSET(REG_EIP)(%eax), %ecx
/* set up the new stack pointer */
movl OFFSET_REG_ESP(%eax), %esp
movl REG_OFFSET(REG_ESP)(%eax), %esp
/* push the return address onto the stack */
pushl %ecx
/* set all of the registers */
movl OFFSET_REG_EBX(%eax), %ebx
movl OFFSET_REG_ECX(%eax), %ecx
movl OFFSET_REG_EDX(%eax), %edx
movl OFFSET_REG_EBP(%eax), %ebp
movl OFFSET_REG_EDI(%eax), %edi
movl OFFSET_REG_ESI(%eax), %esi
movl OFFSET_REG_EAX(%eax), %eax
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
ret
.weak swapcontext;
swapcontext = __swapcontext;
END(__swapcontext)