]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/machdep.c
MFV: r360512
[FreeBSD/FreeBSD.git] / sys / riscv / riscv / machdep.c
1 /*-
2  * Copyright (c) 2014 Andrew Turner
3  * Copyright (c) 2015-2017 Ruslan Bukin <br@bsdpad.com>
4  * All rights reserved.
5  *
6  * Portions of this software were developed by SRI International and the
7  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
8  * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
9  *
10  * Portions of this software were developed by the University of Cambridge
11  * Computer Laboratory as part of the CTSRD Project, with support from the
12  * UK Higher Education Innovation Fund (HEIF).
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include "opt_platform.h"
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/buf.h>
44 #include <sys/bus.h>
45 #include <sys/cons.h>
46 #include <sys/cpu.h>
47 #include <sys/devmap.h>
48 #include <sys/exec.h>
49 #include <sys/imgact.h>
50 #include <sys/kdb.h>
51 #include <sys/kernel.h>
52 #include <sys/ktr.h>
53 #include <sys/limits.h>
54 #include <sys/linker.h>
55 #include <sys/msgbuf.h>
56 #include <sys/pcpu.h>
57 #include <sys/physmem.h>
58 #include <sys/proc.h>
59 #include <sys/ptrace.h>
60 #include <sys/reboot.h>
61 #include <sys/rwlock.h>
62 #include <sys/sched.h>
63 #include <sys/signalvar.h>
64 #include <sys/syscallsubr.h>
65 #include <sys/sysent.h>
66 #include <sys/sysproto.h>
67 #include <sys/tslog.h>
68 #include <sys/ucontext.h>
69 #include <sys/vmmeter.h>
70
71 #include <vm/vm.h>
72 #include <vm/vm_param.h>
73 #include <vm/vm_kern.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_page.h>
76 #include <vm/vm_phys.h>
77 #include <vm/pmap.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_pager.h>
80
81 #include <machine/cpu.h>
82 #include <machine/intr.h>
83 #include <machine/kdb.h>
84 #include <machine/machdep.h>
85 #include <machine/metadata.h>
86 #include <machine/pcb.h>
87 #include <machine/pte.h>
88 #include <machine/reg.h>
89 #include <machine/riscvreg.h>
90 #include <machine/sbi.h>
91 #include <machine/trap.h>
92 #include <machine/vmparam.h>
93
94 #ifdef FPE
95 #include <machine/fpe.h>
96 #endif
97
98 #ifdef FDT
99 #include <contrib/libfdt/libfdt.h>
100 #include <dev/fdt/fdt_common.h>
101 #include <dev/ofw/openfirm.h>
102 #endif
103
104 static void get_fpcontext(struct thread *td, mcontext_t *mcp);
105 static void set_fpcontext(struct thread *td, mcontext_t *mcp);
106
107 struct pcpu __pcpu[MAXCPU];
108
109 static struct trapframe proc0_tf;
110
111 int early_boot = 1;
112 int cold = 1;
113
114 #define DTB_SIZE_MAX    (1024 * 1024)
115
116 vm_paddr_t physmap[PHYS_AVAIL_ENTRIES];
117 u_int physmap_idx;
118
119 struct kva_md_info kmi;
120
121 int64_t dcache_line_size;       /* The minimum D cache line size */
122 int64_t icache_line_size;       /* The minimum I cache line size */
123 int64_t idcache_line_size;      /* The minimum cache line size */
124
125 uint32_t boot_hart;     /* The hart we booted on. */
126 cpuset_t all_harts;
127
128 extern int *end;
129
130 static void
131 cpu_startup(void *dummy)
132 {
133
134         sbi_print_version();
135         identify_cpu();
136
137         printf("real memory  = %ju (%ju MB)\n", ptoa((uintmax_t)realmem),
138             ptoa((uintmax_t)realmem) / (1024 * 1024));
139
140         /*
141          * Display any holes after the first chunk of extended memory.
142          */
143         if (bootverbose) {
144                 int indx;
145
146                 printf("Physical memory chunk(s):\n");
147                 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
148                         vm_paddr_t size;
149
150                         size = phys_avail[indx + 1] - phys_avail[indx];
151                         printf(
152                             "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
153                             (uintmax_t)phys_avail[indx],
154                             (uintmax_t)phys_avail[indx + 1] - 1,
155                             (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
156                 }
157         }
158
159         vm_ksubmap_init(&kmi);
160
161         printf("avail memory = %ju (%ju MB)\n",
162             ptoa((uintmax_t)vm_free_count()),
163             ptoa((uintmax_t)vm_free_count()) / (1024 * 1024));
164         if (bootverbose)
165                 devmap_print_table();
166
167         bufinit();
168         vm_pager_bufferinit();
169 }
170
171 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
172
173 int
174 cpu_idle_wakeup(int cpu)
175 {
176
177         return (0);
178 }
179
180 int
181 fill_regs(struct thread *td, struct reg *regs)
182 {
183         struct trapframe *frame;
184
185         frame = td->td_frame;
186         regs->sepc = frame->tf_sepc;
187         regs->sstatus = frame->tf_sstatus;
188         regs->ra = frame->tf_ra;
189         regs->sp = frame->tf_sp;
190         regs->gp = frame->tf_gp;
191         regs->tp = frame->tf_tp;
192
193         memcpy(regs->t, frame->tf_t, sizeof(regs->t));
194         memcpy(regs->s, frame->tf_s, sizeof(regs->s));
195         memcpy(regs->a, frame->tf_a, sizeof(regs->a));
196
197         return (0);
198 }
199
200 int
201 set_regs(struct thread *td, struct reg *regs)
202 {
203         struct trapframe *frame;
204
205         frame = td->td_frame;
206         frame->tf_sepc = regs->sepc;
207         frame->tf_ra = regs->ra;
208         frame->tf_sp = regs->sp;
209         frame->tf_gp = regs->gp;
210         frame->tf_tp = regs->tp;
211
212         memcpy(frame->tf_t, regs->t, sizeof(frame->tf_t));
213         memcpy(frame->tf_s, regs->s, sizeof(frame->tf_s));
214         memcpy(frame->tf_a, regs->a, sizeof(frame->tf_a));
215
216         return (0);
217 }
218
219 int
220 fill_fpregs(struct thread *td, struct fpreg *regs)
221 {
222 #ifdef FPE
223         struct pcb *pcb;
224
225         pcb = td->td_pcb;
226
227         if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
228                 /*
229                  * If we have just been running FPE instructions we will
230                  * need to save the state to memcpy it below.
231                  */
232                 if (td == curthread)
233                         fpe_state_save(td);
234
235                 memcpy(regs->fp_x, pcb->pcb_x, sizeof(regs->fp_x));
236                 regs->fp_fcsr = pcb->pcb_fcsr;
237         } else
238 #endif
239                 memset(regs, 0, sizeof(*regs));
240
241         return (0);
242 }
243
244 int
245 set_fpregs(struct thread *td, struct fpreg *regs)
246 {
247 #ifdef FPE
248         struct trapframe *frame;
249         struct pcb *pcb;
250
251         frame = td->td_frame;
252         pcb = td->td_pcb;
253
254         memcpy(pcb->pcb_x, regs->fp_x, sizeof(regs->fp_x));
255         pcb->pcb_fcsr = regs->fp_fcsr;
256         pcb->pcb_fpflags |= PCB_FP_STARTED;
257         frame->tf_sstatus &= ~SSTATUS_FS_MASK;
258         frame->tf_sstatus |= SSTATUS_FS_CLEAN;
259 #endif
260
261         return (0);
262 }
263
264 int
265 fill_dbregs(struct thread *td, struct dbreg *regs)
266 {
267
268         panic("fill_dbregs");
269 }
270
271 int
272 set_dbregs(struct thread *td, struct dbreg *regs)
273 {
274
275         panic("set_dbregs");
276 }
277
278 int
279 ptrace_set_pc(struct thread *td, u_long addr)
280 {
281
282         td->td_frame->tf_sepc = addr;
283         return (0);
284 }
285
286 int
287 ptrace_single_step(struct thread *td)
288 {
289
290         /* TODO; */
291         return (EOPNOTSUPP);
292 }
293
294 int
295 ptrace_clear_single_step(struct thread *td)
296 {
297
298         /* TODO; */
299         return (EOPNOTSUPP);
300 }
301
302 void
303 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
304 {
305         struct trapframe *tf;
306         struct pcb *pcb;
307
308         tf = td->td_frame;
309         pcb = td->td_pcb;
310
311         memset(tf, 0, sizeof(struct trapframe));
312
313         tf->tf_a[0] = stack;
314         tf->tf_sp = STACKALIGN(stack);
315         tf->tf_ra = imgp->entry_addr;
316         tf->tf_sepc = imgp->entry_addr;
317
318         pcb->pcb_fpflags &= ~PCB_FP_STARTED;
319 }
320
321 /* Sanity check these are the same size, they will be memcpy'd to and fro */
322 CTASSERT(sizeof(((struct trapframe *)0)->tf_a) ==
323     sizeof((struct gpregs *)0)->gp_a);
324 CTASSERT(sizeof(((struct trapframe *)0)->tf_s) ==
325     sizeof((struct gpregs *)0)->gp_s);
326 CTASSERT(sizeof(((struct trapframe *)0)->tf_t) ==
327     sizeof((struct gpregs *)0)->gp_t);
328 CTASSERT(sizeof(((struct trapframe *)0)->tf_a) ==
329     sizeof((struct reg *)0)->a);
330 CTASSERT(sizeof(((struct trapframe *)0)->tf_s) ==
331     sizeof((struct reg *)0)->s);
332 CTASSERT(sizeof(((struct trapframe *)0)->tf_t) ==
333     sizeof((struct reg *)0)->t);
334
335 /* Support for FDT configurations only. */
336 CTASSERT(FDT);
337
338 int
339 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
340 {
341         struct trapframe *tf = td->td_frame;
342
343         memcpy(mcp->mc_gpregs.gp_t, tf->tf_t, sizeof(mcp->mc_gpregs.gp_t));
344         memcpy(mcp->mc_gpregs.gp_s, tf->tf_s, sizeof(mcp->mc_gpregs.gp_s));
345         memcpy(mcp->mc_gpregs.gp_a, tf->tf_a, sizeof(mcp->mc_gpregs.gp_a));
346
347         if (clear_ret & GET_MC_CLEAR_RET) {
348                 mcp->mc_gpregs.gp_a[0] = 0;
349                 mcp->mc_gpregs.gp_t[0] = 0; /* clear syscall error */
350         }
351
352         mcp->mc_gpregs.gp_ra = tf->tf_ra;
353         mcp->mc_gpregs.gp_sp = tf->tf_sp;
354         mcp->mc_gpregs.gp_gp = tf->tf_gp;
355         mcp->mc_gpregs.gp_tp = tf->tf_tp;
356         mcp->mc_gpregs.gp_sepc = tf->tf_sepc;
357         mcp->mc_gpregs.gp_sstatus = tf->tf_sstatus;
358         get_fpcontext(td, mcp);
359
360         return (0);
361 }
362
363 int
364 set_mcontext(struct thread *td, mcontext_t *mcp)
365 {
366         struct trapframe *tf;
367
368         tf = td->td_frame;
369
370         /*
371          * Permit changes to the USTATUS bits of SSTATUS.
372          *
373          * Ignore writes to read-only bits (SD, XS).
374          *
375          * Ignore writes to the FS field as set_fpcontext() will set
376          * it explicitly.
377          */
378         if (((mcp->mc_gpregs.gp_sstatus ^ tf->tf_sstatus) &
379             ~(SSTATUS_SD | SSTATUS_XS_MASK | SSTATUS_FS_MASK | SSTATUS_UPIE |
380             SSTATUS_UIE)) != 0)
381                 return (EINVAL);
382
383         memcpy(tf->tf_t, mcp->mc_gpregs.gp_t, sizeof(tf->tf_t));
384         memcpy(tf->tf_s, mcp->mc_gpregs.gp_s, sizeof(tf->tf_s));
385         memcpy(tf->tf_a, mcp->mc_gpregs.gp_a, sizeof(tf->tf_a));
386
387         tf->tf_ra = mcp->mc_gpregs.gp_ra;
388         tf->tf_sp = mcp->mc_gpregs.gp_sp;
389         tf->tf_gp = mcp->mc_gpregs.gp_gp;
390         tf->tf_sepc = mcp->mc_gpregs.gp_sepc;
391         tf->tf_sstatus = mcp->mc_gpregs.gp_sstatus;
392         set_fpcontext(td, mcp);
393
394         return (0);
395 }
396
397 static void
398 get_fpcontext(struct thread *td, mcontext_t *mcp)
399 {
400 #ifdef FPE
401         struct pcb *curpcb;
402
403         critical_enter();
404
405         curpcb = curthread->td_pcb;
406
407         KASSERT(td->td_pcb == curpcb, ("Invalid fpe pcb"));
408
409         if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
410                 /*
411                  * If we have just been running FPE instructions we will
412                  * need to save the state to memcpy it below.
413                  */
414                 fpe_state_save(td);
415
416                 KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
417                     ("Non-userspace FPE flags set in get_fpcontext"));
418                 memcpy(mcp->mc_fpregs.fp_x, curpcb->pcb_x,
419                     sizeof(mcp->mc_fpregs));
420                 mcp->mc_fpregs.fp_fcsr = curpcb->pcb_fcsr;
421                 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
422                 mcp->mc_flags |= _MC_FP_VALID;
423         }
424
425         critical_exit();
426 #endif
427 }
428
429 static void
430 set_fpcontext(struct thread *td, mcontext_t *mcp)
431 {
432 #ifdef FPE
433         struct pcb *curpcb;
434 #endif
435
436         td->td_frame->tf_sstatus &= ~SSTATUS_FS_MASK;
437         td->td_frame->tf_sstatus |= SSTATUS_FS_OFF;
438
439 #ifdef FPE
440         critical_enter();
441
442         if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
443                 curpcb = curthread->td_pcb;
444                 /* FPE usage is enabled, override registers. */
445                 memcpy(curpcb->pcb_x, mcp->mc_fpregs.fp_x,
446                     sizeof(mcp->mc_fpregs));
447                 curpcb->pcb_fcsr = mcp->mc_fpregs.fp_fcsr;
448                 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK;
449                 td->td_frame->tf_sstatus |= SSTATUS_FS_CLEAN;
450         }
451
452         critical_exit();
453 #endif
454 }
455
456 void
457 cpu_idle(int busy)
458 {
459
460         spinlock_enter();
461         if (!busy)
462                 cpu_idleclock();
463         if (!sched_runnable())
464                 __asm __volatile(
465                     "fence \n"
466                     "wfi   \n");
467         if (!busy)
468                 cpu_activeclock();
469         spinlock_exit();
470 }
471
472 void
473 cpu_halt(void)
474 {
475
476         intr_disable();
477         for (;;)
478                 __asm __volatile("wfi");
479 }
480
481 /*
482  * Flush the D-cache for non-DMA I/O so that the I-cache can
483  * be made coherent later.
484  */
485 void
486 cpu_flush_dcache(void *ptr, size_t len)
487 {
488
489         /* TBD */
490 }
491
492 /* Get current clock frequency for the given CPU ID. */
493 int
494 cpu_est_clockrate(int cpu_id, uint64_t *rate)
495 {
496
497         panic("cpu_est_clockrate");
498 }
499
500 void
501 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
502 {
503 }
504
505 void
506 spinlock_enter(void)
507 {
508         struct thread *td;
509         register_t reg;
510
511         td = curthread;
512         if (td->td_md.md_spinlock_count == 0) {
513                 reg = intr_disable();
514                 td->td_md.md_spinlock_count = 1;
515                 td->td_md.md_saved_sstatus_ie = reg;
516                 critical_enter();
517         } else
518                 td->td_md.md_spinlock_count++;
519 }
520
521 void
522 spinlock_exit(void)
523 {
524         struct thread *td;
525         register_t sstatus_ie;
526
527         td = curthread;
528         sstatus_ie = td->td_md.md_saved_sstatus_ie;
529         td->td_md.md_spinlock_count--;
530         if (td->td_md.md_spinlock_count == 0) {
531                 critical_exit();
532                 intr_restore(sstatus_ie);
533         }
534 }
535
536 #ifndef _SYS_SYSPROTO_H_
537 struct sigreturn_args {
538         ucontext_t *ucp;
539 };
540 #endif
541
542 int
543 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
544 {
545         ucontext_t uc;
546         int error;
547
548         if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
549                 return (EFAULT);
550
551         error = set_mcontext(td, &uc.uc_mcontext);
552         if (error != 0)
553                 return (error);
554
555         /* Restore signal mask. */
556         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
557
558         return (EJUSTRETURN);
559 }
560
561 /*
562  * Construct a PCB from a trapframe. This is called from kdb_trap() where
563  * we want to start a backtrace from the function that caused us to enter
564  * the debugger. We have the context in the trapframe, but base the trace
565  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
566  * enough for a backtrace.
567  */
568 void
569 makectx(struct trapframe *tf, struct pcb *pcb)
570 {
571
572         memcpy(pcb->pcb_s, tf->tf_s, sizeof(tf->tf_s));
573
574         pcb->pcb_ra = tf->tf_sepc;
575         pcb->pcb_sp = tf->tf_sp;
576         pcb->pcb_gp = tf->tf_gp;
577         pcb->pcb_tp = tf->tf_tp;
578 }
579
580 void
581 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
582 {
583         struct sigframe *fp, frame;
584         struct sysentvec *sysent;
585         struct trapframe *tf;
586         struct sigacts *psp;
587         struct thread *td;
588         struct proc *p;
589         int onstack;
590         int sig;
591
592         td = curthread;
593         p = td->td_proc;
594         PROC_LOCK_ASSERT(p, MA_OWNED);
595
596         sig = ksi->ksi_signo;
597         psp = p->p_sigacts;
598         mtx_assert(&psp->ps_mtx, MA_OWNED);
599
600         tf = td->td_frame;
601         onstack = sigonstack(tf->tf_sp);
602
603         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
604             catcher, sig);
605
606         /* Allocate and validate space for the signal handler context. */
607         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
608             SIGISMEMBER(psp->ps_sigonstack, sig)) {
609                 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
610                     td->td_sigstk.ss_size);
611         } else {
612                 fp = (struct sigframe *)td->td_frame->tf_sp;
613         }
614
615         /* Make room, keeping the stack aligned */
616         fp--;
617         fp = (struct sigframe *)STACKALIGN(fp);
618
619         /* Fill in the frame to copy out */
620         bzero(&frame, sizeof(frame));
621         get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
622         frame.sf_si = ksi->ksi_info;
623         frame.sf_uc.uc_sigmask = *mask;
624         frame.sf_uc.uc_stack = td->td_sigstk;
625         frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
626             (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
627         mtx_unlock(&psp->ps_mtx);
628         PROC_UNLOCK(td->td_proc);
629
630         /* Copy the sigframe out to the user's stack. */
631         if (copyout(&frame, fp, sizeof(*fp)) != 0) {
632                 /* Process has trashed its stack. Kill it. */
633                 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
634                 PROC_LOCK(p);
635                 sigexit(td, SIGILL);
636         }
637
638         tf->tf_a[0] = sig;
639         tf->tf_a[1] = (register_t)&fp->sf_si;
640         tf->tf_a[2] = (register_t)&fp->sf_uc;
641
642         tf->tf_sepc = (register_t)catcher;
643         tf->tf_sp = (register_t)fp;
644
645         sysent = p->p_sysent;
646         if (sysent->sv_sigcode_base != 0)
647                 tf->tf_ra = (register_t)sysent->sv_sigcode_base;
648         else
649                 tf->tf_ra = (register_t)(sysent->sv_psstrings -
650                     *(sysent->sv_szsigcode));
651
652         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_sepc,
653             tf->tf_sp);
654
655         PROC_LOCK(p);
656         mtx_lock(&psp->ps_mtx);
657 }
658
659 static void
660 init_proc0(vm_offset_t kstack)
661 {
662         struct pcpu *pcpup;
663
664         pcpup = &__pcpu[0];
665
666         proc_linkup0(&proc0, &thread0);
667         thread0.td_kstack = kstack;
668         thread0.td_kstack_pages = KSTACK_PAGES;
669         thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
670             thread0.td_kstack_pages * PAGE_SIZE) - 1;
671         thread0.td_pcb->pcb_fpflags = 0;
672         thread0.td_frame = &proc0_tf;
673         pcpup->pc_curpcb = thread0.td_pcb;
674 }
675
676 #ifdef FDT
677 static void
678 try_load_dtb(caddr_t kmdp)
679 {
680         vm_offset_t dtbp;
681
682         dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
683
684 #if defined(FDT_DTB_STATIC)
685         /*
686          * In case the device tree blob was not retrieved (from metadata) try
687          * to use the statically embedded one.
688          */
689         if (dtbp == (vm_offset_t)NULL)
690                 dtbp = (vm_offset_t)&fdt_static_dtb;
691 #endif
692
693         if (dtbp == (vm_offset_t)NULL) {
694                 printf("ERROR loading DTB\n");
695                 return;
696         }
697
698         if (OF_install(OFW_FDT, 0) == FALSE)
699                 panic("Cannot install FDT");
700
701         if (OF_init((void *)dtbp) != 0)
702                 panic("OF_init failed with the found device tree");
703 }
704 #endif
705
706 static void
707 cache_setup(void)
708 {
709
710         /* TODO */
711
712         dcache_line_size = 0;
713         icache_line_size = 0;
714         idcache_line_size = 0;
715 }
716
717 /*
718  * Fake up a boot descriptor table.
719  * RISCVTODO: This needs to be done via loader (when it's available).
720  */
721 vm_offset_t
722 fake_preload_metadata(struct riscv_bootparams *rvbp)
723 {
724         static uint32_t fake_preload[35];
725 #ifdef DDB
726         vm_offset_t zstart = 0, zend = 0;
727 #endif
728         vm_offset_t lastaddr;
729         size_t dtb_size;
730         int i;
731
732         i = 0;
733
734         fake_preload[i++] = MODINFO_NAME;
735         fake_preload[i++] = strlen("kernel") + 1;
736         strcpy((char*)&fake_preload[i++], "kernel");
737         i += 1;
738         fake_preload[i++] = MODINFO_TYPE;
739         fake_preload[i++] = strlen("elf64 kernel") + 1;
740         strcpy((char*)&fake_preload[i++], "elf64 kernel");
741         i += 3;
742         fake_preload[i++] = MODINFO_ADDR;
743         fake_preload[i++] = sizeof(vm_offset_t);
744         *(vm_offset_t *)&fake_preload[i++] =
745             (vm_offset_t)(KERNBASE + KERNENTRY);
746         i += 1;
747         fake_preload[i++] = MODINFO_SIZE;
748         fake_preload[i++] = sizeof(vm_offset_t);
749         fake_preload[i++] = (vm_offset_t)&end -
750             (vm_offset_t)(KERNBASE + KERNENTRY);
751         i += 1;
752 #ifdef DDB
753 #if 0
754         /* RISCVTODO */
755         if (*(uint32_t *)KERNVIRTADDR == MAGIC_TRAMP_NUMBER) {
756                 fake_preload[i++] = MODINFO_METADATA|MODINFOMD_SSYM;
757                 fake_preload[i++] = sizeof(vm_offset_t);
758                 fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 4);
759                 fake_preload[i++] = MODINFO_METADATA|MODINFOMD_ESYM;
760                 fake_preload[i++] = sizeof(vm_offset_t);
761                 fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 8);
762                 lastaddr = *(uint32_t *)(KERNVIRTADDR + 8);
763                 zend = lastaddr;
764                 zstart = *(uint32_t *)(KERNVIRTADDR + 4);
765                 db_fetch_ksymtab(zstart, zend);
766         } else
767 #endif
768 #endif
769                 lastaddr = (vm_offset_t)&end;
770
771         /* Copy the DTB to KVA space. */
772         lastaddr = roundup(lastaddr, sizeof(int));
773         fake_preload[i++] = MODINFO_METADATA | MODINFOMD_DTBP;
774         fake_preload[i++] = sizeof(vm_offset_t);
775         *(vm_offset_t *)&fake_preload[i] = (vm_offset_t)lastaddr;
776         i += sizeof(vm_offset_t) / sizeof(uint32_t);
777         dtb_size = fdt_totalsize(rvbp->dtbp_virt);
778         memmove((void *)lastaddr, (const void *)rvbp->dtbp_virt, dtb_size);
779         lastaddr = roundup(lastaddr + dtb_size, sizeof(int));
780
781         fake_preload[i++] = 0;
782         fake_preload[i] = 0;
783         preload_metadata = (void *)fake_preload;
784
785         KASSERT(i < nitems(fake_preload), ("Too many fake_preload items"));
786
787         return (lastaddr);
788 }
789
790 void
791 initriscv(struct riscv_bootparams *rvbp)
792 {
793         struct mem_region mem_regions[FDT_MEM_REGIONS];
794         struct pcpu *pcpup;
795         int mem_regions_sz;
796         vm_offset_t lastaddr;
797         vm_size_t kernlen;
798         caddr_t kmdp;
799
800         TSRAW(&thread0, TS_ENTER, __func__, NULL);
801
802         /* Set the pcpu data, this is needed by pmap_bootstrap */
803         pcpup = &__pcpu[0];
804         pcpu_init(pcpup, 0, sizeof(struct pcpu));
805         pcpup->pc_hart = boot_hart;
806
807         /* Set the pcpu pointer */
808         __asm __volatile("mv tp, %0" :: "r"(pcpup));
809
810         PCPU_SET(curthread, &thread0);
811
812         /* Initialize SBI interface. */
813         sbi_init();
814
815         /* Set the module data location */
816         lastaddr = fake_preload_metadata(rvbp);
817
818         /* Find the kernel address */
819         kmdp = preload_search_by_type("elf kernel");
820         if (kmdp == NULL)
821                 kmdp = preload_search_by_type("elf64 kernel");
822
823         boothowto = RB_VERBOSE | RB_SINGLE;
824         boothowto = RB_VERBOSE;
825
826         kern_envp = NULL;
827
828 #ifdef FDT
829         try_load_dtb(kmdp);
830
831         /*
832          * Exclude reserved memory specified by the device tree. Typically,
833          * this contains an entry for memory used by the runtime SBI firmware.
834          */
835         if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0) {
836                 physmem_exclude_regions(mem_regions, mem_regions_sz,
837                     EXFLAG_NODUMP | EXFLAG_NOALLOC);
838         }
839
840         /* Grab physical memory regions information from device tree. */
841         if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, NULL) != 0) {
842                 panic("Cannot get physical memory regions");
843         }
844         physmem_hardware_regions(mem_regions, mem_regions_sz);
845 #endif
846
847         /* Do basic tuning, hz etc */
848         init_param1();
849
850         cache_setup();
851
852         /* Bootstrap enough of pmap to enter the kernel proper */
853         kernlen = (lastaddr - KERNBASE);
854         pmap_bootstrap(rvbp->kern_l1pt, rvbp->kern_phys, kernlen);
855
856 #ifdef FDT
857         /*
858          * XXX: Exclude the lowest 2MB of physical memory, if it hasn't been
859          * already, as this area is assumed to contain the SBI firmware. This
860          * is a little fragile, but it is consistent with the platforms we
861          * support so far.
862          *
863          * TODO: remove this when the all regular booting methods properly
864          * report their reserved memory in the device tree.
865          */
866         if (mem_regions[0].mr_start == physmap[0]) {
867                 physmem_exclude_region(mem_regions[0].mr_start, L2_SIZE,
868                     EXFLAG_NODUMP | EXFLAG_NOALLOC);
869         }
870 #endif
871         physmem_init_kernel_globals();
872
873         /* Establish static device mappings */
874         devmap_bootstrap(0, NULL);
875
876         cninit();
877
878         init_proc0(rvbp->kern_stack);
879
880         msgbufinit(msgbufp, msgbufsize);
881         mutex_init();
882         init_param2(physmem);
883         kdb_init();
884
885         if (boothowto & RB_VERBOSE)
886                 physmem_print_tables();
887
888         early_boot = 0;
889
890         TSEXIT();
891 }
892
893 #undef bzero
894 void
895 bzero(void *buf, size_t len)
896 {
897         uint8_t *p;
898
899         p = buf;
900         while(len-- > 0)
901                 *p++ = 0;
902 }