11.16Schristos/*	$NetBSD: mcontext.h,v 1.16 2024/11/30 01:04:10 christos Exp $	*/
21.1Scherry
31.1Scherry/*-
41.1Scherry * Copyright (c) 1999 The NetBSD Foundation, Inc.
51.1Scherry * All rights reserved.
61.1Scherry *
71.5Sscole * Copyright (c) 1999, 2003 Marcel Moolenaar
81.5Sscole *
91.1Scherry * This code is derived from software contributed to The NetBSD Foundation
101.1Scherry * by Klaus Klein.
111.1Scherry *
121.1Scherry * Redistribution and use in source and binary forms, with or without
131.1Scherry * modification, are permitted provided that the following conditions
141.1Scherry * are met:
151.1Scherry * 1. Redistributions of source code must retain the above copyright
161.1Scherry *    notice, this list of conditions and the following disclaimer.
171.1Scherry * 2. Redistributions in binary form must reproduce the above copyright
181.1Scherry *    notice, this list of conditions and the following disclaimer in the
191.1Scherry *    documentation and/or other materials provided with the distribution.
201.5Sscole * 3. The name of the author may not be used to endorse or promote products
211.5Sscole *    derived from this software without specific prior written permission.
221.1Scherry *
231.1Scherry * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
241.1Scherry * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
251.1Scherry * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
261.1Scherry * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
271.1Scherry * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
281.1Scherry * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
291.1Scherry * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
301.1Scherry * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
311.1Scherry * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
321.1Scherry * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
331.1Scherry * POSSIBILITY OF SUCH DAMAGE.
341.1Scherry */
351.1Scherry
361.1Scherry#ifndef _IA64_MCONTEXT_H_
371.1Scherry#define _IA64_MCONTEXT_H_
381.1Scherry
391.1Scherry#include <machine/_regset.h>
401.1Scherry
411.5Sscole/* XXX fix this, just get to compile for now */
421.11Sscole#define _NGREG	1
431.5Sscole
441.5Sscole#ifndef __ASSEMBLER__
451.5Sscoletypedef unsigned long __greg_t;
461.5Sscoletypedef __greg_t __gregset_t[_NGREG];
471.5Sscole
481.5Sscoletypedef struct {
491.5Sscole  union _ia64_fpreg __fpregs[_NGREG];
501.5Sscole} __fpregset_t;
511.5Sscole
521.5Sscole#endif /* __ASSEMBLER__ */
531.5Sscole
541.1Scherry/*
551.1Scherry * The mc_flags field provides the necessary clues when dealing with the gory
561.1Scherry * details of ia64 specific contexts. A comprehensive explanation is added for
571.1Scherry * everybody's sanity, including the author's.
581.1Scherry *
591.1Scherry * The first and foremost variation in the context is synchronous contexts
601.1Scherry * (= synctx) versus asynchronous contexts (= asynctx). A synctx is created
611.1Scherry * synchronously WRT program execution and has the advantage that none of the
621.1Scherry * scratch registers have to be saved. They are assumed to be clobbered by the
631.1Scherry * call to the function that creates the context. An asynctx needs to have the
641.1Scherry * scratch registers preserved because it can describe any point in a thread's
651.1Scherry * (or process') execution.
661.1Scherry * The second variation is for synchronous contexts. When the kernel creates
671.1Scherry * a synchronous context if needs to preserve the scratch registers, because
681.1Scherry * the syscall argument and return values are stored there in the trapframe
691.1Scherry * and they need to be preserved in order to restart a syscall or return the
701.1Scherry * proper return values. Also, the IIP and CFM fields need to be preserved
711.1Scherry * as they point to the syscall stub, which the kernel saves as a favor to
721.1Scherry * userland (it keeps the stubs small and simple).
731.1Scherry *
741.1Scherry * Below a description of the flags and their meaning:
751.1Scherry *
761.1Scherry *	_MC_FLAGS_ASYNC_CONTEXT
771.1Scherry *		If set, indicates that mc_scratch and mc_scratch_fp are both
781.1Scherry *		valid. IFF not set, _MC_FLAGS_SYSCALL_CONTEXT indicates if the
791.1Scherry *		synchronous context is one corresponding to a syscall or not.
801.1Scherry *		Only the kernel is expected to create such a context and it's
811.1Scherry *		probably wise to let the kernel restore it.
821.1Scherry *	_MC_FLAGS_HIGHFP_VALID
831.1Scherry *		If set, indicates that the high FP registers (f32-f127) are
841.1Scherry *		valid. This flag is very likely not going to be set for any
851.1Scherry *		sensible synctx, but is not explicitly disallowed. Any synctx
861.1Scherry *		that has this flag may or may not have the high FP registers
871.1Scherry *		restored. In short: don't do it.
881.1Scherry *	_MC_FLAGS_SYSCALL_CONTEXT
891.1Scherry *		If set (hence _MC_FLAGS_ASYNC_CONTEXT is not set) indicates
901.1Scherry *		that the scratch registers contain syscall arguments and
911.1Scherry *		return values and that additionally IIP and CFM are valid.
921.1Scherry *		Only the kernel is expected to create such a context. It's
931.1Scherry *		probably wise to let the kernel restore it.
941.1Scherry */
951.1Scherry
961.1Scherrytypedef struct __mcontext {
971.1Scherry	unsigned long		mc_flags;
981.1Scherry#define	_MC_FLAGS_ASYNC_CONTEXT		0x0001
991.1Scherry#define	_MC_FLAGS_HIGHFP_VALID		0x0002
1001.1Scherry#define	_MC_FLAGS_SYSCALL_CONTEXT	0x0008
1011.1Scherry	unsigned long		_reserved_;
1021.1Scherry	struct _special		mc_special;
1031.1Scherry	struct _callee_saved	mc_preserved;
1041.1Scherry	struct _callee_saved_fp	mc_preserved_fp;
1051.1Scherry	struct _caller_saved	mc_scratch;
1061.1Scherry	struct _caller_saved_fp	mc_scratch_fp;
1071.1Scherry	struct _high_fp		mc_high_fp;
1081.5Sscole
1091.5Sscole	/* XXX fix */
1101.5Sscole	__gregset_t		__gregs;
1111.5Sscole	__fpregset_t		__fpregs;
1121.1Scherry} mcontext_t;
1131.1Scherry
1141.8Sscole#define _UC_MACHINE_SP(uc)	((uc)->uc_mcontext.mc_special.sp)  /* gregs[12] */
1151.9Skamil#define _UC_MACHINE_FP(uc)	0 /* Not supported in target */
1161.5Sscole#define	_UC_MACHINE_PC(uc)	((uc)->uc_mcontext.mc_special.iip)
1171.12Sscole#define	_UC_MACHINE_INTRV(uc)	((uc)->uc_mcontext.mc_scratch.gr8) /* gregs[8] */
1181.9Skamil#define _UC_MACHINE_SET_PC(uc, pc)	(uc)->uc_mcontext.mc_special.iip = (pc)
1191.1Scherry
1201.13Schristos#define	_UC_TLSBASE	_UC_MD_BIT16
1211.13Schristos#define	_UC_SETSTACK	_UC_MD_BIT17
1221.13Schristos#define	_UC_CLRSTACK	_UC_MD_BIT18
1231.13Schristos
1241.1Scherry#endif	/* !_IA64_MCONTEXT_H_ */
125