2 * Copyright (c) 2015-2017 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
56 /* Get the physical address kernel loaded to */
61 sub s9, t2, t1 /* s9 = physmem base */
62 mv s10, a0 /* s10 = hart id */
63 mv s11, a1 /* s11 = dtbp */
68 /* Direct secondary cores to mpentry */
75 /* Add L1 entry for kernel */
77 la s2, pagetable_l2 /* Link to next level PN */
78 srli s2, s2, PAGE_SHIFT
81 srli a5, a5, L1_SHIFT /* >> L1_SHIFT */
82 andi a5, a5, 0x1ff /* & 0x1ff */
84 slli t5, s2, PTE_PPN0_S /* (s2 << PTE_PPN0_S) */
87 /* Store L1 PTE entry to position */
93 /* Level 2 superpages (512 x 2MiB) */
95 srli t4, s9, 21 /* Div physmem base by 2 MiB */
96 li t2, 512 /* Build 512 entries */
100 li t0, (PTE_V | PTE_RWX | PTE_D)
101 slli t2, t4, PTE_PPN1_S /* << PTE_PPN1_S */
103 sd t5, (s1) /* Store PTE entry to position */
104 addi s1, s1, PTE_SIZE
109 /* Create an L1 page for early devmap */
111 la s2, pagetable_l2_devmap /* Link to next level PN */
112 srli s2, s2, PAGE_SHIFT
114 li a5, (VM_MAX_KERNEL_ADDRESS - L2_SIZE)
115 srli a5, a5, L1_SHIFT /* >> L1_SHIFT */
116 andi a5, a5, 0x1ff /* & 0x1ff */
118 slli t5, s2, PTE_PPN0_S /* (s2 << PTE_PPN0_S) */
121 /* Store single level1 PTE entry to position */
127 /* Create an L2 page superpage for DTB */
128 la s1, pagetable_l2_devmap
130 srli s2, s2, PAGE_SHIFT
132 li t0, (PTE_V | PTE_RWX | PTE_D)
133 slli t2, s2, PTE_PPN0_S /* << PTE_PPN0_S */
136 /* Store PTE entry to position */
143 /* Page tables END */
145 /* Setup supervisor trap vector */
152 /* Set page tables base register */
154 srli s2, s2, PAGE_SHIFT
155 li t0, SATP_MODE_SV39
161 /* Setup supervisor trap vector */
162 la t0, cpu_exception_handler
165 /* Ensure sscratch is zero */
169 /* Initialize stack pointer */
172 addi sp, sp, -PCB_SIZE
175 la a0, _C_LABEL(__bss_start)
176 la s1, _C_LABEL(_end)
182 /* Fill riscv_bootparams */
186 sd t0, 0(sp) /* kern_l1pt */
187 sd s9, 8(sp) /* kern_phys */
190 sd t0, 16(sp) /* kern_stack */
192 li t0, (VM_MAX_KERNEL_ADDRESS - 2 * L2_SIZE)
193 sd t0, 24(sp) /* dtbp_virt */
194 sd s11, 32(sp) /* dtbp_phys */
197 call _C_LABEL(initriscv) /* Off we go */
198 call _C_LABEL(mi_startup)
202 .space (PAGE_SIZE * KSTACK_PAGES)
213 /* sigreturn failed, exit */
219 /* This may be copied to the stack, keep it 16-byte aligned */
227 .quad esigcode - sigcode
241 /* Not in use, but required for linking. */
243 .globl __global_pointer$
249 .quad pagetable_l2 /* XXX: Keep page tables VA */
259 * mpentry(unsigned long)
261 * Called by a core when it is being brought online.
264 /* Setup stack pointer */
265 la t0, secondary_stacks
266 li t1, (PAGE_SIZE * KSTACK_PAGES)
273 /* Setup supervisor trap vector */
280 /* Set page tables base register */
282 srli s2, s2, PAGE_SHIFT
283 li t0, SATP_MODE_SV39
288 /* Setup supervisor trap vector */
289 la t0, cpu_exception_handler
292 /* Ensure sscratch is zero */
296 * Calculate the offset to __riscv_boot_ap
297 * for current core, cpuid in a0.
302 la t0, __riscv_boot_ap
306 /* Wait the kernel to be ready */