summaryrefslogtreecommitdiff
path: root/libc/sysdeps/linux/kvx/makecontext.c
blob: a52eded9f1ce3d730852ca2b528c15c6bd337547 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * This file is subject to the terms and conditions of the GNU General Public
 * License. See the file "COPYING" in the main directory of this archive for
 * more details.
 *
 * Copyright (C) 2025 Kalray Inc.
 * Author(s): Julian Vetter <jvetter@kalrayinc.com>
 */

#include <stdarg.h>
#include <ucontext.h>


/* Number of arguments that go in registers.	*/
#define NREG_ARGS 12

/* Take a context previously prepared via getcontext() and set to
	 call func() with the given int only args.	*/
void
__makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
{
	extern void __startcontext (void);
	unsigned long *funcstack;
	va_list vl;
	unsigned long *regptr;
	unsigned int reg;

	/* Start at the top of stack.	*/
	funcstack = (unsigned long *) (ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
	funcstack -= argc < NREG_ARGS ? 0 : argc - NREG_ARGS;
	funcstack = (unsigned long *) (((uintptr_t) funcstack & -32L));

	ucp->uc_mcontext.sc_regs.r12 = (unsigned long) funcstack;
	/* Use $r20 and $r21 to pass some infos to __startcontext */
	ucp->uc_mcontext.sc_regs.r20 = (unsigned long) ucp->uc_link;
	ucp->uc_mcontext.sc_regs.r21 = (unsigned long) func;
	ucp->uc_mcontext.sc_regs.ra = (unsigned long) __startcontext;

	va_start (vl, argc);

	/* The first twelve arguments go into registers.	*/
	regptr = &(ucp->uc_mcontext.sc_regs.r0);

	for (reg = 0; (reg < argc) && (reg < NREG_ARGS); reg++)
		*regptr++ = va_arg (vl, unsigned long);

	/* And the remainder on the stack.	*/
	for (; reg < argc; reg++)
		*funcstack++ = va_arg (vl, unsigned long);

	va_end (vl);
}
weak_alias (__makecontext, makecontext)