xref: /netbsd-src/lib/libc/arch/riscv/gen/swapcontext.S (revision 35c3446182b5d2ae37a831f3e9c1597901314cac)
1*35c34461Smatt/*	$NetBSD: swapcontext.S,v 1.3 2015/03/27 23:23:14 matt Exp $	*/
26cf6fe02Smatt
36cf6fe02Smatt/*-
46cf6fe02Smatt * Copyright (c) 2001 The NetBSD Foundation, Inc.
56cf6fe02Smatt * All rights reserved.
66cf6fe02Smatt *
76cf6fe02Smatt * This code is derived from software contributed to The NetBSD Foundation
86cf6fe02Smatt * by Klaus Klein.
96cf6fe02Smatt *
106cf6fe02Smatt * Redistribution and use in source and binary forms, with or without
116cf6fe02Smatt * modification, are permitted provided that the following conditions
126cf6fe02Smatt * are met:
136cf6fe02Smatt * 1. Redistributions of source code must retain the above copyright
146cf6fe02Smatt *    notice, this list of conditions and the following disclaimer.
156cf6fe02Smatt * 2. Redistributions in binary form must reproduce the above copyright
166cf6fe02Smatt *    notice, this list of conditions and the following disclaimer in the
176cf6fe02Smatt *    documentation and/or other materials provided with the distribution.
186cf6fe02Smatt *
196cf6fe02Smatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
206cf6fe02Smatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
216cf6fe02Smatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
226cf6fe02Smatt * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
236cf6fe02Smatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
246cf6fe02Smatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
256cf6fe02Smatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266cf6fe02Smatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
276cf6fe02Smatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286cf6fe02Smatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296cf6fe02Smatt * POSSIBILITY OF SUCH DAMAGE.
306cf6fe02Smatt */
316cf6fe02Smatt
326cf6fe02Smatt#include "SYS.h"
336cf6fe02Smatt#include "assym.h"
346cf6fe02Smatt
35*35c34461Smatt__RCSID("$NetBSD: swapcontext.S,v 1.3 2015/03/27 23:23:14 matt Exp $")
366cf6fe02Smatt
376cf6fe02Smatt#define	CALLFRAME_UCP	CALLFRAME_S1
386cf6fe02Smatt#define	CALLFRAME_OUCP	CALLFRAME_S0
396cf6fe02Smatt
406cf6fe02SmattENTRY(swapcontext)
416cf6fe02Smatt	addi	sp, sp, -CALLFRAME_SIZ		# adjust stack
426cf6fe02Smatt	REG_S	ra, CALLFRAME_RA(sp)		# save return address
436cf6fe02Smatt	REG_S	a0, CALLFRAME_OUCP(sp)		# save oucp
446cf6fe02Smatt	REG_S	a1, CALLFRAME_UCP(sp)		# save ucp
456cf6fe02Smatt
466cf6fe02Smatt	call	PLT(_C_LABEL(_getcontext))	# getcontext(oucp)
478a3081d2Smatt	mv	t0, a0				# save return value
486cf6fe02Smatt
496cf6fe02Smatt	REG_L	ra, CALLFRAME_RA(sp)		# restore return address
506cf6fe02Smatt	REG_L	a0, CALLFRAME_UCP(sp)		# load ucp
516cf6fe02Smatt	REG_L	a1, CALLFRAME_OUCP(sp)		# load oucp for adjustment
526cf6fe02Smatt	addi	sp, sp, CALLFRAME_SIZ		# restore stack
536cf6fe02Smatt
548a3081d2Smatt	bnez	t0, 1f				# error? yes, cleanup
556cf6fe02Smatt
566cf6fe02Smatt	REG_S	ra, UC_GREGS_PC(a1)		# pc <- ra
576cf6fe02Smatt	REG_S	sp, UC_GREGS_SP(a1)		# adjust sp
586cf6fe02Smatt
59*35c34461Smatt	tail	PLT(_C_LABEL(setcontext))	# setcontext(ucp)
606cf6fe02Smatt
616cf6fe02Smatt1:	ret					# simply return
626cf6fe02SmattEND(swapcontext)
63