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() and swapcontext() are written in assembler since it has to
2N/A * capture the correct machine state of the caller, including
2N/A * the registers: %edi, %esi and %ebx.
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
2N/A#define GETCONTEXT_IMPL \
2N/A movl 4(%esp), %eax; /* %eax <-- first arg: ucp */ \
2N/A pushl %eax; /* push ucp for system call */ \
2N/A call __getcontext; /* call getcontext: syscall */ \
2N/A addl $4, %esp; /* pop arg */ \
2N/A andl %eax, %eax; /* if (err_ret_from_syscall) */ \
2N/A je 1f; \
2N/A ret; /* then return */ \
2N/A1: \
2N/A movl 4(%esp), %eax; /* recompute first arg */ \
2N/A /* \
2N/A * fix up %esp and %eip \
2N/A */ \
2N/A leal UC_MCONTEXT_GREGS (%eax), %edx; \
2N/A /* %edx <-- &ucp->uc_mcontext.gregs */ \
2N/A movl 0(%esp), %eax; /* read return PC from stack */ \
2N/A movl %eax, EIP_OFF (%edx); \
2N/A /* store ret PC in EIP of env var */ \
2N/A leal 4(%esp), %eax; /* get caller's sp at time of call */ \
2N/A movl %eax, UESP_OFF (%edx); \
2N/A /* store the sp into UESP of env var */ \
2N/A xorl %eax, %eax; /* return 0 */ \
2N/A movl %eax, EAX_OFF (%edx); \
2N/A /* getcontext returns 0 after a setcontext */
2N/A
2N/A/*
2N/A * getcontext(ucontext_t *ucp)
2N/A */
2N/A ENTRY(getcontext)
2N/A GETCONTEXT_IMPL
2N/A ret
2N/A SET_SIZE(getcontext)
2N/A
2N/A
2N/A/*
2N/A * swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
2N/A */
2N/A ENTRY(swapcontext)
2N/A GETCONTEXT_IMPL
2N/A / call setcontext
2N/A movl 8(%esp), %eax /* %eax <-- second arg: ucp */
2N/A pushl %eax /* push ucp for setcontext */
2N/A call setcontext
2N/A addl $4, %esp /* pop arg: just in case */
2N/A ret
2N/A SET_SIZE(swapcontext)