Home | History | Annotate | Line # | Download | only in include
      1 /* $NetBSD: locore.h,v 1.14 2024/11/22 20:01:04 skrll Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Thomas of 3am Software Foundry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _RISCV_LOCORE_H_
     33 #define _RISCV_LOCORE_H_
     34 
     35 #include <sys/lwp.h>
     36 #include <sys/userret.h>
     37 
     38 #include <riscv/reg.h>
     39 #include <riscv/sysreg.h>
     40 
     41 #define	FB_A0	0
     42 #define	FB_RA	1
     43 #define	FB_SP	2
     44 #define	FB_GP	3
     45 #define	FB_S0	4
     46 #define	FB_S1	5
     47 #define	FB_S2	6
     48 #define	FB_S3	7
     49 #define	FB_S4	8
     50 #define	FB_S5	9
     51 #define	FB_S6	10
     52 #define	FB_S7	11
     53 #define	FB_S8	12
     54 #define	FB_S9	13
     55 #define	FB_S10	14
     56 #define	FB_S11	15
     57 #define	FB_MAX	16
     58 
     59 struct faultbuf {
     60 	register_t fb_reg[FB_MAX];
     61 	register_t fb_sr;
     62 };
     63 
     64 CTASSERT(sizeof(label_t) == sizeof(struct faultbuf));
     65 
     66 #ifdef _KERNEL
     67 extern int cpu_printfataltraps;
     68 
     69 #ifdef FPE
     70 extern const pcu_ops_t pcu_fpu_ops;
     71 #endif
     72 
     73 static inline vaddr_t
     74 stack_align(vaddr_t sp)
     75 {
     76 	return sp & ~STACK_ALIGNBYTES;
     77 }
     78 
     79 static inline void
     80 fpu_load(void)
     81 {
     82 #ifdef FPE
     83 	pcu_load(&pcu_fpu_ops);
     84 #endif
     85 }
     86 
     87 static inline void
     88 fpu_save(lwp_t *l)
     89 {
     90 #ifdef FPE
     91 	pcu_save(&pcu_fpu_ops, l);
     92 #endif
     93 }
     94 
     95 static inline void
     96 fpu_discard(lwp_t *l)
     97 {
     98 #ifdef FPE
     99 	pcu_discard(&pcu_fpu_ops, l, false);
    100 #endif
    101 }
    102 
    103 static inline void
    104 fpu_replace(lwp_t *l)
    105 {
    106 #ifdef FPE
    107 	pcu_discard(&pcu_fpu_ops, l, true);
    108 #endif
    109 }
    110 
    111 static inline bool
    112 fpu_valid_p(lwp_t *l)
    113 {
    114 #ifdef FPE
    115 	return pcu_valid_p(&pcu_fpu_ops, l);
    116 #else
    117 	return false;
    118 #endif
    119 }
    120 
    121 void	__syncicache(const void *, size_t);
    122 
    123 int	cpu_set_onfault(struct faultbuf *) __returns_twice;
    124 void	cpu_jump_onfault(struct trapframe *, const struct faultbuf *, int);
    125 
    126 static inline void
    127 cpu_unset_onfault(void)
    128 {
    129 	curlwp->l_md.md_onfault = NULL;
    130 }
    131 
    132 static inline struct faultbuf *
    133 cpu_disable_onfault(void)
    134 {
    135 	struct faultbuf * const fb = curlwp->l_md.md_onfault;
    136 	curlwp->l_md.md_onfault = NULL;
    137 	return fb;
    138 }
    139 
    140 static inline void
    141 cpu_enable_onfault(struct faultbuf *fb)
    142 {
    143 	curlwp->l_md.md_onfault = fb;
    144 }
    145 
    146 void	cpu_intr(struct trapframe */*tf*/, register_t /*epc*/,
    147 	    register_t /*status*/, register_t /*cause*/);
    148 void	cpu_trap(struct trapframe */*tf*/, register_t /*epc*/,
    149 	    register_t /*status*/, register_t /*cause*/,
    150 	    register_t /*badvaddr*/);
    151 void	cpu_ast(struct trapframe *);
    152 void	cpu_fast_switchto(struct lwp *, int);
    153 
    154 void	lwp_trampoline(void);
    155 
    156 void *	cpu_sendsig_getframe(struct lwp *, int, bool *);
    157 
    158 #endif
    159 
    160 #endif /* _RISCV_LOCORE_H_ */
    161