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