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