2 * Copyright (c) 2015-2018 Ruslan Bukin <br@bsdpad.com>
5 * Portions of this software were developed by SRI International and the
6 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
7 * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Portions of this software were developed by the University of Cambridge
10 * Computer Laboratory as part of the CTSRD Project, with support from the
11 * UK Higher Education Innovation Fund (HEIF).
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/syscall.h>
40 #include <machine/asm.h>
41 #include <machine/param.h>
42 #include <machine/trap.h>
43 #include <machine/riscvreg.h>
44 #include <machine/pte.h>
47 .set kernbase, KERNBASE
51 * Alternate entry point. Used when booting via SBI firmware. It must be placed
52 * at the beginning of the .text section. Arguments are as follows:
56 * Multiple CPUs might enter from this point, so we perform a hart lottery and
57 * send the losers to mpentry.
61 /* Set the global pointer */
64 lla gp, __global_pointer$
67 /* Pick a hart to run the boot process. */
70 amoadd.w t0, t1, 0(t0)
73 * We must jump to mpentry in the non-BSP case because the offset is
74 * too large to fit in a 12-bit branch immediate.
79 /* Store the boot hart */
83 /* Load zero as modulep */
88 * Main entry point. This routine is marked as the ELF entry, and is where
89 * loader(8) will enter the kernel. Arguments are as follows:
93 * It is expected that only a single CPU will enter here.
97 /* Set the global pointer */
100 lla gp, __global_pointer$
104 * Zero a1 to indicate that we have no DTB pointer. It is already
105 * included in the loader(8) metadata.
111 * a0 - modulep or zero
115 /* Get the kernel's load address */
118 /* Add L1 entry for kernel */
120 lla s2, pagetable_l2 /* Link to next level PN */
121 srli s2, s2, PAGE_SHIFT
124 srli a5, a5, L1_SHIFT /* >> L1_SHIFT */
125 andi a5, a5, 0x1ff /* & 0x1ff */
127 slli t5, s2, PTE_PPN0_S /* (s2 << PTE_PPN0_S) */
130 /* Store L1 PTE entry to position */
136 /* Level 2 superpages (512 x 2MiB) */
138 srli t4, s9, 21 /* Div physmem base by 2 MiB */
139 li t2, 512 /* Build 512 entries */
142 li t0, (PTE_KERN | PTE_X)
144 slli t2, t4, PTE_PPN1_S /* << PTE_PPN1_S */
146 sd t5, (s1) /* Store PTE entry to position */
147 addi s1, s1, PTE_SIZE
152 /* Create an L1 page for early devmap */
154 lla s2, pagetable_l2_devmap /* Link to next level PN */
155 srli s2, s2, PAGE_SHIFT
157 li a5, (VM_MAX_KERNEL_ADDRESS - L2_SIZE)
158 srli a5, a5, L1_SHIFT /* >> L1_SHIFT */
159 andi a5, a5, 0x1ff /* & 0x1ff */
161 slli t5, s2, PTE_PPN0_S /* (s2 << PTE_PPN0_S) */
164 /* Store single level1 PTE entry to position */
170 /* Check if we have a DTB that needs to be mapped */
173 /* Create an L2 page superpage for DTB */
174 lla s1, pagetable_l2_devmap
176 srli s2, s2, PAGE_SHIFT
177 /* Mask off any bits that aren't aligned */
178 andi s2, s2, ~((1 << (PTE_PPN1_S - PTE_PPN0_S)) - 1)
181 slli t2, s2, PTE_PPN0_S /* << PTE_PPN0_S */
184 /* Store PTE entry to position */
191 /* Page tables END */
193 /* Setup supervisor trap vector */
201 /* Set page tables base register */
203 srli s2, s2, PAGE_SHIFT
204 li t0, SATP_MODE_SV39
211 /* Set the global pointer again, this time with the virtual address. */
214 lla gp, __global_pointer$
217 /* Setup supervisor trap vector */
218 la t0, cpu_exception_handler
221 /* Ensure sscratch is zero */
225 /* Initialize stack pointer */
228 /* Clear frame pointer */
231 /* Allocate space for thread0 PCB and riscv_bootparams */
232 addi sp, sp, -(PCB_SIZE + RISCV_BOOTPARAMS_SIZE) & ~STACKALIGNBYTES
235 la t0, _C_LABEL(__bss_start)
236 la t1, _C_LABEL(_end)
242 /* Fill riscv_bootparams */
244 sd t0, RISCV_BOOTPARAMS_KERN_L1PT(sp)
245 sd s9, RISCV_BOOTPARAMS_KERN_PHYS(sp)
248 sd t0, RISCV_BOOTPARAMS_KERN_STACK(sp)
250 li t0, (VM_EARLY_DTB_ADDRESS)
251 /* Add offset of DTB within superpage */
255 sd t0, RISCV_BOOTPARAMS_DTBP_VIRT(sp)
256 sd a1, RISCV_BOOTPARAMS_DTBP_PHYS(sp)
258 sd a0, RISCV_BOOTPARAMS_MODULEP(sp)
261 call _C_LABEL(initriscv) /* Off we go */
262 call _C_LABEL(mi_startup)
264 /* We should never reach here, but if so just hang. */
270 * Get the physical address the kernel is loaded to. Returned in s9.
273 lla t0, virt_map /* physical address of virt_map */
274 ld t1, 0(t0) /* virtual address of virt_map */
275 sub t1, t1, t0 /* calculate phys->virt delta */
277 sub s9, t2, t1 /* s9 = physmem base */
282 .space (PAGE_SIZE * KSTACK_PAGES)
293 /* sigreturn failed, exit */
299 /* This may be copied to the stack, keep it 16-byte aligned */
307 .quad esigcode - sigcode
325 .quad pagetable_l2 /* XXX: Keep page tables VA */
335 * mpentry(unsigned long)
337 * Called by a core when it is being brought online.
341 * Calculate the offset to __riscv_boot_ap
342 * for the current core, cpuid is in a0.
346 /* Get the pointer */
347 lla t0, __riscv_boot_ap
351 /* Wait the kernel to be ready */
355 /* Setup stack pointer */
359 /* Get the kernel's load address */
362 /* Setup supervisor trap vector */
369 /* Set page tables base register */
371 srli s2, s2, PAGE_SHIFT
372 li t0, SATP_MODE_SV39
379 /* Set the global pointer again, this time with the virtual address. */
382 lla gp, __global_pointer$
385 /* Setup supervisor trap vector */
386 la t0, cpu_exception_handler
389 /* Ensure sscratch is zero */