11.9Sriastrad/* $NetBSD: makecontext.c,v 1.9 2025/04/27 16:15:40 riastradh Exp $ */ 21.2Sthorpej 31.2Sthorpej/*- 41.2Sthorpej * Copyright (c) 2001 The NetBSD Foundation, Inc. 51.2Sthorpej * All rights reserved. 61.2Sthorpej * 71.2Sthorpej * This code is derived from software contributed to The NetBSD Foundation 81.2Sthorpej * by Klaus Klein. 91.2Sthorpej * 101.2Sthorpej * Redistribution and use in source and binary forms, with or without 111.2Sthorpej * modification, are permitted provided that the following conditions 121.2Sthorpej * are met: 131.2Sthorpej * 1. Redistributions of source code must retain the above copyright 141.2Sthorpej * notice, this list of conditions and the following disclaimer. 151.2Sthorpej * 2. Redistributions in binary form must reproduce the above copyright 161.2Sthorpej * notice, this list of conditions and the following disclaimer in the 171.2Sthorpej * documentation and/or other materials provided with the distribution. 181.2Sthorpej * 191.2Sthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 201.2Sthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 211.2Sthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 221.2Sthorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 231.2Sthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 241.2Sthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 251.2Sthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 261.2Sthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 271.2Sthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 281.2Sthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 291.2Sthorpej * POSSIBILITY OF SUCH DAMAGE. 301.2Sthorpej */ 311.2Sthorpej 321.2Sthorpej#include <sys/cdefs.h> 331.2Sthorpej#if defined(LIBC_SCCS) && !defined(lint) 341.9Sriastrad__RCSID("$NetBSD: makecontext.c,v 1.9 2025/04/27 16:15:40 riastradh Exp $"); 351.2Sthorpej#endif 361.2Sthorpej 371.9Sriastrad#include <sys/param.h> 381.9Sriastrad 391.8Sriastrad#include <inttypes.h> 401.8Sriastrad#include <stdarg.h> 411.2Sthorpej#include <stddef.h> 421.2Sthorpej#include <ucontext.h> 431.8Sriastrad 441.2Sthorpej#include "extern.h" 451.2Sthorpej 461.6Sjoergvoid __resumecontext(void) __dead; 471.6Sjoerg 481.2Sthorpejvoid 491.2Sthorpejmakecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) 501.2Sthorpej{ 511.2Sthorpej __greg_t *gr = ucp->uc_mcontext.__gregs; 521.2Sthorpej int i; 531.2Sthorpej unsigned long *sp; 541.2Sthorpej va_list ap; 551.2Sthorpej 561.7Sthorpej /* Compute stack pointer. */ 571.2Sthorpej sp = (unsigned long *) 581.7Sthorpej ((uintptr_t)ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size); 591.2Sthorpej /* Allocate necessary stack space for arguments exceeding a0-5. */ 601.2Sthorpej if (argc > 6) 611.2Sthorpej sp -= (argc - 6); 621.7Sthorpej /* Align stack pointer. */ 631.9Sriastrad sp = (unsigned long *)((uintptr_t)sp & ~STACK_ALIGNBYTES); 641.2Sthorpej gr[_REG_SP] = (__greg_t)sp; 651.2Sthorpej /* Arrange for return via the trampoline code. */ 661.3Sdrochner gr[_REG_RA] = (__greg_t)__resumecontext; 671.2Sthorpej gr[_REG_PC] = (__greg_t)func; 681.2Sthorpej gr[_REG_PV] = (__greg_t)func; 691.2Sthorpej 701.2Sthorpej va_start(ap, argc); 711.2Sthorpej /* Pass up to six arguments in a0-5. */ 721.2Sthorpej for (i = 0; i < argc && i < 6; i++) 731.2Sthorpej gr[_REG_A0 + i] = va_arg(ap, unsigned long); 741.2Sthorpej /* Pass any additional arguments on the stack. */ 751.2Sthorpej for (argc -= i; argc > 0; argc--) 761.2Sthorpej *sp++ = va_arg(ap, unsigned long); 771.2Sthorpej va_end(ap); 781.2Sthorpej} 79