2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A .file "getcontext.s"
2N/A
2N/A#include <sys/asm_linkage.h>
2N/A
2N/A ANSI_PRAGMA_WEAK(getcontext,function)
2N/A ANSI_PRAGMA_WEAK(swapcontext,function)
2N/A
2N/A#include "SYS.h"
2N/A#include <../assym.h>
2N/A
2N/A/*
2N/A * getcontext() is written in assembler since it has to capture the correct
2N/A * machine state of the calle.
2N/A *
2N/A * As swapcontext() is actually equivalent to getcontext() + setcontext(),
2N/A * swapcontext() shares the most code with getcontext().
2N/A */
2N/A
2N/A#define GETCONTEXT_IMPL(offset) \
2N/A pushq %rdi; /* preserve the ucontext_t pointer */ \
2N/A call __getcontext; \
2N/A /* call getcontext: syscall */ \
2N/A popq %rdx; \
2N/A andl %eax, %eax; /* if (error_return_from_syscall) */ \
2N/A je 1f; \
2N/A addq $offset, %rsp; \
2N/A ret; /* then just return */ \
2N/A1: \
2N/A /* \
2N/A * fix up %rsp and %rip \
2N/A */ \
2N/A addq $UC_MCONTEXT_GREGS, %rdx; \
2N/A /* &ucp->uc_mcontext.gregs */ \
2N/A movq offset+0(%rsp), %rax; \
2N/A /* read return PC from stack */ \
2N/A movq %rax, RIP_OFF (%rdx); \
2N/A /* store ret PC in EIP of env var */ \
2N/A leaq offset+8(%rsp), %rax; \
2N/A /* get caller's sp at time of call */ \
2N/A movq %rax, RSP_OFF (%rdx); \
2N/A /* store the sp into UESP of env var */ \
2N/A xorq %rax, %rax; /* return 0 */ \
2N/A movq %rax, RAX_OFF (%rdx); \
2N/A /* getcontext returns 0 after setcontext */
2N/A
2N/A/*
2N/A * getcontext(ucontext_t *ucp)
2N/A */
2N/A
2N/A ENTRY(getcontext)
2N/A GETCONTEXT_IMPL(0)
2N/A ret
2N/A SET_SIZE(getcontext)
2N/A
2N/A/*
2N/A * swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
2N/A */
2N/A
2N/A ENTRY(swapcontext)
2N/A pushq %rsi /* preserve the 2nd argument */
2N/A
2N/A GETCONTEXT_IMPL(8)
2N/A
2N/A /* call setcontext */
2N/A popq %rdi
2N/A call setcontext
2N/A ret
2N/A SET_SIZE(swapcontext)