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