]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/machdep.c
Upgrade Unbound to 1.7.3. More to follow.
[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/exec.h>
48 #include <sys/imgact.h>
49 #include <sys/kdb.h>
50 #include <sys/kernel.h>
51 #include <sys/limits.h>
52 #include <sys/linker.h>
53 #include <sys/msgbuf.h>
54 #include <sys/pcpu.h>
55 #include <sys/proc.h>
56 #include <sys/ptrace.h>
57 #include <sys/reboot.h>
58 #include <sys/rwlock.h>
59 #include <sys/sched.h>
60 #include <sys/signalvar.h>
61 #include <sys/syscallsubr.h>
62 #include <sys/sysent.h>
63 #include <sys/sysproto.h>
64 #include <sys/ucontext.h>
65
66 #include <vm/vm.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page.h>
70 #include <vm/pmap.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_pager.h>
73
74 #include <machine/riscvreg.h>
75 #include <machine/cpu.h>
76 #include <machine/kdb.h>
77 #include <machine/machdep.h>
78 #include <machine/pcb.h>
79 #include <machine/reg.h>
80 #include <machine/trap.h>
81 #include <machine/vmparam.h>
82 #include <machine/intr.h>
83 #include <machine/sbi.h>
84
85 #include <machine/asm.h>
86
87 #ifdef FPE
88 #include <machine/fpe.h>
89 #endif
90
91 #ifdef FDT
92 #include <dev/fdt/fdt_common.h>
93 #include <dev/ofw/openfirm.h>
94 #endif
95
96 struct pcpu __pcpu[MAXCPU];
97
98 static struct trapframe proc0_tf;
99
100 vm_paddr_t phys_avail[PHYS_AVAIL_SIZE + 2];
101 vm_paddr_t dump_avail[PHYS_AVAIL_SIZE + 2];
102
103 int early_boot = 1;
104 int cold = 1;
105 long realmem = 0;
106 long Maxmem = 0;
107
108 #define DTB_SIZE_MAX    (1024 * 1024)
109
110 #define PHYSMAP_SIZE    (2 * (VM_PHYSSEG_MAX - 1))
111 vm_paddr_t physmap[PHYSMAP_SIZE];
112 u_int physmap_idx;
113
114 struct kva_md_info kmi;
115
116 int64_t dcache_line_size;       /* The minimum D cache line size */
117 int64_t icache_line_size;       /* The minimum I cache line size */
118 int64_t idcache_line_size;      /* The minimum cache line size */
119
120 extern int *end;
121 extern int *initstack_end;
122
123 struct pcpu *pcpup;
124
125 uintptr_t mcall_trap(uintptr_t mcause, uintptr_t* regs);
126
127 uintptr_t
128 mcall_trap(uintptr_t mcause, uintptr_t* regs)
129 {
130
131         return (0);
132 }
133
134 static void
135 cpu_startup(void *dummy)
136 {
137
138         identify_cpu();
139
140         vm_ksubmap_init(&kmi);
141         bufinit();
142         vm_pager_bufferinit();
143 }
144
145 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
146
147 int
148 cpu_idle_wakeup(int cpu)
149 {
150
151         return (0);
152 }
153
154 int
155 fill_regs(struct thread *td, struct reg *regs)
156 {
157         struct trapframe *frame;
158
159         frame = td->td_frame;
160         regs->sepc = frame->tf_sepc;
161         regs->sstatus = frame->tf_sstatus;
162         regs->ra = frame->tf_ra;
163         regs->sp = frame->tf_sp;
164         regs->gp = frame->tf_gp;
165         regs->tp = frame->tf_tp;
166
167         memcpy(regs->t, frame->tf_t, sizeof(regs->t));
168         memcpy(regs->s, frame->tf_s, sizeof(regs->s));
169         memcpy(regs->a, frame->tf_a, sizeof(regs->a));
170
171         return (0);
172 }
173
174 int
175 set_regs(struct thread *td, struct reg *regs)
176 {
177         struct trapframe *frame;
178
179         frame = td->td_frame;
180         frame->tf_sepc = regs->sepc;
181         frame->tf_sstatus = regs->sstatus;
182         frame->tf_ra = regs->ra;
183         frame->tf_sp = regs->sp;
184         frame->tf_gp = regs->gp;
185         frame->tf_tp = regs->tp;
186
187         memcpy(frame->tf_t, regs->t, sizeof(frame->tf_t));
188         memcpy(frame->tf_s, regs->s, sizeof(frame->tf_s));
189         memcpy(frame->tf_a, regs->a, sizeof(frame->tf_a));
190
191         return (0);
192 }
193
194 int
195 fill_fpregs(struct thread *td, struct fpreg *regs)
196 {
197 #ifdef FPE
198         struct pcb *pcb;
199
200         pcb = td->td_pcb;
201
202         if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
203                 /*
204                  * If we have just been running FPE instructions we will
205                  * need to save the state to memcpy it below.
206                  */
207                 fpe_state_save(td);
208
209                 memcpy(regs->fp_x, pcb->pcb_x, sizeof(regs->fp_x));
210                 regs->fp_fcsr = pcb->pcb_fcsr;
211         } else
212 #endif
213                 memset(regs->fp_x, 0, sizeof(regs->fp_x));
214
215         return (0);
216 }
217
218 int
219 set_fpregs(struct thread *td, struct fpreg *regs)
220 {
221 #ifdef FPE
222         struct pcb *pcb;
223
224         pcb = td->td_pcb;
225
226         memcpy(pcb->pcb_x, regs->fp_x, sizeof(regs->fp_x));
227         pcb->pcb_fcsr = regs->fp_fcsr;
228 #endif
229
230         return (0);
231 }
232
233 int
234 fill_dbregs(struct thread *td, struct dbreg *regs)
235 {
236
237         panic("fill_dbregs");
238 }
239
240 int
241 set_dbregs(struct thread *td, struct dbreg *regs)
242 {
243
244         panic("set_dbregs");
245 }
246
247 int
248 ptrace_set_pc(struct thread *td, u_long addr)
249 {
250
251         panic("ptrace_set_pc");
252         return (0);
253 }
254
255 int
256 ptrace_single_step(struct thread *td)
257 {
258
259         /* TODO; */
260         return (0);
261 }
262
263 int
264 ptrace_clear_single_step(struct thread *td)
265 {
266
267         /* TODO; */
268         return (0);
269 }
270
271 void
272 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
273 {
274         struct trapframe *tf;
275         struct pcb *pcb;
276
277         tf = td->td_frame;
278         pcb = td->td_pcb;
279
280         memset(tf, 0, sizeof(struct trapframe));
281
282         tf->tf_a[0] = stack;
283         tf->tf_sp = STACKALIGN(stack);
284         tf->tf_ra = imgp->entry_addr;
285         tf->tf_sepc = imgp->entry_addr;
286
287         pcb->pcb_fpflags &= ~PCB_FP_STARTED;
288 }
289
290 /* Sanity check these are the same size, they will be memcpy'd to and fro */
291 CTASSERT(sizeof(((struct trapframe *)0)->tf_a) ==
292     sizeof((struct gpregs *)0)->gp_a);
293 CTASSERT(sizeof(((struct trapframe *)0)->tf_s) ==
294     sizeof((struct gpregs *)0)->gp_s);
295 CTASSERT(sizeof(((struct trapframe *)0)->tf_t) ==
296     sizeof((struct gpregs *)0)->gp_t);
297 CTASSERT(sizeof(((struct trapframe *)0)->tf_a) ==
298     sizeof((struct reg *)0)->a);
299 CTASSERT(sizeof(((struct trapframe *)0)->tf_s) ==
300     sizeof((struct reg *)0)->s);
301 CTASSERT(sizeof(((struct trapframe *)0)->tf_t) ==
302     sizeof((struct reg *)0)->t);
303
304 /* Support for FDT configurations only. */
305 CTASSERT(FDT);
306
307 int
308 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
309 {
310         struct trapframe *tf = td->td_frame;
311
312         memcpy(mcp->mc_gpregs.gp_t, tf->tf_t, sizeof(mcp->mc_gpregs.gp_t));
313         memcpy(mcp->mc_gpregs.gp_s, tf->tf_s, sizeof(mcp->mc_gpregs.gp_s));
314         memcpy(mcp->mc_gpregs.gp_a, tf->tf_a, sizeof(mcp->mc_gpregs.gp_a));
315
316         if (clear_ret & GET_MC_CLEAR_RET) {
317                 mcp->mc_gpregs.gp_a[0] = 0;
318                 mcp->mc_gpregs.gp_t[0] = 0; /* clear syscall error */
319         }
320
321         mcp->mc_gpregs.gp_ra = tf->tf_ra;
322         mcp->mc_gpregs.gp_sp = tf->tf_sp;
323         mcp->mc_gpregs.gp_gp = tf->tf_gp;
324         mcp->mc_gpregs.gp_tp = tf->tf_tp;
325         mcp->mc_gpregs.gp_sepc = tf->tf_sepc;
326         mcp->mc_gpregs.gp_sstatus = tf->tf_sstatus;
327
328         return (0);
329 }
330
331 int
332 set_mcontext(struct thread *td, mcontext_t *mcp)
333 {
334         struct trapframe *tf;
335
336         tf = td->td_frame;
337
338         memcpy(tf->tf_t, mcp->mc_gpregs.gp_t, sizeof(tf->tf_t));
339         memcpy(tf->tf_s, mcp->mc_gpregs.gp_s, sizeof(tf->tf_s));
340         memcpy(tf->tf_a, mcp->mc_gpregs.gp_a, sizeof(tf->tf_a));
341
342         tf->tf_ra = mcp->mc_gpregs.gp_ra;
343         tf->tf_sp = mcp->mc_gpregs.gp_sp;
344         tf->tf_gp = mcp->mc_gpregs.gp_gp;
345         tf->tf_sepc = mcp->mc_gpregs.gp_sepc;
346         tf->tf_sstatus = mcp->mc_gpregs.gp_sstatus;
347
348         return (0);
349 }
350
351 static void
352 get_fpcontext(struct thread *td, mcontext_t *mcp)
353 {
354 #ifdef FPE
355         struct pcb *curpcb;
356
357         critical_enter();
358
359         curpcb = curthread->td_pcb;
360
361         KASSERT(td->td_pcb == curpcb, ("Invalid fpe pcb"));
362
363         if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
364                 /*
365                  * If we have just been running FPE instructions we will
366                  * need to save the state to memcpy it below.
367                  */
368                 fpe_state_save(td);
369
370                 KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
371                     ("Non-userspace FPE flags set in get_fpcontext"));
372                 memcpy(mcp->mc_fpregs.fp_x, curpcb->pcb_x,
373                     sizeof(mcp->mc_fpregs));
374                 mcp->mc_fpregs.fp_fcsr = curpcb->pcb_fcsr;
375                 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
376                 mcp->mc_flags |= _MC_FP_VALID;
377         }
378
379         critical_exit();
380 #endif
381 }
382
383 static void
384 set_fpcontext(struct thread *td, mcontext_t *mcp)
385 {
386 #ifdef FPE
387         struct pcb *curpcb;
388
389         critical_enter();
390
391         if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
392                 curpcb = curthread->td_pcb;
393                 /* FPE usage is enabled, override registers. */
394                 memcpy(curpcb->pcb_x, mcp->mc_fpregs.fp_x,
395                     sizeof(mcp->mc_fpregs));
396                 curpcb->pcb_fcsr = mcp->mc_fpregs.fp_fcsr;
397                 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK;
398         }
399
400         critical_exit();
401 #endif
402 }
403
404 void
405 cpu_idle(int busy)
406 {
407
408         spinlock_enter();
409         if (!busy)
410                 cpu_idleclock();
411         if (!sched_runnable())
412                 __asm __volatile(
413                     "fence \n"
414                     "wfi   \n");
415         if (!busy)
416                 cpu_activeclock();
417         spinlock_exit();
418 }
419
420 void
421 cpu_halt(void)
422 {
423
424         panic("cpu_halt");
425 }
426
427 /*
428  * Flush the D-cache for non-DMA I/O so that the I-cache can
429  * be made coherent later.
430  */
431 void
432 cpu_flush_dcache(void *ptr, size_t len)
433 {
434
435         /* TBD */
436 }
437
438 /* Get current clock frequency for the given CPU ID. */
439 int
440 cpu_est_clockrate(int cpu_id, uint64_t *rate)
441 {
442
443         panic("cpu_est_clockrate");
444 }
445
446 void
447 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
448 {
449 }
450
451 void
452 spinlock_enter(void)
453 {
454         struct thread *td;
455
456         td = curthread;
457         if (td->td_md.md_spinlock_count == 0) {
458                 td->td_md.md_spinlock_count = 1;
459                 td->td_md.md_saved_sstatus_ie = intr_disable();
460         } else
461                 td->td_md.md_spinlock_count++;
462         critical_enter();
463 }
464
465 void
466 spinlock_exit(void)
467 {
468         struct thread *td;
469         register_t sstatus_ie;
470
471         td = curthread;
472         critical_exit();
473         sstatus_ie = td->td_md.md_saved_sstatus_ie;
474         td->td_md.md_spinlock_count--;
475         if (td->td_md.md_spinlock_count == 0)
476                 intr_restore(sstatus_ie);
477 }
478
479 #ifndef _SYS_SYSPROTO_H_
480 struct sigreturn_args {
481         ucontext_t *ucp;
482 };
483 #endif
484
485 int
486 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
487 {
488         uint64_t sstatus;
489         ucontext_t uc;
490         int error;
491
492         if (uap == NULL)
493                 return (EFAULT);
494         if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
495                 return (EFAULT);
496
497         /*
498          * Make sure the processor mode has not been tampered with and
499          * interrupts have not been disabled.
500          * Supervisor interrupts in user mode are always enabled.
501          */
502         sstatus = uc.uc_mcontext.mc_gpregs.gp_sstatus;
503         if ((sstatus & SSTATUS_SPP) != 0)
504                 return (EINVAL);
505
506         error = set_mcontext(td, &uc.uc_mcontext);
507         if (error != 0)
508                 return (error);
509
510         set_fpcontext(td, &uc.uc_mcontext);
511
512         /* Restore signal mask. */
513         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
514
515         return (EJUSTRETURN);
516 }
517
518 /*
519  * Construct a PCB from a trapframe. This is called from kdb_trap() where
520  * we want to start a backtrace from the function that caused us to enter
521  * the debugger. We have the context in the trapframe, but base the trace
522  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
523  * enough for a backtrace.
524  */
525 void
526 makectx(struct trapframe *tf, struct pcb *pcb)
527 {
528
529         memcpy(pcb->pcb_t, tf->tf_t, sizeof(tf->tf_t));
530         memcpy(pcb->pcb_s, tf->tf_s, sizeof(tf->tf_s));
531         memcpy(pcb->pcb_a, tf->tf_a, sizeof(tf->tf_a));
532
533         pcb->pcb_ra = tf->tf_ra;
534         pcb->pcb_sp = tf->tf_sp;
535         pcb->pcb_gp = tf->tf_gp;
536         pcb->pcb_tp = tf->tf_tp;
537         pcb->pcb_sepc = tf->tf_sepc;
538 }
539
540 void
541 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
542 {
543         struct sigframe *fp, frame;
544         struct sysentvec *sysent;
545         struct trapframe *tf;
546         struct sigacts *psp;
547         struct thread *td;
548         struct proc *p;
549         int onstack;
550         int sig;
551
552         td = curthread;
553         p = td->td_proc;
554         PROC_LOCK_ASSERT(p, MA_OWNED);
555
556         sig = ksi->ksi_signo;
557         psp = p->p_sigacts;
558         mtx_assert(&psp->ps_mtx, MA_OWNED);
559
560         tf = td->td_frame;
561         onstack = sigonstack(tf->tf_sp);
562
563         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
564             catcher, sig);
565
566         /* Allocate and validate space for the signal handler context. */
567         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
568             SIGISMEMBER(psp->ps_sigonstack, sig)) {
569                 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
570                     td->td_sigstk.ss_size);
571         } else {
572                 fp = (struct sigframe *)td->td_frame->tf_sp;
573         }
574
575         /* Make room, keeping the stack aligned */
576         fp--;
577         fp = (struct sigframe *)STACKALIGN(fp);
578
579         /* Fill in the frame to copy out */
580         get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
581         get_fpcontext(td, &frame.sf_uc.uc_mcontext);
582         frame.sf_si = ksi->ksi_info;
583         frame.sf_uc.uc_sigmask = *mask;
584         frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) ?
585             ((onstack) ? SS_ONSTACK : 0) : SS_DISABLE;
586         frame.sf_uc.uc_stack = td->td_sigstk;
587         mtx_unlock(&psp->ps_mtx);
588         PROC_UNLOCK(td->td_proc);
589
590         /* Copy the sigframe out to the user's stack. */
591         if (copyout(&frame, fp, sizeof(*fp)) != 0) {
592                 /* Process has trashed its stack. Kill it. */
593                 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
594                 PROC_LOCK(p);
595                 sigexit(td, SIGILL);
596         }
597
598         tf->tf_a[0] = sig;
599         tf->tf_a[1] = (register_t)&fp->sf_si;
600         tf->tf_a[2] = (register_t)&fp->sf_uc;
601
602         tf->tf_sepc = (register_t)catcher;
603         tf->tf_sp = (register_t)fp;
604
605         sysent = p->p_sysent;
606         if (sysent->sv_sigcode_base != 0)
607                 tf->tf_ra = (register_t)sysent->sv_sigcode_base;
608         else
609                 tf->tf_ra = (register_t)(sysent->sv_psstrings -
610                     *(sysent->sv_szsigcode));
611
612         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_sepc,
613             tf->tf_sp);
614
615         PROC_LOCK(p);
616         mtx_lock(&psp->ps_mtx);
617 }
618
619 static void
620 init_proc0(vm_offset_t kstack)
621 {
622
623         pcpup = &__pcpu[0];
624
625         proc_linkup0(&proc0, &thread0);
626         thread0.td_kstack = kstack;
627         thread0.td_pcb = (struct pcb *)(thread0.td_kstack) - 1;
628         thread0.td_pcb->pcb_fpflags = 0;
629         thread0.td_frame = &proc0_tf;
630         pcpup->pc_curpcb = thread0.td_pcb;
631 }
632
633 static int
634 add_physmap_entry(uint64_t base, uint64_t length, vm_paddr_t *physmap,
635     u_int *physmap_idxp)
636 {
637         u_int i, insert_idx, _physmap_idx;
638
639         _physmap_idx = *physmap_idxp;
640
641         if (length == 0)
642                 return (1);
643
644         /*
645          * Find insertion point while checking for overlap.  Start off by
646          * assuming the new entry will be added to the end.
647          */
648         insert_idx = _physmap_idx;
649         for (i = 0; i <= _physmap_idx; i += 2) {
650                 if (base < physmap[i + 1]) {
651                         if (base + length <= physmap[i]) {
652                                 insert_idx = i;
653                                 break;
654                         }
655                         if (boothowto & RB_VERBOSE)
656                                 printf(
657                     "Overlapping memory regions, ignoring second region\n");
658                         return (1);
659                 }
660         }
661
662         /* See if we can prepend to the next entry. */
663         if (insert_idx <= _physmap_idx &&
664             base + length == physmap[insert_idx]) {
665                 physmap[insert_idx] = base;
666                 return (1);
667         }
668
669         /* See if we can append to the previous entry. */
670         if (insert_idx > 0 && base == physmap[insert_idx - 1]) {
671                 physmap[insert_idx - 1] += length;
672                 return (1);
673         }
674
675         _physmap_idx += 2;
676         *physmap_idxp = _physmap_idx;
677         if (_physmap_idx == PHYSMAP_SIZE) {
678                 printf(
679                 "Too many segments in the physical address map, giving up\n");
680                 return (0);
681         }
682
683         /*
684          * Move the last 'N' entries down to make room for the new
685          * entry if needed.
686          */
687         for (i = _physmap_idx; i > insert_idx; i -= 2) {
688                 physmap[i] = physmap[i - 2];
689                 physmap[i + 1] = physmap[i - 1];
690         }
691
692         /* Insert the new entry. */
693         physmap[insert_idx] = base;
694         physmap[insert_idx + 1] = base + length;
695
696         printf("physmap[%d] = 0x%016lx\n", insert_idx, base);
697         printf("physmap[%d] = 0x%016lx\n", insert_idx + 1, base + length);
698         return (1);
699 }
700
701 #ifdef FDT
702 static void
703 try_load_dtb(caddr_t kmdp, vm_offset_t dtbp)
704 {
705
706 #if defined(FDT_DTB_STATIC)
707         dtbp = (vm_offset_t)&fdt_static_dtb;
708 #endif
709
710         if (dtbp == (vm_offset_t)NULL) {
711                 printf("ERROR loading DTB\n");
712                 return;
713         }
714
715         if (OF_install(OFW_FDT, 0) == FALSE)
716                 panic("Cannot install FDT");
717
718         if (OF_init((void *)dtbp) != 0)
719                 panic("OF_init failed with the found device tree");
720 }
721 #endif
722
723 static void
724 cache_setup(void)
725 {
726
727         /* TODO */
728 }
729
730 /*
731  * Fake up a boot descriptor table.
732  * RISCVTODO: This needs to be done via loader (when it's available).
733  */
734 vm_offset_t
735 fake_preload_metadata(struct riscv_bootparams *rvbp __unused)
736 {
737         static uint32_t fake_preload[35];
738 #ifdef DDB
739         vm_offset_t zstart = 0, zend = 0;
740 #endif
741         vm_offset_t lastaddr;
742         int i;
743
744         i = 0;
745
746         fake_preload[i++] = MODINFO_NAME;
747         fake_preload[i++] = strlen("kernel") + 1;
748         strcpy((char*)&fake_preload[i++], "kernel");
749         i += 1;
750         fake_preload[i++] = MODINFO_TYPE;
751         fake_preload[i++] = strlen("elf64 kernel") + 1;
752         strcpy((char*)&fake_preload[i++], "elf64 kernel");
753         i += 3;
754         fake_preload[i++] = MODINFO_ADDR;
755         fake_preload[i++] = sizeof(vm_offset_t);
756         *(vm_offset_t *)&fake_preload[i++] =
757             (vm_offset_t)(KERNBASE + KERNENTRY);
758         i += 1;
759         fake_preload[i++] = MODINFO_SIZE;
760         fake_preload[i++] = sizeof(vm_offset_t);
761         fake_preload[i++] = (vm_offset_t)&end -
762             (vm_offset_t)(KERNBASE + KERNENTRY);
763         i += 1;
764 #ifdef DDB
765 #if 0
766         /* RISCVTODO */
767         if (*(uint32_t *)KERNVIRTADDR == MAGIC_TRAMP_NUMBER) {
768                 fake_preload[i++] = MODINFO_METADATA|MODINFOMD_SSYM;
769                 fake_preload[i++] = sizeof(vm_offset_t);
770                 fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 4);
771                 fake_preload[i++] = MODINFO_METADATA|MODINFOMD_ESYM;
772                 fake_preload[i++] = sizeof(vm_offset_t);
773                 fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 8);
774                 lastaddr = *(uint32_t *)(KERNVIRTADDR + 8);
775                 zend = lastaddr;
776                 zstart = *(uint32_t *)(KERNVIRTADDR + 4);
777                 db_fetch_ksymtab(zstart, zend);
778         } else
779 #endif
780 #endif
781                 lastaddr = (vm_offset_t)&end;
782         fake_preload[i++] = 0;
783         fake_preload[i] = 0;
784         preload_metadata = (void *)fake_preload;
785
786         return (lastaddr);
787 }
788
789 void
790 initriscv(struct riscv_bootparams *rvbp)
791 {
792         struct mem_region mem_regions[FDT_MEM_REGIONS];
793         vm_offset_t rstart, rend;
794         vm_offset_t s, e;
795         int mem_regions_sz;
796         vm_offset_t lastaddr;
797         vm_size_t kernlen;
798         caddr_t kmdp;
799         int i;
800
801         /* Set the module data location */
802         lastaddr = fake_preload_metadata(rvbp);
803
804         /* Find the kernel address */
805         kmdp = preload_search_by_type("elf kernel");
806         if (kmdp == NULL)
807                 kmdp = preload_search_by_type("elf64 kernel");
808
809         boothowto = RB_VERBOSE | RB_SINGLE;
810         boothowto = RB_VERBOSE;
811
812         kern_envp = NULL;
813
814 #ifdef FDT
815         try_load_dtb(kmdp, rvbp->dtbp_virt);
816 #endif
817
818         /* Load the physical memory ranges */
819         physmap_idx = 0;
820
821 #ifdef FDT
822         /* Grab physical memory regions information from device tree. */
823         if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, NULL) != 0)
824                 panic("Cannot get physical memory regions");
825
826         s = rvbp->dtbp_phys;
827         e = s + DTB_SIZE_MAX;
828
829         for (i = 0; i < mem_regions_sz; i++) {
830                 rstart = mem_regions[i].mr_start;
831                 rend = (mem_regions[i].mr_start + mem_regions[i].mr_size);
832
833                 if ((rstart < s) && (rend > e)) {
834                         /* Exclude DTB region. */
835                         add_physmap_entry(rstart, (s - rstart), physmap, &physmap_idx);
836                         add_physmap_entry(e, (rend - e), physmap, &physmap_idx);
837                 } else {
838                         add_physmap_entry(mem_regions[i].mr_start,
839                             mem_regions[i].mr_size, physmap, &physmap_idx);
840                 }
841         }
842 #endif
843
844         /* Set the pcpu data, this is needed by pmap_bootstrap */
845         pcpup = &__pcpu[0];
846         pcpu_init(pcpup, 0, sizeof(struct pcpu));
847
848         /* Set the pcpu pointer */
849         __asm __volatile("mv gp, %0" :: "r"(pcpup));
850
851         PCPU_SET(curthread, &thread0);
852
853         /* Do basic tuning, hz etc */
854         init_param1();
855
856         cache_setup();
857
858         /* Bootstrap enough of pmap to enter the kernel proper */
859         kernlen = (lastaddr - KERNBASE);
860         pmap_bootstrap(rvbp->kern_l1pt, mem_regions[0].mr_start, kernlen);
861
862         cninit();
863
864         init_proc0(rvbp->kern_stack);
865
866         /* set page table base register for thread0 */
867         thread0.td_pcb->pcb_l1addr = \
868             (rvbp->kern_l1pt - KERNBASE + rvbp->kern_phys);
869
870         msgbufinit(msgbufp, msgbufsize);
871         mutex_init();
872         init_param2(physmem);
873         kdb_init();
874
875         early_boot = 0;
876 }
877
878 #undef bzero
879 void
880 bzero(void *buf, size_t len)
881 {
882         uint8_t *p;
883
884         p = buf;
885         while(len-- > 0)
886                 *p++ = 0;
887 }