| 1 | #
|
|---|
| 2 | # Copyright (c) 2016 Martin Decky
|
|---|
| 3 | # All rights reserved.
|
|---|
| 4 | #
|
|---|
| 5 | # Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | # modification, are permitted provided that the following conditions
|
|---|
| 7 | # are met:
|
|---|
| 8 | #
|
|---|
| 9 | # - Redistributions of source code must retain the above copyright
|
|---|
| 10 | # notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | # - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | # notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | # documentation and/or other materials provided with the distribution.
|
|---|
| 14 | # - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | # derived from this software without specific prior written permission.
|
|---|
| 16 | #
|
|---|
| 17 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | #
|
|---|
| 28 |
|
|---|
| 29 | #include <abi/asmtool.h>
|
|---|
| 30 | #include <arch/arch.h>
|
|---|
| 31 | #include <arch/mm.h>
|
|---|
| 32 |
|
|---|
| 33 | #define MCOUNTEREN_CY_MASK 0x00000001
|
|---|
| 34 | #define MCOUNTEREN_TM_MASK 0x00000002
|
|---|
| 35 | #define MCOUNTEREN_IR_MASK 0x00000004
|
|---|
| 36 |
|
|---|
| 37 | #define MSTATUS_MPP_MASK 0x00001800
|
|---|
| 38 | #define MSTATUS_MPP_USER 0x00000000
|
|---|
| 39 | #define MSTATUS_MPP_SUPERVISOR 0x00000800
|
|---|
| 40 | #define MSTATUS_MPP_MACHINE 0x00001800
|
|---|
| 41 |
|
|---|
| 42 | #define MSTATUS_SUM_MASK 0x00040000
|
|---|
| 43 |
|
|---|
| 44 | #define SATP_PFN_MASK 0x00000fffffffffff
|
|---|
| 45 |
|
|---|
| 46 | #define SATP_MODE_MASK 0xf000000000000000
|
|---|
| 47 | #define SATP_MODE_BARE 0x0000000000000000
|
|---|
| 48 | #define SATP_MODE_SV39 0x8000000000000000
|
|---|
| 49 | #define SATP_MODE_SV48 0x9000000000000000
|
|---|
| 50 |
|
|---|
| 51 | .section BOOTSTRAP
|
|---|
| 52 |
|
|---|
| 53 | .org DEFAULT_MTVEC + TRAP_VECTOR_RESET
|
|---|
| 54 | SYMBOL(start)
|
|---|
| 55 | /* Set up machine state */
|
|---|
| 56 | li x1, 0
|
|---|
| 57 | li x2, 0
|
|---|
| 58 | li x3, 0
|
|---|
| 59 | li x4, 0
|
|---|
| 60 | li x5, 0
|
|---|
| 61 | li x6, 0
|
|---|
| 62 | li x7, 0
|
|---|
| 63 | li x8, 0
|
|---|
| 64 | li x9, 0
|
|---|
| 65 | li x10, 0
|
|---|
| 66 | li x11, 0
|
|---|
| 67 | li x12, 0
|
|---|
| 68 | li x13, 0
|
|---|
| 69 | li x14, 0
|
|---|
| 70 | li x15, 0
|
|---|
| 71 | li x16, 0
|
|---|
| 72 | li x17, 0
|
|---|
| 73 | li x18, 0
|
|---|
| 74 | li x19, 0
|
|---|
| 75 | li x20, 0
|
|---|
| 76 | li x21, 0
|
|---|
| 77 | li x22, 0
|
|---|
| 78 | li x23, 0
|
|---|
| 79 | li x24, 0
|
|---|
| 80 | li x25, 0
|
|---|
| 81 | li x26, 0
|
|---|
| 82 | li x27, 0
|
|---|
| 83 | li x28, 0
|
|---|
| 84 | li x29, 0
|
|---|
| 85 | li x30, 0
|
|---|
| 86 | li x31, 0
|
|---|
| 87 |
|
|---|
| 88 | /* Set up stack, create stack frame */
|
|---|
| 89 | la sp, boot_stack + BOOT_STACK_SIZE
|
|---|
| 90 | addi sp, sp, -16
|
|---|
| 91 |
|
|---|
| 92 | j bootstrap
|
|---|
| 93 |
|
|---|
| 94 | .text
|
|---|
| 95 |
|
|---|
| 96 | FUNCTION_BEGIN(jump_to_kernel)
|
|---|
| 97 | /* Enable performance counters access in supervisor mode */
|
|---|
| 98 | csrsi mcounteren, MCOUNTEREN_CY_MASK | MCOUNTEREN_TM_MASK | MCOUNTEREN_IR_MASK
|
|---|
| 99 |
|
|---|
| 100 | /* Setup SV48 paging for supervisor mode */
|
|---|
| 101 | la t0, ptl_0
|
|---|
| 102 | srli t0, t0, 12
|
|---|
| 103 |
|
|---|
| 104 | li t1, SATP_PFN_MASK
|
|---|
| 105 | and t0, t0, t1
|
|---|
| 106 |
|
|---|
| 107 | li t1, SATP_MODE_SV48
|
|---|
| 108 | or t0, t0, t1
|
|---|
| 109 |
|
|---|
| 110 | csrw sptbr, t0
|
|---|
| 111 |
|
|---|
| 112 | /* Jump to supervisor mode */
|
|---|
| 113 | csrr t0, mstatus
|
|---|
| 114 |
|
|---|
| 115 | li t1, ~MSTATUS_MPP_MASK
|
|---|
| 116 | and t0, t0, t1
|
|---|
| 117 |
|
|---|
| 118 | /*
|
|---|
| 119 | * TODO: Enable running with Supervisor User Mode
|
|---|
| 120 | * access disabled.
|
|---|
| 121 | */
|
|---|
| 122 | li t1, MSTATUS_MPP_SUPERVISOR | MSTATUS_SUM_MASK
|
|---|
| 123 | or t0, t0, t1
|
|---|
| 124 |
|
|---|
| 125 | csrw mstatus, t0
|
|---|
| 126 |
|
|---|
| 127 | li ra, PA2KA(BOOT_OFFSET)
|
|---|
| 128 | csrw mepc, ra
|
|---|
| 129 |
|
|---|
| 130 | mret
|
|---|
| 131 | FUNCTION_END(jump_to_kernel)
|
|---|
| 132 |
|
|---|
| 133 | FUNCTION_BEGIN(halt)
|
|---|
| 134 | j halt
|
|---|
| 135 | FUNCTION_END(halt)
|
|---|
| 136 |
|
|---|
| 137 | .data
|
|---|
| 138 |
|
|---|
| 139 | .align PAGE_WIDTH
|
|---|
| 140 | SYMBOL(boot_stack)
|
|---|
| 141 | .space BOOT_STACK_SIZE
|
|---|
| 142 |
|
|---|
| 143 | .section .pt, "aw", @progbits
|
|---|
| 144 |
|
|---|
| 145 | .align PAGE_WIDTH
|
|---|
| 146 | SYMBOL(ptl_0)
|
|---|
| 147 | .fill 256, 8, 0
|
|---|
| 148 | /* Identity mapping for [0; 512G) */
|
|---|
| 149 | .quad 0 + (PTL_DIRTY | PTL_ACCESSED | PTL_EXECUTABLE | PTL_WRITABLE | PTL_READABLE | PTL_VALID)
|
|---|
| 150 | .fill 255, 8, 0
|
|---|