Zephyr Project API  3.2.0
A Scalable Open Source RTOS
arch.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 Jean-Paul Etienne <fractalclone@gmail.com>
3 * Contributors: 2018 Antmicro <www.antmicro.com>
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
15#ifndef ZEPHYR_INCLUDE_ARCH_RISCV_ARCH_H_
16#define ZEPHYR_INCLUDE_ARCH_RISCV_ARCH_H_
17
24#if defined(CONFIG_USERSPACE)
26#endif /* CONFIG_USERSPACE */
27#include <zephyr/irq.h>
28#include <zephyr/sw_isr_table.h>
29#include <soc.h>
30#include <zephyr/devicetree.h>
33
34/* stacks, for RISCV architecture stack should be 16byte-aligned */
35#define ARCH_STACK_PTR_ALIGN 16
36
37#ifdef CONFIG_PMP_STACK_GUARD
38/*
39 * The StackGuard is an area at the bottom of the kernel-mode stack made to
40 * fault when accessed. It is _not_ faulting when in exception mode as we rely
41 * on that area to save the exception stack frame and to process said fault.
42 * Therefore the guard area must be large enough to hold the esf, plus some
43 * configurable stack wiggle room to execute the fault handling code off of,
44 * as well as some guard size to cover possible sudden stack pointer
45 * displacement before the fault.
46 *
47 * The m-mode PMP set is not overly used so no need to force NAPOT.
48 */
49#define Z_RISCV_STACK_GUARD_SIZE \
50 ROUND_UP(sizeof(z_arch_esf_t) + CONFIG_PMP_STACK_GUARD_MIN_SIZE, \
51 ARCH_STACK_PTR_ALIGN)
52
53/* Kernel-only stacks have the following layout if a stack guard is enabled:
54 *
55 * +------------+ <- thread.stack_obj
56 * | Guard | } Z_RISCV_STACK_GUARD_SIZE
57 * +------------+ <- thread.stack_info.start
58 * | Kernel |
59 * | stack |
60 * | |
61 * +............|
62 * | TLS | } thread.stack_info.delta
63 * +------------+ <- thread.stack_info.start + thread.stack_info.size
64 */
65#define ARCH_KERNEL_STACK_RESERVED Z_RISCV_STACK_GUARD_SIZE
66
67#else /* !CONFIG_PMP_STACK_GUARD */
68#define Z_RISCV_STACK_GUARD_SIZE 0
69#endif
70
71#ifdef CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT
72/* The privilege elevation stack is located in another area of memory
73 * generated at build time by gen_kobject_list.py
74 *
75 * +------------+ <- thread.arch.priv_stack_start
76 * | Guard | } Z_RISCV_STACK_GUARD_SIZE
77 * +------------+
78 * | Priv Stack | } CONFIG_PRIVILEGED_STACK_SIZE
79 * +------------+ <- thread.arch.priv_stack_start +
80 * CONFIG_PRIVILEGED_STACK_SIZE +
81 * Z_RISCV_STACK_GUARD_SIZE
82 *
83 * The main stack will be initially (or potentially only) used by kernel
84 * mode so we need to make room for a possible stack guard area when enabled:
85 *
86 * +------------+ <- thread.stack_obj
87 * | Guard | } Z_RISCV_STACK_GUARD_SIZE
88 * +............| <- thread.stack_info.start
89 * | Thread |
90 * | stack |
91 * | |
92 * +............|
93 * | TLS | } thread.stack_info.delta
94 * +------------+ <- thread.stack_info.start + thread.stack_info.size
95 *
96 * When transitioning to user space, the guard area will be removed from
97 * the main stack. Any thread running in user mode will have full access
98 * to the region denoted by thread.stack_info. Make it PMP-NAPOT compatible.
99 *
100 * +------------+ <- thread.stack_obj = thread.stack_info.start
101 * | Thread |
102 * | stack |
103 * | |
104 * +............|
105 * | TLS | } thread.stack_info.delta
106 * +------------+ <- thread.stack_info.start + thread.stack_info.size
107 */
108#define ARCH_THREAD_STACK_RESERVED Z_RISCV_STACK_GUARD_SIZE
109#define ARCH_THREAD_STACK_SIZE_ADJUST(size) \
110 Z_POW2_CEIL(MAX(size, CONFIG_PRIVILEGED_STACK_SIZE))
111#define ARCH_THREAD_STACK_OBJ_ALIGN(size) \
112 ARCH_THREAD_STACK_SIZE_ADJUST(size)
113
114#else /* !CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT */
115
116/* The stack object will contain the PMP guard, the privilege stack, and then
117 * the usermode stack buffer in that order:
118 *
119 * +------------+ <- thread.stack_obj
120 * | Guard | } Z_RISCV_STACK_GUARD_SIZE
121 * +------------+
122 * | Priv Stack | } CONFIG_PRIVILEGED_STACK_SIZE
123 * +------------+ <- thread.stack_info.start
124 * | Thread |
125 * | stack |
126 * | |
127 * +............|
128 * | TLS | } thread.stack_info.delta
129 * +------------+ <- thread.stack_info.start + thread.stack_info.size
130 */
131#define ARCH_THREAD_STACK_RESERVED \
132 ROUND_UP(Z_RISCV_STACK_GUARD_SIZE + CONFIG_PRIVILEGED_STACK_SIZE, \
133 ARCH_STACK_PTR_ALIGN)
134
135#endif /* CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT */
136
137#ifdef CONFIG_64BIT
138#define RV_REGSIZE 8
139#define RV_REGSHIFT 3
140#else
141#define RV_REGSIZE 4
142#define RV_REGSHIFT 2
143#endif
144
145/* Common mstatus bits. All supported cores today have the same
146 * layouts.
147 */
148
149#define MSTATUS_IEN (1UL << 3)
150#define MSTATUS_MPP_M (3UL << 11)
151#define MSTATUS_MPIE_EN (1UL << 7)
152#define MSTATUS_FS_INIT (1UL << 13)
153#define MSTATUS_FS_MASK ((1UL << 13) | (1UL << 14))
154
155
156/* This comes from openisa_rv32m1, but doesn't seem to hurt on other
157 * platforms:
158 * - Preserve machine privileges in MPP. If you see any documentation
159 * telling you that MPP is read-only on this SoC, don't believe its
160 * lies.
161 * - Enable interrupts when exiting from exception into a new thread
162 * by setting MPIE now, so it will be copied into IE on mret.
163 */
164#define MSTATUS_DEF_RESTORE (MSTATUS_MPP_M | MSTATUS_MPIE_EN)
165
166#ifndef _ASMLANGUAGE
167#include <zephyr/sys/util.h>
168
169#ifdef __cplusplus
170extern "C" {
171#endif
172
173#ifdef CONFIG_IRQ_VECTOR_TABLE_JUMP_BY_CODE
174#define ARCH_IRQ_VECTOR_JUMP_CODE(v) "j " STRINGIFY(v)
175#endif
176
177/* Kernel macros for memory attribution
178 * (access permissions and cache-ability).
179 *
180 * The macros are to be stored in k_mem_partition_attr_t
181 * objects. The format of a k_mem_partition_attr_t object
182 * is an uint8_t composed by configuration register flags
183 * located in arch/riscv/include/core_pmp.h
184 */
185
186/* Read-Write access permission attributes */
187#define K_MEM_PARTITION_P_RW_U_RW ((k_mem_partition_attr_t) \
188 {PMP_R | PMP_W})
189#define K_MEM_PARTITION_P_RW_U_RO ((k_mem_partition_attr_t) \
190 {PMP_R})
191#define K_MEM_PARTITION_P_RW_U_NA ((k_mem_partition_attr_t) \
192 {0})
193#define K_MEM_PARTITION_P_RO_U_RO ((k_mem_partition_attr_t) \
194 {PMP_R})
195#define K_MEM_PARTITION_P_RO_U_NA ((k_mem_partition_attr_t) \
196 {0})
197#define K_MEM_PARTITION_P_NA_U_NA ((k_mem_partition_attr_t) \
198 {0})
199
200/* Execution-allowed attributes */
201#define K_MEM_PARTITION_P_RWX_U_RWX ((k_mem_partition_attr_t) \
202 {PMP_R | PMP_W | PMP_X})
203#define K_MEM_PARTITION_P_RX_U_RX ((k_mem_partition_attr_t) \
204 {PMP_R | PMP_X})
205
206/* Typedef for the k_mem_partition attribute */
207typedef struct {
210
211struct arch_mem_domain {
212 unsigned int pmp_update_nr;
213};
214
215extern void z_irq_spurious(const void *unused);
216
217/*
218 * use atomic instruction csrrc to lock global irq
219 * csrrc: atomic read and clear bits in CSR register
220 */
221static ALWAYS_INLINE unsigned int arch_irq_lock(void)
222{
223 unsigned int key;
224
225 __asm__ volatile ("csrrc %0, mstatus, %1"
226 : "=r" (key)
227 : "rK" (MSTATUS_IEN)
228 : "memory");
229
230 return key;
231}
232
233/*
234 * use atomic instruction csrs to unlock global irq
235 * csrs: atomic set bits in CSR register
236 */
237static ALWAYS_INLINE void arch_irq_unlock(unsigned int key)
238{
239 __asm__ volatile ("csrs mstatus, %0"
240 :
241 : "r" (key & MSTATUS_IEN)
242 : "memory");
243}
244
245static ALWAYS_INLINE bool arch_irq_unlocked(unsigned int key)
246{
247 return (key & MSTATUS_IEN) != 0;
248}
249
250static ALWAYS_INLINE void arch_nop(void)
251{
252 __asm__ volatile("nop");
253}
254
256
257static inline uint32_t arch_k_cycle_get_32(void)
258{
259 return sys_clock_cycle_get_32();
260}
261
263
264static inline uint64_t arch_k_cycle_get_64(void)
265{
266 return sys_clock_cycle_get_64();
267}
268
270
271#ifdef __cplusplus
272}
273#endif
274
275#endif /*_ASMLANGUAGE */
276
277#if defined(CONFIG_SOC_FAMILY_RISCV_PRIVILEGE)
279#endif
280
281
282#endif
uint32_t k_mem_partition_attr_t
Definition: arch.h:220
RISC-V public interrupt handling.
RISCV specific syscall header.
Per-arch thread definition.
#define ALWAYS_INLINE
Definition: common.h:124
Devicetree main header.
Public interface for configuring interrupts.
uint64_t sys_clock_cycle_get_64(void)
uint32_t sys_clock_cycle_get_32(void)
static ALWAYS_INLINE void arch_nop(void)
Definition: arch.h:250
static ALWAYS_INLINE unsigned int arch_irq_lock(void)
Definition: arch.h:221
#define MSTATUS_IEN
Definition: arch.h:149
static ALWAYS_INLINE void arch_irq_unlock(unsigned int key)
Definition: arch.h:237
static uint32_t arch_k_cycle_get_32(void)
Definition: arch.h:257
static uint64_t arch_k_cycle_get_64(void)
Definition: arch.h:264
static ALWAYS_INLINE bool arch_irq_unlocked(unsigned int key)
Definition: arch.h:245
RISCV public error handling.
RISCV public exception handling.
static k_spinlock_key_t key
Definition: spinlock_error_case.c:14
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT64_TYPE__ uint64_t
Definition: stdint.h:91
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
Definition: arch.h:46
unsigned int pmp_update_nr
Definition: arch.h:212
uint8_t pmp_attr
Definition: arch.h:208
Software-managed ISR table.
Misc utilities.