]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/machdep.c
Merge ^/vendor/llvm-openmp/dist up to its last change, and resolve conflicts.
[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 static void get_fpcontext(struct thread *td, mcontext_t *mcp);
103 static void set_fpcontext(struct thread *td, mcontext_t *mcp);
104
105 struct pcpu __pcpu[MAXCPU];
106
107 static struct trapframe proc0_tf;
108
109 int early_boot = 1;
110 int cold = 1;
111 long realmem = 0;
112 long Maxmem = 0;
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          * Make sure the processor mode has not been tampered with and
372          * interrupts have not been disabled.
373          * Supervisor interrupts in user mode are always enabled.
374          */
375         if ((mcp->mc_gpregs.gp_sstatus & SSTATUS_SPP) != 0)
376                 return (EINVAL);
377
378         memcpy(tf->tf_t, mcp->mc_gpregs.gp_t, sizeof(tf->tf_t));
379         memcpy(tf->tf_s, mcp->mc_gpregs.gp_s, sizeof(tf->tf_s));
380         memcpy(tf->tf_a, mcp->mc_gpregs.gp_a, sizeof(tf->tf_a));
381
382         tf->tf_ra = mcp->mc_gpregs.gp_ra;
383         tf->tf_sp = mcp->mc_gpregs.gp_sp;
384         tf->tf_gp = mcp->mc_gpregs.gp_gp;
385         tf->tf_sepc = mcp->mc_gpregs.gp_sepc;
386         tf->tf_sstatus = mcp->mc_gpregs.gp_sstatus;
387         set_fpcontext(td, mcp);
388
389         return (0);
390 }
391
392 static void
393 get_fpcontext(struct thread *td, mcontext_t *mcp)
394 {
395 #ifdef FPE
396         struct pcb *curpcb;
397
398         critical_enter();
399
400         curpcb = curthread->td_pcb;
401
402         KASSERT(td->td_pcb == curpcb, ("Invalid fpe pcb"));
403
404         if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
405                 /*
406                  * If we have just been running FPE instructions we will
407                  * need to save the state to memcpy it below.
408                  */
409                 fpe_state_save(td);
410
411                 KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
412                     ("Non-userspace FPE flags set in get_fpcontext"));
413                 memcpy(mcp->mc_fpregs.fp_x, curpcb->pcb_x,
414                     sizeof(mcp->mc_fpregs));
415                 mcp->mc_fpregs.fp_fcsr = curpcb->pcb_fcsr;
416                 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
417                 mcp->mc_flags |= _MC_FP_VALID;
418         }
419
420         critical_exit();
421 #endif
422 }
423
424 static void
425 set_fpcontext(struct thread *td, mcontext_t *mcp)
426 {
427 #ifdef FPE
428         struct pcb *curpcb;
429
430         critical_enter();
431
432         if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
433                 curpcb = curthread->td_pcb;
434                 /* FPE usage is enabled, override registers. */
435                 memcpy(curpcb->pcb_x, mcp->mc_fpregs.fp_x,
436                     sizeof(mcp->mc_fpregs));
437                 curpcb->pcb_fcsr = mcp->mc_fpregs.fp_fcsr;
438                 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK;
439         }
440
441         critical_exit();
442 #endif
443 }
444
445 void
446 cpu_idle(int busy)
447 {
448
449         spinlock_enter();
450         if (!busy)
451                 cpu_idleclock();
452         if (!sched_runnable())
453                 __asm __volatile(
454                     "fence \n"
455                     "wfi   \n");
456         if (!busy)
457                 cpu_activeclock();
458         spinlock_exit();
459 }
460
461 void
462 cpu_halt(void)
463 {
464
465         intr_disable();
466         for (;;)
467                 __asm __volatile("wfi");
468 }
469
470 /*
471  * Flush the D-cache for non-DMA I/O so that the I-cache can
472  * be made coherent later.
473  */
474 void
475 cpu_flush_dcache(void *ptr, size_t len)
476 {
477
478         /* TBD */
479 }
480
481 /* Get current clock frequency for the given CPU ID. */
482 int
483 cpu_est_clockrate(int cpu_id, uint64_t *rate)
484 {
485
486         panic("cpu_est_clockrate");
487 }
488
489 void
490 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
491 {
492 }
493
494 void
495 spinlock_enter(void)
496 {
497         struct thread *td;
498         register_t reg;
499
500         td = curthread;
501         if (td->td_md.md_spinlock_count == 0) {
502                 reg = intr_disable();
503                 td->td_md.md_spinlock_count = 1;
504                 td->td_md.md_saved_sstatus_ie = reg;
505                 critical_enter();
506         } else
507                 td->td_md.md_spinlock_count++;
508 }
509
510 void
511 spinlock_exit(void)
512 {
513         struct thread *td;
514         register_t sstatus_ie;
515
516         td = curthread;
517         sstatus_ie = td->td_md.md_saved_sstatus_ie;
518         td->td_md.md_spinlock_count--;
519         if (td->td_md.md_spinlock_count == 0) {
520                 critical_exit();
521                 intr_restore(sstatus_ie);
522         }
523 }
524
525 #ifndef _SYS_SYSPROTO_H_
526 struct sigreturn_args {
527         ucontext_t *ucp;
528 };
529 #endif
530
531 int
532 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
533 {
534         ucontext_t uc;
535         int error;
536
537         if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
538                 return (EFAULT);
539
540         error = set_mcontext(td, &uc.uc_mcontext);
541         if (error != 0)
542                 return (error);
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         frame.sf_si = ksi->ksi_info;
615         frame.sf_uc.uc_sigmask = *mask;
616         frame.sf_uc.uc_stack = td->td_sigstk;
617         frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
618             (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
619         mtx_unlock(&psp->ps_mtx);
620         PROC_UNLOCK(td->td_proc);
621
622         /* Copy the sigframe out to the user's stack. */
623         if (copyout(&frame, fp, sizeof(*fp)) != 0) {
624                 /* Process has trashed its stack. Kill it. */
625                 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
626                 PROC_LOCK(p);
627                 sigexit(td, SIGILL);
628         }
629
630         tf->tf_a[0] = sig;
631         tf->tf_a[1] = (register_t)&fp->sf_si;
632         tf->tf_a[2] = (register_t)&fp->sf_uc;
633
634         tf->tf_sepc = (register_t)catcher;
635         tf->tf_sp = (register_t)fp;
636
637         sysent = p->p_sysent;
638         if (sysent->sv_sigcode_base != 0)
639                 tf->tf_ra = (register_t)sysent->sv_sigcode_base;
640         else
641                 tf->tf_ra = (register_t)(sysent->sv_psstrings -
642                     *(sysent->sv_szsigcode));
643
644         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_sepc,
645             tf->tf_sp);
646
647         PROC_LOCK(p);
648         mtx_lock(&psp->ps_mtx);
649 }
650
651 static void
652 init_proc0(vm_offset_t kstack)
653 {
654         struct pcpu *pcpup;
655
656         pcpup = &__pcpu[0];
657
658         proc_linkup0(&proc0, &thread0);
659         thread0.td_kstack = kstack;
660         thread0.td_kstack_pages = KSTACK_PAGES;
661         thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
662             thread0.td_kstack_pages * PAGE_SIZE) - 1;
663         thread0.td_pcb->pcb_fpflags = 0;
664         thread0.td_frame = &proc0_tf;
665         pcpup->pc_curpcb = thread0.td_pcb;
666 }
667
668 static int
669 add_physmap_entry(uint64_t base, uint64_t length, vm_paddr_t *physmap,
670     u_int *physmap_idxp)
671 {
672         u_int i, insert_idx, _physmap_idx;
673
674         _physmap_idx = *physmap_idxp;
675
676         if (length == 0)
677                 return (1);
678
679         /*
680          * Find insertion point while checking for overlap.  Start off by
681          * assuming the new entry will be added to the end.
682          */
683         insert_idx = _physmap_idx;
684         for (i = 0; i <= _physmap_idx; i += 2) {
685                 if (base < physmap[i + 1]) {
686                         if (base + length <= physmap[i]) {
687                                 insert_idx = i;
688                                 break;
689                         }
690                         if (boothowto & RB_VERBOSE)
691                                 printf(
692                     "Overlapping memory regions, ignoring second region\n");
693                         return (1);
694                 }
695         }
696
697         /* See if we can prepend to the next entry. */
698         if (insert_idx <= _physmap_idx &&
699             base + length == physmap[insert_idx]) {
700                 physmap[insert_idx] = base;
701                 return (1);
702         }
703
704         /* See if we can append to the previous entry. */
705         if (insert_idx > 0 && base == physmap[insert_idx - 1]) {
706                 physmap[insert_idx - 1] += length;
707                 return (1);
708         }
709
710         _physmap_idx += 2;
711         *physmap_idxp = _physmap_idx;
712         if (_physmap_idx == PHYS_AVAIL_ENTRIES) {
713                 printf(
714                 "Too many segments in the physical address map, giving up\n");
715                 return (0);
716         }
717
718         /*
719          * Move the last 'N' entries down to make room for the new
720          * entry if needed.
721          */
722         for (i = _physmap_idx; i > insert_idx; i -= 2) {
723                 physmap[i] = physmap[i - 2];
724                 physmap[i + 1] = physmap[i - 1];
725         }
726
727         /* Insert the new entry. */
728         physmap[insert_idx] = base;
729         physmap[insert_idx + 1] = base + length;
730
731         printf("physmap[%d] = 0x%016lx\n", insert_idx, base);
732         printf("physmap[%d] = 0x%016lx\n", insert_idx + 1, base + length);
733         return (1);
734 }
735
736 #ifdef FDT
737 static void
738 try_load_dtb(caddr_t kmdp, vm_offset_t dtbp)
739 {
740
741 #if defined(FDT_DTB_STATIC)
742         dtbp = (vm_offset_t)&fdt_static_dtb;
743 #endif
744
745         if (dtbp == (vm_offset_t)NULL) {
746                 printf("ERROR loading DTB\n");
747                 return;
748         }
749
750         if (OF_install(OFW_FDT, 0) == FALSE)
751                 panic("Cannot install FDT");
752
753         if (OF_init((void *)dtbp) != 0)
754                 panic("OF_init failed with the found device tree");
755 }
756 #endif
757
758 static void
759 cache_setup(void)
760 {
761
762         /* TODO */
763
764         dcache_line_size = 0;
765         icache_line_size = 0;
766         idcache_line_size = 0;
767 }
768
769 /*
770  * Fake up a boot descriptor table.
771  * RISCVTODO: This needs to be done via loader (when it's available).
772  */
773 vm_offset_t
774 fake_preload_metadata(struct riscv_bootparams *rvbp __unused)
775 {
776         static uint32_t fake_preload[35];
777 #ifdef DDB
778         vm_offset_t zstart = 0, zend = 0;
779 #endif
780         vm_offset_t lastaddr;
781         int i;
782
783         i = 0;
784
785         fake_preload[i++] = MODINFO_NAME;
786         fake_preload[i++] = strlen("kernel") + 1;
787         strcpy((char*)&fake_preload[i++], "kernel");
788         i += 1;
789         fake_preload[i++] = MODINFO_TYPE;
790         fake_preload[i++] = strlen("elf64 kernel") + 1;
791         strcpy((char*)&fake_preload[i++], "elf64 kernel");
792         i += 3;
793         fake_preload[i++] = MODINFO_ADDR;
794         fake_preload[i++] = sizeof(vm_offset_t);
795         *(vm_offset_t *)&fake_preload[i++] =
796             (vm_offset_t)(KERNBASE + KERNENTRY);
797         i += 1;
798         fake_preload[i++] = MODINFO_SIZE;
799         fake_preload[i++] = sizeof(vm_offset_t);
800         fake_preload[i++] = (vm_offset_t)&end -
801             (vm_offset_t)(KERNBASE + KERNENTRY);
802         i += 1;
803 #ifdef DDB
804 #if 0
805         /* RISCVTODO */
806         if (*(uint32_t *)KERNVIRTADDR == MAGIC_TRAMP_NUMBER) {
807                 fake_preload[i++] = MODINFO_METADATA|MODINFOMD_SSYM;
808                 fake_preload[i++] = sizeof(vm_offset_t);
809                 fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 4);
810                 fake_preload[i++] = MODINFO_METADATA|MODINFOMD_ESYM;
811                 fake_preload[i++] = sizeof(vm_offset_t);
812                 fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 8);
813                 lastaddr = *(uint32_t *)(KERNVIRTADDR + 8);
814                 zend = lastaddr;
815                 zstart = *(uint32_t *)(KERNVIRTADDR + 4);
816                 db_fetch_ksymtab(zstart, zend);
817         } else
818 #endif
819 #endif
820                 lastaddr = (vm_offset_t)&end;
821         fake_preload[i++] = 0;
822         fake_preload[i] = 0;
823         preload_metadata = (void *)fake_preload;
824
825         return (lastaddr);
826 }
827
828 void
829 initriscv(struct riscv_bootparams *rvbp)
830 {
831         struct mem_region mem_regions[FDT_MEM_REGIONS];
832         struct pcpu *pcpup;
833         vm_offset_t rstart, rend;
834         vm_offset_t s, e;
835         int mem_regions_sz;
836         vm_offset_t lastaddr;
837         vm_size_t kernlen;
838         caddr_t kmdp;
839         int i;
840
841         TSRAW(&thread0, TS_ENTER, __func__, NULL);
842
843         /* Set the pcpu data, this is needed by pmap_bootstrap */
844         pcpup = &__pcpu[0];
845         pcpu_init(pcpup, 0, sizeof(struct pcpu));
846         pcpup->pc_hart = boot_hart;
847
848         /* Set the pcpu pointer */
849         __asm __volatile("mv tp, %0" :: "r"(pcpup));
850
851         PCPU_SET(curthread, &thread0);
852
853         /* Initialize SBI interface. */
854         sbi_init();
855
856         /* Set the module data location */
857         lastaddr = fake_preload_metadata(rvbp);
858
859         /* Find the kernel address */
860         kmdp = preload_search_by_type("elf kernel");
861         if (kmdp == NULL)
862                 kmdp = preload_search_by_type("elf64 kernel");
863
864         boothowto = RB_VERBOSE | RB_SINGLE;
865         boothowto = RB_VERBOSE;
866
867         kern_envp = NULL;
868
869 #ifdef FDT
870         try_load_dtb(kmdp, rvbp->dtbp_virt);
871 #endif
872
873         /* Load the physical memory ranges */
874         physmap_idx = 0;
875
876 #ifdef FDT
877         /* Grab physical memory regions information from device tree. */
878         if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, NULL) != 0)
879                 panic("Cannot get physical memory regions");
880
881         s = rvbp->dtbp_phys;
882         e = s + DTB_SIZE_MAX;
883
884         for (i = 0; i < mem_regions_sz; i++) {
885                 rstart = mem_regions[i].mr_start;
886                 rend = (mem_regions[i].mr_start + mem_regions[i].mr_size);
887
888                 if ((rstart < s) && (rend > e)) {
889                         /* Exclude DTB region. */
890                         add_physmap_entry(rstart, (s - rstart), physmap, &physmap_idx);
891                         add_physmap_entry(e, (rend - e), physmap, &physmap_idx);
892                 } else {
893                         add_physmap_entry(mem_regions[i].mr_start,
894                             mem_regions[i].mr_size, physmap, &physmap_idx);
895                 }
896         }
897 #endif
898
899         /* Do basic tuning, hz etc */
900         init_param1();
901
902         cache_setup();
903
904         /* Bootstrap enough of pmap to enter the kernel proper */
905         kernlen = (lastaddr - KERNBASE);
906         pmap_bootstrap(rvbp->kern_l1pt, mem_regions[0].mr_start, kernlen);
907
908         /* Establish static device mappings */
909         devmap_bootstrap(0, NULL);
910
911         cninit();
912
913         init_proc0(rvbp->kern_stack);
914
915         msgbufinit(msgbufp, msgbufsize);
916         mutex_init();
917         init_param2(physmem);
918         kdb_init();
919
920         early_boot = 0;
921
922         TSEXIT();
923 }
924
925 #undef bzero
926 void
927 bzero(void *buf, size_t len)
928 {
929         uint8_t *p;
930
931         p = buf;
932         while(len-- > 0)
933                 *p++ = 0;
934 }