10Sstevel@tonic-gate/* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 56515Sraf * Common Development and Distribution License (the "License"). 66515Sraf * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 216515Sraf 220Sstevel@tonic-gate/* 236515Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 27*7298SMark.J.Nelson@Sun.COM .file "getcontext.s" 280Sstevel@tonic-gate 290Sstevel@tonic-gate#include <sys/asm_linkage.h> 300Sstevel@tonic-gate 310Sstevel@tonic-gate ANSI_PRAGMA_WEAK(getcontext,function) 320Sstevel@tonic-gate ANSI_PRAGMA_WEAK(swapcontext,function) 330Sstevel@tonic-gate 340Sstevel@tonic-gate#include "SYS.h" 350Sstevel@tonic-gate#include <assym.h> 360Sstevel@tonic-gate 370Sstevel@tonic-gate/* 380Sstevel@tonic-gate * getcontext() and swapcontext() are written in assembler since it has to 390Sstevel@tonic-gate * capture the correct machine state of the caller, including 400Sstevel@tonic-gate * the registers: %edi, %esi and %ebx. 410Sstevel@tonic-gate * 420Sstevel@tonic-gate * As swapcontext() is actually equivalent to getcontext() + setcontext(), 430Sstevel@tonic-gate * swapcontext() shares the most code with getcontext(). 440Sstevel@tonic-gate */ 450Sstevel@tonic-gate 460Sstevel@tonic-gate 470Sstevel@tonic-gate#define GETCONTEXT_IMPL \ 480Sstevel@tonic-gate movl 4(%esp), %eax; /* %eax <-- first arg: ucp */ \ 490Sstevel@tonic-gate pushl %eax; /* push ucp for system call */ \ 506515Sraf call __getcontext; /* call getcontext: syscall */ \ 510Sstevel@tonic-gate addl $4, %esp; /* pop arg */ \ 520Sstevel@tonic-gate andl %eax, %eax; /* if (err_ret_from_syscall) */ \ 530Sstevel@tonic-gate je 1f; \ 540Sstevel@tonic-gate ret; /* then return */ \ 550Sstevel@tonic-gate1: \ 560Sstevel@tonic-gate movl 4(%esp), %eax; /* recompute first arg */ \ 570Sstevel@tonic-gate /* \ 580Sstevel@tonic-gate * fix up %esp and %eip \ 590Sstevel@tonic-gate */ \ 600Sstevel@tonic-gate leal UC_MCONTEXT_GREGS (%eax), %edx; \ 610Sstevel@tonic-gate /* %edx <-- &ucp->uc_mcontext.gregs */ \ 620Sstevel@tonic-gate movl 0(%esp), %eax; /* read return PC from stack */ \ 630Sstevel@tonic-gate movl %eax, EIP_OFF (%edx); \ 640Sstevel@tonic-gate /* store ret PC in EIP of env var */ \ 650Sstevel@tonic-gate leal 4(%esp), %eax; /* get caller's sp at time of call */ \ 660Sstevel@tonic-gate movl %eax, UESP_OFF (%edx); \ 670Sstevel@tonic-gate /* store the sp into UESP of env var */ \ 680Sstevel@tonic-gate xorl %eax, %eax; /* return 0 */ \ 690Sstevel@tonic-gate movl %eax, EAX_OFF (%edx); \ 700Sstevel@tonic-gate /* getcontext returns 0 after a setcontext */ 710Sstevel@tonic-gate 720Sstevel@tonic-gate/* 730Sstevel@tonic-gate * getcontext(ucontext_t *ucp) 740Sstevel@tonic-gate */ 750Sstevel@tonic-gate ENTRY(getcontext) 760Sstevel@tonic-gate GETCONTEXT_IMPL 770Sstevel@tonic-gate ret 780Sstevel@tonic-gate SET_SIZE(getcontext) 790Sstevel@tonic-gate 800Sstevel@tonic-gate 810Sstevel@tonic-gate/* 820Sstevel@tonic-gate * swapcontext(ucontext_t *oucp, const ucontext_t *ucp) 830Sstevel@tonic-gate */ 840Sstevel@tonic-gate ENTRY(swapcontext) 850Sstevel@tonic-gate GETCONTEXT_IMPL 860Sstevel@tonic-gate / call setcontext 870Sstevel@tonic-gate movl 8(%esp), %eax /* %eax <-- second arg: ucp */ 880Sstevel@tonic-gate pushl %eax /* push ucp for setcontext */ 896515Sraf call setcontext 900Sstevel@tonic-gate addl $4, %esp /* pop arg: just in case */ 910Sstevel@tonic-gate ret 920Sstevel@tonic-gate SET_SIZE(swapcontext) 93