Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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
16
17#ifndef ZEPHYR_INCLUDE_ARCH_RISCV_ARCH_H_
18#define ZEPHYR_INCLUDE_ARCH_RISCV_ARCH_H_
19
27#if defined(CONFIG_USERSPACE)
29#endif /* CONFIG_USERSPACE */
30#include <zephyr/irq.h>
31#include <zephyr/sw_isr_table.h>
32#include <zephyr/devicetree.h>
34
35/* stacks, for RISCV architecture stack should be 16byte-aligned */
36#define ARCH_STACK_PTR_ALIGN 16
37
38#define Z_RISCV_STACK_PMP_ALIGN \
39 MAX(CONFIG_PMP_GRANULARITY, ARCH_STACK_PTR_ALIGN)
40
41#ifdef CONFIG_PMP_STACK_GUARD
42/*
43 * The StackGuard is an area at the bottom of the kernel-mode stack made to
44 * fault when accessed. It is _not_ faulting when in exception mode as we rely
45 * on that area to save the exception stack frame and to process said fault.
46 * Therefore the guard area must be large enough to hold the esf, plus some
47 * configurable stack wiggle room to execute the fault handling code off of,
48 * as well as some guard size to cover possible sudden stack pointer
49 * displacement before the fault.
50 */
51#ifdef CONFIG_PMP_POWER_OF_TWO_ALIGNMENT
52#define Z_RISCV_STACK_GUARD_SIZE \
53 Z_POW2_CEIL(MAX(sizeof(struct arch_esf) + CONFIG_PMP_STACK_GUARD_MIN_SIZE, \
54 Z_RISCV_STACK_PMP_ALIGN))
55#define ARCH_KERNEL_STACK_OBJ_ALIGN Z_RISCV_STACK_GUARD_SIZE
56#else
57#define Z_RISCV_STACK_GUARD_SIZE \
58 ROUND_UP(sizeof(struct arch_esf) + CONFIG_PMP_STACK_GUARD_MIN_SIZE, \
59 Z_RISCV_STACK_PMP_ALIGN)
60#define ARCH_KERNEL_STACK_OBJ_ALIGN Z_RISCV_STACK_PMP_ALIGN
61#endif
62
63/* Kernel-only stacks have the following layout if a stack guard is enabled:
64 *
65 * +------------+ <- thread.stack_obj
66 * | Guard | } Z_RISCV_STACK_GUARD_SIZE
67 * +------------+ <- thread.stack_info.start
68 * | Kernel |
69 * | stack |
70 * | |
71 * +............|
72 * | TLS | } thread.stack_info.delta
73 * +------------+ <- thread.stack_info.start + thread.stack_info.size
74 */
75#define ARCH_KERNEL_STACK_RESERVED Z_RISCV_STACK_GUARD_SIZE
76
77#else /* !CONFIG_PMP_STACK_GUARD */
78#define Z_RISCV_STACK_GUARD_SIZE 0
79#endif
80
81#ifdef CONFIG_PMP_POWER_OF_TWO_ALIGNMENT
82/* The privilege elevation stack is located in another area of memory
83 * generated at build time by gen_kobject_list.py
84 *
85 * +------------+ <- thread.arch.priv_stack_start
86 * | Guard | } Z_RISCV_STACK_GUARD_SIZE
87 * +------------+
88 * | Priv Stack | } CONFIG_PRIVILEGED_STACK_SIZE
89 * +------------+ <- thread.arch.priv_stack_start +
90 * CONFIG_PRIVILEGED_STACK_SIZE +
91 * Z_RISCV_STACK_GUARD_SIZE
92 *
93 * The main stack will be initially (or potentially only) used by kernel
94 * mode so we need to make room for a possible stack guard area when enabled:
95 *
96 * +------------+ <- thread.stack_obj
97 * | Guard | } Z_RISCV_STACK_GUARD_SIZE
98 * +............| <- thread.stack_info.start
99 * | Thread |
100 * | stack |
101 * | |
102 * +............|
103 * | TLS | } thread.stack_info.delta
104 * +------------+ <- thread.stack_info.start + thread.stack_info.size
105 *
106 * When transitioning to user space, the guard area will be removed from
107 * the main stack. Any thread running in user mode will have full access
108 * to the region denoted by thread.stack_info. Make it PMP-NAPOT compatible.
109 *
110 * +------------+ <- thread.stack_obj = thread.stack_info.start
111 * | Thread |
112 * | stack |
113 * | |
114 * +............|
115 * | TLS | } thread.stack_info.delta
116 * +------------+ <- thread.stack_info.start + thread.stack_info.size
117 */
118#define ARCH_THREAD_STACK_RESERVED Z_RISCV_STACK_GUARD_SIZE
119#define ARCH_THREAD_STACK_SIZE_ADJUST(size) \
120 Z_POW2_CEIL(MAX(MAX(size, CONFIG_PRIVILEGED_STACK_SIZE), \
121 Z_RISCV_STACK_PMP_ALIGN))
122#define ARCH_THREAD_STACK_OBJ_ALIGN(size) \
123 ARCH_THREAD_STACK_SIZE_ADJUST(size)
124
125#else /* !CONFIG_PMP_POWER_OF_TWO_ALIGNMENT */
126
127/* The stack object will contain the PMP guard, the privilege stack, and then
128 * the usermode stack buffer in that order:
129 *
130 * +------------+ <- thread.stack_obj
131 * | Guard | } Z_RISCV_STACK_GUARD_SIZE
132 * +------------+
133 * | Priv Stack | } CONFIG_PRIVILEGED_STACK_SIZE
134 * +------------+ <- thread.stack_info.start
135 * | Thread |
136 * | stack |
137 * | |
138 * +............|
139 * | TLS | } thread.stack_info.delta
140 * +------------+ <- thread.stack_info.start + thread.stack_info.size
141 */
142#define ARCH_THREAD_STACK_RESERVED \
143 ROUND_UP(Z_RISCV_STACK_GUARD_SIZE + CONFIG_PRIVILEGED_STACK_SIZE, \
144 Z_RISCV_STACK_PMP_ALIGN)
145#define ARCH_THREAD_STACK_SIZE_ADJUST(size) \
146 ROUND_UP(size, Z_RISCV_STACK_PMP_ALIGN)
147#define ARCH_THREAD_STACK_OBJ_ALIGN(size) Z_RISCV_STACK_PMP_ALIGN
148#endif /* CONFIG_PMP_POWER_OF_TWO_ALIGNMENT */
149
150#ifdef CONFIG_64BIT
151#define RV_REGSIZE 8
152#define RV_REGSHIFT 3
153#else
154#define RV_REGSIZE 4
155#define RV_REGSHIFT 2
156#endif
157
158/* Common mstatus bits. All supported cores today have the same
159 * layouts.
160 */
161
162#define MSTATUS_IEN (1UL << 3)
164#define MSTATUS_MPP_U (PRV_U << 11)
166#define MSTATUS_MPP_S (PRV_S << 11)
168#define MSTATUS_MPP_M (PRV_M << 11)
169#define MSTATUS_MPIE_EN (1UL << 7)
170
171#define MSTATUS_FS_OFF (0UL << 13)
172#define MSTATUS_FS_INIT (1UL << 13)
173#define MSTATUS_FS_CLEAN (2UL << 13)
174#define MSTATUS_FS_DIRTY (3UL << 13)
175
191#ifdef CONFIG_RISCV_S_MODE
192#define RV_STATUS_DEF_RESTORE (SSTATUS_SPP | SSTATUS_SPIE)
193#else
194#define RV_STATUS_DEF_RESTORE (MSTATUS_MPP_M | MSTATUS_MPIE_EN)
195#endif
196
197/* Previous-privilege field and its user-mode value, abstracted across M/S mode.
198 * Use as: (esf->mstatus & RV_STATUS_PP) == RV_STATUS_PP_U
199 */
200#ifdef CONFIG_RISCV_S_MODE
202#define RV_STATUS_PP SSTATUS_SPP
204#define RV_STATUS_PP_U 0
205#else
207#define RV_STATUS_PP MSTATUS_MPP
209#define RV_STATUS_PP_U PRV_U
210#endif
211
212#ifndef _ASMLANGUAGE
213#include <zephyr/sys/util.h>
214#include <zephyr/sys/slist.h>
215
216#ifdef __cplusplus
217extern "C" {
218#endif
219
220#ifdef CONFIG_IRQ_VECTOR_TABLE_JUMP_BY_CODE
221#define ARCH_IRQ_VECTOR_JUMP_CODE(v) "j " STRINGIFY(v)
222#endif
223
224/* Kernel macros for memory attribution
225 * (access permissions and cache-ability).
226 *
227 * The macros are to be stored in k_mem_partition_attr_t
228 * objects. The format of a k_mem_partition_attr_t object
229 * is an uint8_t composed by configuration register flags
230 * located in arch/riscv/include/core_pmp.h
231 */
232
233/* Read-Write access permission attributes */
234#define K_MEM_PARTITION_P_RW_U_RW ((k_mem_partition_attr_t) \
235 {PMP_R | PMP_W})
236#define K_MEM_PARTITION_P_RW_U_RO ((k_mem_partition_attr_t) \
237 {PMP_R})
238#define K_MEM_PARTITION_P_RW_U_NA ((k_mem_partition_attr_t) \
239 {0})
240#define K_MEM_PARTITION_P_RO_U_RO ((k_mem_partition_attr_t) \
241 {PMP_R})
242#define K_MEM_PARTITION_P_RO_U_NA ((k_mem_partition_attr_t) \
243 {0})
244#define K_MEM_PARTITION_P_NA_U_NA ((k_mem_partition_attr_t) \
245 {0})
246
247/* Execution-allowed attributes */
248#define K_MEM_PARTITION_P_RWX_U_RWX ((k_mem_partition_attr_t) \
249 {PMP_R | PMP_W | PMP_X})
250#define K_MEM_PARTITION_P_RX_U_RX ((k_mem_partition_attr_t) \
251 {PMP_R | PMP_X})
252
261#define K_MEM_PARTITION_IS_WRITABLE(attr) (attr.pmp_attr & PMP_W)
262
272#define K_MEM_PARTITION_IS_EXECUTABLE(attr) (attr.pmp_attr & PMP_X)
273
274/* Typedef for the k_mem_partition attribute */
275typedef struct {
278
279struct arch_mem_domain {
280 unsigned int pmp_update_nr;
281};
282
283extern void z_irq_spurious(const void *unused);
284
285/* Privilege-level abstraction for IRQ enable/disable CSR and bit */
286#ifdef CONFIG_RISCV_S_MODE
288#define RV_STATUS_CSR "sstatus"
290#define RV_STATUS_IE SSTATUS_SIE
292#define RV_STATUS_PIE SSTATUS_SPIE
293#else
295#define RV_STATUS_CSR "mstatus"
297#define RV_STATUS_IE MSTATUS_IEN
299#define RV_STATUS_PIE MSTATUS_MPIE_EN
300#endif
301
307static ALWAYS_INLINE unsigned long z_riscv_status_read(void)
308{
309 unsigned long __rv;
310
311 __asm__ volatile ("csrr %0, " RV_STATUS_CSR : "=r" (__rv));
312 return __rv;
313}
314
318static ALWAYS_INLINE void z_riscv_status_set(unsigned long val)
319{
320 __asm__ volatile ("csrs " RV_STATUS_CSR ", %0" : : "rK" (val) : "memory");
321}
322
326static ALWAYS_INLINE void z_riscv_status_clear(unsigned long val)
327{
328 __asm__ volatile ("csrc " RV_STATUS_CSR ", %0" : : "rK" (val) : "memory");
329}
330
331/*
332 * use atomic instruction csrrc to lock global irq
333 * csrrc: atomic read and clear bits in CSR register
334 */
335static ALWAYS_INLINE unsigned int arch_irq_lock(void)
336{
337#ifdef CONFIG_RISCV_SOC_HAS_CUSTOM_IRQ_LOCK_OPS
338 return z_soc_irq_lock();
339#else
340 unsigned int key;
341
342 __asm__ volatile ("csrrc %0, " RV_STATUS_CSR ", %1"
343 : "=r" (key)
344 : "rK" (RV_STATUS_IE)
345 : "memory");
346
347 return key;
348#endif
349}
350
351/*
352 * use atomic instruction csrs to unlock global irq
353 * csrs: atomic set bits in CSR register
354 */
355static ALWAYS_INLINE void arch_irq_unlock(unsigned int key)
356{
357#ifdef CONFIG_RISCV_SOC_HAS_CUSTOM_IRQ_LOCK_OPS
358 z_soc_irq_unlock(key);
359#else
360 __asm__ volatile ("csrs " RV_STATUS_CSR ", %0"
361 :
362 : "r" (key & RV_STATUS_IE)
363 : "memory");
364#endif
365}
366
367static ALWAYS_INLINE bool arch_irq_unlocked(unsigned int key)
368{
369#ifdef CONFIG_RISCV_SOC_HAS_CUSTOM_IRQ_LOCK_OPS
370 return z_soc_irq_unlocked(key);
371#else
372 return (key & RV_STATUS_IE) != 0;
373#endif
374}
375
378{
379#ifdef CONFIG_RISCV_SOC_HAS_CUSTOM_IRQ_LOCK_OPS
380 /* No direct probe primitive in the SoC ops; fall back to
381 * briefly locking and restoring.
382 */
383 unsigned int key = z_soc_irq_lock();
384 bool enabled = z_soc_irq_unlocked(key);
385
386 z_soc_irq_unlock(key);
387 return enabled;
388#else
389 unsigned int status = z_riscv_status_read();
390
391 return (status & RV_STATUS_IE) != 0;
392#endif
393}
394
395static ALWAYS_INLINE void arch_nop(void)
396{
397 __asm__ volatile("nop");
398}
399
401
402static inline uint32_t arch_k_cycle_get_32(void)
403{
404 return sys_clock_cycle_get_32();
405}
406
408
409static inline uint64_t arch_k_cycle_get_64(void)
410{
411 return sys_clock_cycle_get_64();
412}
413
415
416#ifdef __cplusplus
417}
418#endif
419
420#endif /*_ASMLANGUAGE */
421
422#if defined(CONFIG_RISCV_PRIVILEGED)
424#endif
425
426
427#endif
static ALWAYS_INLINE void arch_nop(void)
Definition arch.h:351
Header file for bit reverse operations.
RISC-V public interrupt handling.
RISCV specific syscall header.
Per-arch thread definition.
Devicetree main header.
RISCV public error handling.
#define ALWAYS_INLINE
Definition common.h:180
Public interface for configuring interrupts.
static ALWAYS_INLINE unsigned int arch_irq_lock(void)
Definition arch.h:72
static ALWAYS_INLINE void arch_irq_unlock(unsigned int key)
Definition arch.h:83
uint64_t sys_clock_cycle_get_64(void)
uint32_t sys_clock_cycle_get_32(void)
static uint32_t arch_k_cycle_get_32(void)
Definition arch.h:114
static ALWAYS_INLINE bool arch_cpu_irqs_are_enabled(void)
Implementation of arch_cpu_irqs_are_enabled.
Definition arch.h:102
static uint64_t arch_k_cycle_get_64(void)
Definition arch.h:121
static ALWAYS_INLINE bool arch_irq_unlocked(unsigned int key)
Definition arch.h:96
#define RV_STATUS_IE
Interrupt-enable bit in the status CSR (M-mode: MIE).
Definition arch.h:297
#define RV_STATUS_CSR
Name of the interrupt-status CSR as a string literal (M-mode).
Definition arch.h:295
Header file for the single-linked list API.
__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:47
unsigned int pmp_update_nr
Definition arch.h:280
Definition arm_mpu_v7m.h:155
uint8_t pmp_attr
Definition arch.h:276
Software-managed ISR table.
Misc utilities.