1/* Create new context.
2   Copyright (C) 2012-2021 Free Software Foundation, Inc.
3   This file is part of the GNU C Library.
4
5   The GNU C Library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9
10   The GNU C Library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with the GNU C Library; if not, see
17   <https://www.gnu.org/licenses/>.  */
18
19#include <sysdep.h>
20
21#include "ucontext_i.h"
22
23
24ENTRY(__makecontext)
25	move.l	4(%sp), %a0
26
27	/* Get the address of the function we are supposed to run.  */
28	move.l	8(%sp), oPC(%a0)
29
30	/* Compute the address of the stack.  The information comes from
31	   to us_stack element.  */
32	move.l	oSS_SP(%a0), %a1
33	add.l	oSS_SIZE(%a0), %a1
34
35	/* Remember the number of parameters for the exit handler since
36	   it has to remove them.  We store the number in the D7 register
37	   which the function we will call must preserve.  */
38	move.l	12(%sp), %d1
39	move.l	%d1, oGREGS+7*4(%a0)
40
41	/* Make room on the new stack for the parameters.
42	   Room for the arguments, return address (== 1f) and
43	   oLINK pointer is needed.  */
44	neg.l	%d1
45	lea	-8(%a1,%d1.l*4), %a1
46	neg.l	%d1
47
48	/* Store the future stack pointer.  */
49	move.l	%a1, oSP(%a0)
50
51	/* Put the next context on the new stack (from the uc_link
52	   element).  */
53	move.l	oLINK(%a0), 4(%a1,%d1.l*4)
54
55	/* Copy all the parameters.  */
561:	subq.l	#1,%d1
57	jmi	2f
58	move.l	16(%sp,%d1.l*4), 4(%a1,%d1.l*4)
59	jra	1b
602:
61
62	/* If the function we call returns we must continue with the
63	   context which is given in the uc_link element.  To do this
64	   set the return address for the function the user provides
65	   to a little bit of helper code which does the magic (see
66	   below).  */
67	lea	1f(%pc), %a0
68	move.l	%a0, (%a1)
69	/* 'makecontext' returns no value.  */
70	rts
71
72	/* This is the helper code which gets called if a function which
73	   is registered with 'makecontext' returns.  In this case we
74	   have to install the context listed in the uc_link element of
75	   the context 'makecontext' manipulated at the time of the
76	   'makecontext' call.  If the pointer is NULL the process must
77	   terminate.
78	   Make sure to separate the return label from the previous unwind
79	   region, because the unwinder uses ra-1 to find the FDE.  */
80	cfi_endproc
81	nop
821:
83	/* This removes the parameters passed to the function given to
84	   'makecontext' from the stack.  D7 contains the number of
85	   parameters (see above).  */
86	lea	(%sp,%d7.l*4), %sp
87
88	tst.l	(%sp)			/* Check the next context.  */
89	jeq	2f			/* If it is zero exit.  */
90
91	jbsr	JUMPTARGET(__setcontext)
92	/* If this returns (which can happen if the syscall fails) we'll
93	   exit the program with the return error value (-1).  */
94
95	move.l	%d0, (%sp)
962:	jbsr	HIDDEN_JUMPTARGET(exit)
97	/* The 'exit' call should never return.  In case it does cause
98	   the process to terminate.  */
99	illegal
100	cfi_startproc
101END(__makecontext)
102
103weak_alias (__makecontext, makecontext)
104