Thaminda Edirisooriya | b094583 | 2015-08-26 15:28:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Early initialization code for riscv virtual memory |
| 3 | * |
| 4 | * Copyright 2015 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; version 2 of |
| 9 | * the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
| 14 | * GNU General Public License for more details. |
Thaminda Edirisooriya | b094583 | 2015-08-26 15:28:04 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Thaminda Edirisooriya | b094583 | 2015-08-26 15:28:04 -0700 | [diff] [blame] | 17 | #include <arch/encoding.h> |
Jonathan Neuschäfer | cc5be8b | 2016-07-26 01:54:34 +0200 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | #include <vm.h> |
Thaminda Edirisooriya | b094583 | 2015-08-26 15:28:04 -0700 | [diff] [blame] | 20 | |
Ronald G. Minnich | 3d302b0 | 2016-11-12 07:31:16 -0800 | [diff] [blame] | 21 | /* Delegate controls which traps are delegated to the payload. If you |
| 22 | * wish to temporarily disable some or all delegation you can, in a |
| 23 | * debugger, set it to a different value (e.g. 0 to have all traps go |
| 24 | * to M-mode). In practice, this variable has been a lifesaver. It is |
| 25 | * still not quite determined which delegation might by unallowed by |
| 26 | * the spec so for now we enumerate and set them all. */ |
| 27 | static int delegate = 0 |
| 28 | | (1 << CAUSE_MISALIGNED_FETCH) |
Jonathan Neuschäfer | a5c49b8 | 2018-02-16 13:36:46 +0100 | [diff] [blame] | 29 | | (1 << CAUSE_FETCH_ACCESS) |
Ronald G. Minnich | 3d302b0 | 2016-11-12 07:31:16 -0800 | [diff] [blame] | 30 | | (1 << CAUSE_ILLEGAL_INSTRUCTION) |
| 31 | | (1 << CAUSE_BREAKPOINT) |
Jonathan Neuschäfer | a5c49b8 | 2018-02-16 13:36:46 +0100 | [diff] [blame] | 32 | | (1 << CAUSE_LOAD_ACCESS) |
| 33 | | (1 << CAUSE_STORE_ACCESS) |
Ronald G. Minnich | 3d302b0 | 2016-11-12 07:31:16 -0800 | [diff] [blame] | 34 | | (1 << CAUSE_USER_ECALL) |
Jonathan Neuschäfer | 6186414 | 2018-02-16 13:36:46 +0100 | [diff] [blame] | 35 | | (1 << CAUSE_FETCH_PAGE_FAULT) |
| 36 | | (1 << CAUSE_LOAD_PAGE_FAULT) |
| 37 | | (1 << CAUSE_STORE_PAGE_FAULT) |
Ronald G. Minnich | 3d302b0 | 2016-11-12 07:31:16 -0800 | [diff] [blame] | 38 | ; |
Ronald G. Minnich | 4e793ec | 2016-11-04 11:27:25 -0700 | [diff] [blame] | 39 | |
Thaminda Edirisooriya | b094583 | 2015-08-26 15:28:04 -0700 | [diff] [blame] | 40 | void mstatus_init(void) |
| 41 | { |
Thaminda Edirisooriya | b094583 | 2015-08-26 15:28:04 -0700 | [diff] [blame] | 42 | uintptr_t ms = 0; |
Ronald G. Minnich | d9307c2 | 2016-12-12 15:09:42 -0800 | [diff] [blame] | 43 | |
Thaminda Edirisooriya | b094583 | 2015-08-26 15:28:04 -0700 | [diff] [blame] | 44 | ms = INSERT_FIELD(ms, MSTATUS_FS, 3); |
| 45 | ms = INSERT_FIELD(ms, MSTATUS_XS, 3); |
| 46 | write_csr(mstatus, ms); |
Thaminda Edirisooriya | b094583 | 2015-08-26 15:28:04 -0700 | [diff] [blame] | 47 | |
Ronald G. Minnich | d9307c2 | 2016-12-12 15:09:42 -0800 | [diff] [blame] | 48 | // clear any pending timer interrupts. |
| 49 | clear_csr(mip, MIP_STIP | MIP_SSIP); |
| 50 | |
| 51 | // enable machine and supervisor timer and |
| 52 | // all other supervisor interrupts. |
| 53 | set_csr(mie, MIP_MTIP | MIP_STIP | MIP_SSIP); |
| 54 | |
| 55 | // Delegate supervisor timer and other interrupts |
| 56 | // to supervisor mode. |
| 57 | set_csr(mideleg, MIP_STIP | MIP_SSIP); |
Jonathan Neuschäfer | d9ff75f | 2016-08-22 19:37:15 +0200 | [diff] [blame] | 58 | |
Ronald G. Minnich | 3d302b0 | 2016-11-12 07:31:16 -0800 | [diff] [blame] | 59 | set_csr(medeleg, delegate); |
Ronald G. Minnich | 5965cba | 2016-10-19 08:07:13 -0700 | [diff] [blame] | 60 | |
Ronald G. Minnich | f171e66 | 2016-12-19 09:06:00 -0800 | [diff] [blame] | 61 | // Enable all user/supervisor-mode counters using |
wxjstz | d277960 | 2017-06-06 16:50:46 +0800 | [diff] [blame] | 62 | // v1.10 register addresses. |
Ronald G. Minnich | f171e66 | 2016-12-19 09:06:00 -0800 | [diff] [blame] | 63 | // They moved from the earlier spec. |
| 64 | // Until we trust our toolchain use the hardcoded constants. |
| 65 | // These were in flux and people who get the older toolchain |
| 66 | // will have difficult-to-debug failures. |
wxjstz | d277960 | 2017-06-06 16:50:46 +0800 | [diff] [blame] | 67 | write_csr(/*mcounteren*/0x306, 7); |
Thaminda Edirisooriya | b094583 | 2015-08-26 15:28:04 -0700 | [diff] [blame] | 68 | } |