# --- T2-COPYRIGHT-BEGIN --- # t2/package/*/libucontext/hotfix-i860.patch # Copyright (C) 2026 The T2 SDE Project # SPDX-License-Identifier: GPL-2.0 or patched project license # --- T2-COPYRIGHT-END --- libucontext: i860 Signed-off-by: René Rebe diff -Nurp a/arch/i860/defs.h b/arch/i860/defs.h --- a/arch/i860/defs.h 1970-01-01 01:00:00.000000000 +0100 +++ b/arch/i860/defs.h 2026-11-02 10:12:39.557803316 +0100 @@ -0,0 +1,133 @@ +/* + * Copyright (C) 2026 René Rebe + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#ifndef __ARCH_I860_DEFS_H +#define __ARCH_I860_DEFS_H + +#define REG_RA 1 +#define REG_SP 2 +#define REG_FP 3 +#define REG_LINK 15 /* callee saved: uc_link for the trampoline */ +#define REG_A0 16 +#define NARG_REGS 12 /* r16-r27 */ + +/* offsets in ucontext_t, checked by the test: gregs, fpregs, fir, and the mask */ +#define G1 24 +#define G2 28 +#define G3 32 +#define G4 36 +#define G5 40 +#define G6 44 +#define G7 48 +#define G8 52 +#define G9 56 +#define G10 60 +#define G11 64 +#define G12 68 +#define G13 72 +#define G14 76 +#define G15 80 +#define G16 84 +#define G17 88 +#define G18 92 +#define G19 96 +#define G20 100 +#define G21 104 +#define G22 108 +#define G23 112 +#define G24 116 +#define G25 120 +#define G26 124 +#define G27 128 +#define F2 156 +#define F3 160 +#define F4 164 +#define F5 168 +#define F6 172 +#define F7 176 +#define FIR 284 +#define SIGMASK 424 + +#define NR_RT_SIGPROCMASK 135 +#define SIG_SETMASK_ 2 +#define SIGSET_BYTES 8 + +#ifdef __ASSEMBLER__ + .macro SAVE_CONTEXT base + st.l %r1,G1(\base) + st.l %r1,FIR(\base) + st.l %sp,G2(\base) + st.l %fp,G3(\base) + st.l %r4,G4(\base) + st.l %r5,G5(\base) + st.l %r6,G6(\base) + st.l %r7,G7(\base) + st.l %r8,G8(\base) + st.l %r9,G9(\base) + st.l %r10,G10(\base) + st.l %r11,G11(\base) + st.l %r12,G12(\base) + st.l %r13,G13(\base) + st.l %r14,G14(\base) + st.l %r15,G15(\base) + st.l %r0,G16(\base) + fst.l %f2,F2(\base) + fst.l %f3,F3(\base) + fst.l %f4,F4(\base) + fst.l %f5,F5(\base) + fst.l %f6,F6(\base) + fst.l %f7,F7(\base) + .endm + + /* r29 = the context; r16 last, in the slot of the jump through r31 */ + .macro RESTORE_CONTEXT + fld.l F2(%r29),%f2 + fld.l F3(%r29),%f3 + fld.l F4(%r29),%f4 + fld.l F5(%r29),%f5 + fld.l F6(%r29),%f6 + fld.l F7(%r29),%f7 + ld.l G1(%r29),%r1 + ld.l G2(%r29),%sp + ld.l G3(%r29),%fp + ld.l G4(%r29),%r4 + ld.l G5(%r29),%r5 + ld.l G6(%r29),%r6 + ld.l G7(%r29),%r7 + ld.l G8(%r29),%r8 + ld.l G9(%r29),%r9 + ld.l G10(%r29),%r10 + ld.l G11(%r29),%r11 + ld.l G12(%r29),%r12 + ld.l G13(%r29),%r13 + ld.l G14(%r29),%r14 + ld.l G15(%r29),%r15 + ld.l G17(%r29),%r17 + ld.l G18(%r29),%r18 + ld.l G19(%r29),%r19 + ld.l G20(%r29),%r20 + ld.l G21(%r29),%r21 + ld.l G22(%r29),%r22 + ld.l G23(%r29),%r23 + ld.l G24(%r29),%r24 + ld.l G25(%r29),%r25 + ld.l G26(%r29),%r26 + ld.l G27(%r29),%r27 + ld.l FIR(%r29),%r31 + bri %r31 + ld.l G16(%r29),%r16 + .endm +#endif + +#include "common-defs.h" + +#endif diff -Nurp a/arch/i860/getcontext.S b/arch/i860/getcontext.S --- a/arch/i860/getcontext.S 1970-01-01 01:00:00.000000000 +0100 +++ b/arch/i860/getcontext.S 2026-11-02 10:12:39.557826837 +0100 @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2026 René Rebe + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#include "defs.h" + +ALIAS(getcontext, libucontext_getcontext) +ALIAS(__getcontext, libucontext_getcontext) + + .text +FUNC(libucontext_getcontext) + SAVE_CONTEXT %r16 + /* rt_sigprocmask(0, NULL, &ucp->uc_sigmask, 8) */ + addu SIGMASK,%r16,%r18 + adds 0,%r0,%r16 + adds 0,%r0,%r17 + adds SIGSET_BYTES,%r0,%r19 + or NR_RT_SIGPROCMASK,%r0,%r28 + trap %r0,%r0,%r0 + bri %r1 + adds 0,%r0,%r16 +END(libucontext_getcontext) diff -Nurp a/arch/i860/include/libucontext/bits.h b/arch/i860/include/libucontext/bits.h --- a/arch/i860/include/libucontext/bits.h 1970-01-01 01:00:00.000000000 +0100 +++ b/arch/i860/include/libucontext/bits.h 2026-11-02 10:12:39.557877869 +0100 @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2026 René Rebe + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#ifndef LIBUCONTEXT_BITS_H +#define LIBUCONTEXT_BITS_H + +typedef unsigned long libucontext_greg_t; + +typedef struct { + unsigned long __gregs[32]; + unsigned long __fpregs[32]; + unsigned long __psr, __epsr, __fir, __fsr; + unsigned long __oldmask; + unsigned long __fp_pipe[32]; +} libucontext_mcontext_t; + +typedef struct { + void *ss_sp; + int ss_flags; + size_t ss_size; +} libucontext_stack_t; + +typedef struct libucontext_ucontext { + unsigned long uc_flags; + struct libucontext_ucontext *uc_link; + libucontext_stack_t uc_stack; + libucontext_mcontext_t uc_mcontext; + unsigned long uc_sigmask[128 / sizeof(unsigned long)]; +} libucontext_ucontext_t; + +#endif diff -Nurp a/arch/i860/makecontext.c b/arch/i860/makecontext.c --- a/arch/i860/makecontext.c 1970-01-01 01:00:00.000000000 +0100 +++ b/arch/i860/makecontext.c 2026-11-02 10:12:39.557857488 +0100 @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2026 René Rebe + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#include +#include +#include +#include "defs.h" +#include + +extern void libucontext_trampoline(void); + +void +libucontext_makecontext(libucontext_ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + libucontext_greg_t *sp, *regs = ucp->uc_mcontext.__gregs; + va_list va; + int i; + + /* arguments past the registers start at sp at the call */ + sp = (libucontext_greg_t *) ((uintptr_t) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size); + sp -= argc < NARG_REGS ? 0 : argc - NARG_REGS; + sp = (libucontext_greg_t *) ((uintptr_t) sp & -16L); + + regs[REG_RA] = (libucontext_greg_t) libucontext_trampoline; + regs[REG_SP] = (libucontext_greg_t) sp; + regs[REG_FP] = 0; + regs[REG_LINK] = (libucontext_greg_t) ucp->uc_link; + ucp->uc_mcontext.__fir = (libucontext_greg_t) func; + + va_start(va, argc); + for (i = 0; i < argc && i < NARG_REGS; i++) + regs[REG_A0 + i] = va_arg(va, libucontext_greg_t); + for (; i < argc; i++) + *sp++ = va_arg(va, libucontext_greg_t); + va_end(va); +} + +#ifdef EXPORT_UNPREFIXED +extern __typeof(libucontext_makecontext) makecontext __attribute__((weak, __alias__("libucontext_makecontext"))); +extern __typeof(libucontext_makecontext) __makecontext __attribute__((weak, __alias__("libucontext_makecontext"))); +#endif diff -Nurp a/arch/i860/setcontext.S b/arch/i860/setcontext.S --- a/arch/i860/setcontext.S 1970-01-01 01:00:00.000000000 +0100 +++ b/arch/i860/setcontext.S 2026-11-02 10:12:39.557838197 +0100 @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2026 René Rebe + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#include "defs.h" + +ALIAS(setcontext, libucontext_setcontext) +ALIAS(__setcontext, libucontext_setcontext) + + .text +FUNC(libucontext_setcontext) + mov %r16,%r29 + /* rt_sigprocmask(SIG_SETMASK, &ucp->uc_sigmask, NULL, 8) */ + adds SIG_SETMASK_,%r0,%r16 + addu SIGMASK,%r29,%r17 + adds 0,%r0,%r18 + adds SIGSET_BYTES,%r0,%r19 + or NR_RT_SIGPROCMASK,%r0,%r28 + trap %r0,%r0,%r0 + RESTORE_CONTEXT +END(libucontext_setcontext) diff -Nurp a/arch/i860/swapcontext.S b/arch/i860/swapcontext.S --- a/arch/i860/swapcontext.S 1970-01-01 01:00:00.000000000 +0100 +++ b/arch/i860/swapcontext.S 2026-11-02 10:12:39.557847868 +0100 @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2026 René Rebe + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#include "defs.h" + +ALIAS(swapcontext, libucontext_swapcontext) +ALIAS(__swapcontext, libucontext_swapcontext) + + .text +FUNC(libucontext_swapcontext) + SAVE_CONTEXT %r16 + mov %r17,%r29 + /* rt_sigprocmask(SIG_SETMASK, &ucp->uc_sigmask, &oucp->uc_sigmask, 8) */ + addu SIGMASK,%r16,%r18 + addu SIGMASK,%r29,%r17 + adds SIG_SETMASK_,%r0,%r16 + adds SIGSET_BYTES,%r0,%r19 + or NR_RT_SIGPROCMASK,%r0,%r28 + trap %r0,%r0,%r0 + RESTORE_CONTEXT +END(libucontext_swapcontext) diff -Nurp a/arch/i860/trampoline.c b/arch/i860/trampoline.c --- a/arch/i860/trampoline.c 1970-01-01 01:00:00.000000000 +0100 +++ b/arch/i860/trampoline.c 2026-11-02 10:12:39.557867289 +0100 @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2026 René Rebe + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#include "defs.h" +#include + +#define FETCH_LINKPTR(dest) \ + __asm__("mov %%r15,%0" : "=r" ((dest))) + +#include "common-trampoline.c"