riscv_machdep.c revision 1.14 1 /* $NetBSD: riscv_machdep.c,v 1.14 2021/05/01 06:53:08 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2014, 2019 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 #include <sys/cdefs.h>
33
34 #include "opt_modular.h"
35
36 __RCSID("$NetBSD: riscv_machdep.c,v 1.14 2021/05/01 06:53:08 skrll Exp $");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/cpu.h>
41 #include <sys/exec.h>
42 #include <sys/lwp.h>
43 #include <sys/kmem.h>
44 #include <sys/ktrace.h>
45 #include <sys/module.h>
46 #include <sys/proc.h>
47 #include <sys/reboot.h>
48 #include <sys/syscall.h>
49
50 #include <uvm/uvm_extern.h>
51
52 #include <riscv/locore.h>
53
54 int cpu_printfataltraps;
55 char machine[] = MACHINE;
56 char machine_arch[] = MACHINE_ARCH;
57
58 struct vm_map *phys_map;
59
60 struct trapframe cpu_ddb_regs;
61
62 struct cpu_info cpu_info_store = {
63 .ci_cpl = IPL_HIGH,
64 .ci_ddb_regs = &cpu_ddb_regs,
65 };
66
67 const pcu_ops_t * const pcu_ops_md_defs[PCU_UNIT_COUNT] = {
68 #ifdef FPE
69 [PCU_FPU] = &pcu_fpu_ops,
70 #endif
71 };
72
73 void
74 delay(unsigned long us)
75 {
76 const uint32_t cycles_per_us = curcpu()->ci_data.cpu_cc_freq / 1000000;
77 const uint64_t cycles = (uint64_t)us * cycles_per_us;
78 const uint64_t finish = riscvreg_cycle_read() + cycles;
79
80 while (riscvreg_cycle_read() < finish) {
81 /* spin, baby spin */
82 }
83 }
84
85 #ifdef MODULAR
86 /*
87 * Push any modules loaded by the boot loader.
88 */
89 void
90 module_init_md(void)
91 {
92 }
93 #endif /* MODULAR */
94
95 /*
96 * Set registers on exec.
97 * Clear all registers except sp, pc, and t9.
98 * $sp is set to the stack pointer passed in. $pc is set to the entry
99 * point given by the exec_package passed in, as is $t9 (used for PIC
100 * code by the MIPS elf abi).
101 */
102 void
103 setregs(struct lwp *l, struct exec_package *pack, vaddr_t stack)
104 {
105 struct trapframe * const tf = l->l_md.md_utf;
106 struct proc * const p = l->l_proc;
107
108 memset(tf, 0, sizeof(struct trapframe));
109 tf->tf_sp = (intptr_t)stack_align(stack);
110 tf->tf_pc = (intptr_t)pack->ep_entry & ~1;
111 #ifdef _LP64
112 tf->tf_sr = (p->p_flag & PK_32) ? SR_USER32 : SR_USER;
113 #else
114 tf->tf_sr = SR_USER;
115 #endif
116 // Set up arguments for _start(obj, cleanup, ps_strings)
117 tf->tf_a0 = 0; // obj
118 tf->tf_a1 = 0; // cleanup
119 tf->tf_a2 = p->p_psstrp; // ps_strings
120 }
121
122 void
123 md_child_return(struct lwp *l)
124 {
125 struct trapframe * const tf = l->l_md.md_utf;
126
127 tf->tf_a0 = 0;
128 tf->tf_a1 = 1;
129 #ifdef FPE
130 tf->tf_sr &= ~SR_EF; /* Disable FP as we can't be them. */
131 #endif
132 }
133
134 void
135 cpu_spawn_return(struct lwp *l)
136 {
137 userret(l);
138 }
139
140 /*
141 * Start a new LWP
142 */
143 void
144 startlwp(void *arg)
145 {
146 ucontext_t * const uc = arg;
147 lwp_t * const l = curlwp;
148 int error __diagused;
149
150 error = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
151 KASSERT(error == 0);
152
153 kmem_free(uc, sizeof(ucontext_t));
154 userret(l);
155 }
156
157 // We've worked hard to make sure struct reg and __gregset_t are the same.
158 // Ditto for struct fpreg and fregset_t.
159
160 CTASSERT(sizeof(struct reg) == sizeof(__gregset_t));
161 CTASSERT(sizeof(struct fpreg) == sizeof(__fregset_t));
162
163 void
164 cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
165 {
166 const struct trapframe * const tf = l->l_md.md_utf;
167
168 /* Save register context. */
169 *(struct reg *)mcp->__gregs = tf->tf_regs;
170
171 mcp->__private = (intptr_t)l->l_private;
172
173 *flags |= _UC_CPU | _UC_TLSBASE;
174
175 /* Save floating point register context, if any. */
176 KASSERT(l == curlwp);
177 if (fpu_valid_p(l)) {
178 /*
179 * If this process is the current FP owner, dump its
180 * context to the PCB first.
181 */
182 fpu_save(l);
183
184 struct pcb * const pcb = lwp_getpcb(l);
185 *(struct fpreg *)mcp->__fregs = pcb->pcb_fpregs;
186 *flags |= _UC_FPU;
187 }
188 }
189
190 int
191 cpu_mcontext_validate(struct lwp *l, const mcontext_t *mcp)
192 {
193 /*
194 * Verify that at least the PC and SP are user addresses.
195 */
196 if ((intptr_t) mcp->__gregs[_REG_PC] < 0
197 || (intptr_t) mcp->__gregs[_REG_SP] < 0
198 || (mcp->__gregs[_REG_PC] & 1))
199 return EINVAL;
200
201 return 0;
202 }
203
204 int
205 cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
206 {
207 struct trapframe * const tf = l->l_md.md_utf;
208 struct proc * const p = l->l_proc;
209 const __greg_t * const gr = mcp->__gregs;
210 int error;
211
212 /* Restore register context, if any. */
213 if (flags & _UC_CPU) {
214 error = cpu_mcontext_validate(l, mcp);
215 if (error)
216 return error;
217
218 /* Save register context. */
219 tf->tf_regs = *(const struct reg *)gr;
220 }
221
222 /* Restore the private thread context */
223 if (flags & _UC_TLSBASE) {
224 lwp_setprivate(l, (void *)(intptr_t)mcp->__private);
225 }
226
227 /* Restore floating point register context, if any. */
228 if (flags & _UC_FPU) {
229 KASSERT(l == curlwp);
230 /* Tell PCU we are replacing the FPU contents. */
231 fpu_replace(l);
232
233 /*
234 * The PCB FP regs struct includes the FP CSR, so use the
235 * proper size of fpreg when copying.
236 */
237 struct pcb * const pcb = lwp_getpcb(l);
238 pcb->pcb_fpregs = *(const struct fpreg *)mcp->__fregs;
239 }
240
241 mutex_enter(p->p_lock);
242 if (flags & _UC_SETSTACK)
243 l->l_sigstk.ss_flags |= SS_ONSTACK;
244 if (flags & _UC_CLRSTACK)
245 l->l_sigstk.ss_flags &= ~SS_ONSTACK;
246 mutex_exit(p->p_lock);
247
248 return (0);
249 }
250
251 void
252 cpu_need_resched(struct cpu_info *ci, struct lwp *l, int flags)
253 {
254 KASSERT(kpreempt_disabled());
255
256 if ((flags & RESCHED_KPREEMPT) != 0) {
257 #ifdef __HAVE_PREEMPTION
258 if ((flags & RESCHED_REMOTE) != 0) {
259 cpu_send_ipi(ci, IPI_KPREEMPT);
260 } else {
261 softint_trigger(SOFTINT_KPREEMPT);
262 }
263 #endif
264 return;
265 }
266 if ((flags & RESCHED_REMOTE) != 0) {
267 #ifdef MULTIPROCESSOR
268 cpu_send_ipi(ci, IPI_AST);
269 #endif
270 } else {
271 l->l_md.md_astpending = 1; /* force call to ast() */
272 }
273 }
274
275 void
276 cpu_signotify(struct lwp *l)
277 {
278 KASSERT(kpreempt_disabled());
279 #ifdef __HAVE_FAST_SOFTINTS
280 KASSERT(lwp_locked(l, NULL));
281 #endif
282
283 if (l->l_cpu != curcpu()) {
284 #ifdef MULTIPROCESSOR
285 cpu_send_ipi(ci, IPI_AST);
286 #endif
287 } else {
288 l->l_md.md_astpending = 1; /* force call to ast() */
289 }
290 }
291
292 void
293 cpu_need_proftick(struct lwp *l)
294 {
295 KASSERT(kpreempt_disabled());
296 KASSERT(l->l_cpu == curcpu());
297
298 l->l_pflag |= LP_OWEUPC;
299 l->l_md.md_astpending = 1; /* force call to ast() */
300 }
301
302 void
303 cpu_reboot(int how, char *bootstr)
304 {
305 for (;;) {
306 }
307 }
308
309 void
310 cpu_dumpconf(void)
311 {
312 // TBD!!
313 }
314
315 void
316 cpu_startup(void)
317 {
318 vaddr_t minaddr, maxaddr;
319 char pbuf[9]; /* "99999 MB" */
320
321 /*
322 * Good {morning,afternoon,evening,night}.
323 */
324 printf("%s%s", copyright, version);
325 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
326 printf("total memory = %s\n", pbuf);
327
328 minaddr = 0;
329 /*
330 * Allocate a submap for physio.
331 */
332 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
333 VM_PHYS_SIZE, 0, FALSE, NULL);
334
335 format_bytes(pbuf, sizeof(pbuf), ptoa(uvm_availmem(false)));
336 printf("avail memory = %s\n", pbuf);
337 }
338
339 void
340 init_riscv(vaddr_t kernstart, vaddr_t kernend)
341 {
342
343 /* Early VM bootstrap. */
344 pmap_bootstrap();
345 }
346