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