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