2015-04-10 16:44:37 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Wind River Systems, Inc.
|
|
|
|
*
|
2017-01-18 17:01:01 -08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-04-10 16:44:37 -07:00
|
|
|
*/
|
|
|
|
|
2015-12-04 10:09:39 -05:00
|
|
|
/**
|
|
|
|
* @file
|
2016-12-23 07:32:56 -05:00
|
|
|
* @brief ARC specific kernel interface header
|
2015-12-04 10:09:39 -05:00
|
|
|
*
|
2016-12-23 07:32:56 -05:00
|
|
|
* This header contains the ARC specific kernel interface. It is
|
|
|
|
* included by the kernel interface architecture-abstraction header
|
|
|
|
* include/arch/cpu.h)
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2018-09-14 10:43:44 -07:00
|
|
|
#ifndef ZEPHYR_INCLUDE_ARCH_ARC_ARCH_H_
|
|
|
|
#define ZEPHYR_INCLUDE_ARCH_ARC_ARCH_H_
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2020-01-16 13:29:53 +01:00
|
|
|
#include <devicetree.h>
|
2015-11-24 17:02:50 -06:00
|
|
|
#include <sw_isr_table.h>
|
2020-07-10 02:51:47 +03:00
|
|
|
#include <arch/common/ffs.h>
|
2019-10-25 00:08:21 +09:00
|
|
|
#include <arch/arc/thread.h>
|
2020-07-21 00:35:18 +03:00
|
|
|
#include <arch/common/sys_bitops.h>
|
2021-04-01 18:45:12 +03:00
|
|
|
#include "sys-io-common.h"
|
|
|
|
|
2015-05-28 10:56:47 -07:00
|
|
|
#include <arch/arc/v2/exc.h>
|
|
|
|
#include <arch/arc/v2/irq.h>
|
|
|
|
#include <arch/arc/v2/misc.h>
|
|
|
|
#include <arch/arc/v2/aux_regs.h>
|
|
|
|
#include <arch/arc/v2/arcv2_irq_unit.h>
|
2015-08-25 14:39:56 +03:00
|
|
|
#include <arch/arc/v2/asm_inline.h>
|
2019-06-02 21:44:42 -04:00
|
|
|
#include <arch/common/addr_types.h>
|
2021-04-01 20:29:47 +03:00
|
|
|
#include <arch/arc/v2/error.h>
|
|
|
|
|
2019-07-24 17:47:29 +08:00
|
|
|
#ifdef CONFIG_ARC_CONNECT
|
|
|
|
#include <arch/arc/v2/arc_connect.h>
|
|
|
|
#endif
|
2021-07-21 22:42:39 +03:00
|
|
|
|
|
|
|
#ifdef CONFIG_ISA_ARCV2
|
|
|
|
#include "v2/sys_io.h"
|
2019-08-01 12:39:35 +08:00
|
|
|
#ifdef CONFIG_ARC_HAS_SECURE
|
|
|
|
#include <arch/arc/v2/secureshield/arc_secure.h>
|
|
|
|
#endif
|
2015-04-10 16:44:37 -07:00
|
|
|
#endif
|
|
|
|
|
2020-04-23 13:55:56 -07:00
|
|
|
#ifndef _ASMLANGUAGE
|
|
|
|
|
2018-07-19 14:47:59 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2021-04-07 23:19:45 +03:00
|
|
|
#ifdef CONFIG_64BIT
|
|
|
|
#define ARCH_STACK_PTR_ALIGN 8
|
|
|
|
#else
|
2020-04-19 15:06:31 -07:00
|
|
|
#define ARCH_STACK_PTR_ALIGN 4
|
2021-04-07 23:19:45 +03:00
|
|
|
#endif /* CONFIG_64BIT */
|
2020-04-19 15:06:31 -07:00
|
|
|
|
2020-04-23 13:55:56 -07:00
|
|
|
/* Indicate, for a minimally sized MPU region, how large it must be and what
|
|
|
|
* its base address must be aligned to.
|
arch: arc: add user space support for arc
* add the implementation of syscall
* based on 'trap_s' intruction, id = 3
* add the privilege stack
* the privilege stack is allocted with thread stack
* for the kernel thread, the privilege stack is also a
part of thread stack, the start of stack can be configured
as stack guard
* for the user thread, no stack guard, when the user stack is
overflow, it will fall into kernel memory area which requires
kernel privilege, privilege violation will be raised
* modify the linker template and add MPU_ADDR_ALIGN
* add user space corresponding codes in mpu
* the user sp aux reg will be part of thread context
* When user thread is interruptted for the 1st time, the context is
saved in user stack (U bit of IRQ_CTLR is set to 1). When nest
interrupt comes, the context is saved in thread's privilege stack
* the arc_mpu_regions.c is moved to board folder, as it's board
specific
* the above codes have been tested through tests/kernel/mem_protect/
userspace for MPU version 2
Signed-off-by: Wayne Ren <wei.ren@synopsys.com>
2018-01-23 17:13:09 +08:00
|
|
|
*
|
2020-04-23 13:55:56 -07:00
|
|
|
* For regions that are NOT the minimum size, this define has no semantics
|
|
|
|
* on ARC MPUv2 as its regions must be power of two size and aligned to their
|
2021-02-23 15:10:14 +08:00
|
|
|
* own size. On ARC MPUv4, region sizes are arbitrary and this just indicates
|
2020-04-23 13:55:56 -07:00
|
|
|
* the required size granularity.
|
arch: arc: add user space support for arc
* add the implementation of syscall
* based on 'trap_s' intruction, id = 3
* add the privilege stack
* the privilege stack is allocted with thread stack
* for the kernel thread, the privilege stack is also a
part of thread stack, the start of stack can be configured
as stack guard
* for the user thread, no stack guard, when the user stack is
overflow, it will fall into kernel memory area which requires
kernel privilege, privilege violation will be raised
* modify the linker template and add MPU_ADDR_ALIGN
* add user space corresponding codes in mpu
* the user sp aux reg will be part of thread context
* When user thread is interruptted for the 1st time, the context is
saved in user stack (U bit of IRQ_CTLR is set to 1). When nest
interrupt comes, the context is saved in thread's privilege stack
* the arc_mpu_regions.c is moved to board folder, as it's board
specific
* the above codes have been tested through tests/kernel/mem_protect/
userspace for MPU version 2
Signed-off-by: Wayne Ren <wei.ren@synopsys.com>
2018-01-23 17:13:09 +08:00
|
|
|
*/
|
2020-04-23 13:55:56 -07:00
|
|
|
#ifdef CONFIG_ARC_CORE_MPU
|
2020-04-02 22:53:06 +08:00
|
|
|
#if CONFIG_ARC_MPU_VER == 2
|
2020-04-23 13:55:56 -07:00
|
|
|
#define Z_ARC_MPU_ALIGN 2048
|
2021-02-23 15:10:14 +08:00
|
|
|
#elif CONFIG_ARC_MPU_VER == 4
|
2020-04-23 13:55:56 -07:00
|
|
|
#define Z_ARC_MPU_ALIGN 32
|
|
|
|
#else
|
|
|
|
#error "Unsupported MPU version"
|
|
|
|
#endif
|
2020-04-02 22:53:06 +08:00
|
|
|
#endif
|
|
|
|
|
2020-04-23 13:55:56 -07:00
|
|
|
#ifdef CONFIG_MPU_STACK_GUARD
|
|
|
|
#define Z_ARC_STACK_GUARD_SIZE Z_ARC_MPU_ALIGN
|
|
|
|
#else
|
|
|
|
#define Z_ARC_STACK_GUARD_SIZE 0
|
|
|
|
#endif
|
2020-04-02 22:53:06 +08:00
|
|
|
|
2020-04-24 16:24:46 -07:00
|
|
|
/* Kernel-only stacks have the following layout if a stack guard is enabled:
|
|
|
|
*
|
|
|
|
* +------------+ <- thread.stack_obj
|
|
|
|
* | Guard | } Z_ARC_STACK_GUARD_SIZE
|
|
|
|
* +------------+ <- thread.stack_info.start
|
|
|
|
* | Kernel |
|
|
|
|
* | stack |
|
|
|
|
* | |
|
|
|
|
* +............|
|
|
|
|
* | TLS | } thread.stack_info.delta
|
|
|
|
* +------------+ <- thread.stack_info.start + thread.stack_info.size
|
|
|
|
*/
|
|
|
|
#ifdef CONFIG_MPU_STACK_GUARD
|
|
|
|
#define ARCH_KERNEL_STACK_RESERVED Z_ARC_STACK_GUARD_SIZE
|
|
|
|
#define ARCH_KERNEL_STACK_OBJ_ALIGN Z_ARC_MPU_ALIGN
|
|
|
|
#endif
|
|
|
|
|
2020-04-23 13:55:56 -07:00
|
|
|
#ifdef CONFIG_USERSPACE
|
|
|
|
/* Any thread running In user mode will have full access to the region denoted
|
|
|
|
* by thread.stack_info.
|
|
|
|
*
|
|
|
|
* Thread-local storage is at the very highest memory locations of this area.
|
|
|
|
* Memory for TLS and any initial random stack pointer offset is captured
|
|
|
|
* in thread.stack_info.delta.
|
2019-04-08 01:32:33 +08:00
|
|
|
*/
|
2020-04-23 13:55:56 -07:00
|
|
|
#ifdef CONFIG_MPU_STACK_GUARD
|
|
|
|
/* MPU guards are only supported with V3 MPU and later. In this configuration
|
|
|
|
* the stack object will contain the MPU guard, the privilege stack, and then
|
|
|
|
* the stack buffer in that order:
|
|
|
|
*
|
|
|
|
* +------------+ <- thread.stack_obj
|
|
|
|
* | Guard | } Z_ARC_STACK_GUARD_SIZE
|
|
|
|
* +------------+ <- thread.arch.priv_stack_start
|
|
|
|
* | Priv Stack | } CONFIG_PRIVILEGED_STACK_SIZE
|
|
|
|
* +------------+ <- thread.stack_info.start
|
|
|
|
* | Thread |
|
|
|
|
* | stack |
|
|
|
|
* | |
|
|
|
|
* +............|
|
|
|
|
* | TLS | } thread.stack_info.delta
|
|
|
|
* +------------+ <- thread.stack_info.start + thread.stack_info.size
|
|
|
|
*/
|
|
|
|
#define ARCH_THREAD_STACK_RESERVED (Z_ARC_STACK_GUARD_SIZE + \
|
|
|
|
CONFIG_PRIVILEGED_STACK_SIZE)
|
|
|
|
#define ARCH_THREAD_STACK_OBJ_ALIGN(size) Z_ARC_MPU_ALIGN
|
|
|
|
/* We need to be able to exactly cover the stack buffer with an MPU region,
|
|
|
|
* so round its size up to the required granularity of the MPU
|
|
|
|
*/
|
|
|
|
#define ARCH_THREAD_STACK_SIZE_ADJUST(size) \
|
|
|
|
(ROUND_UP((size), Z_ARC_MPU_ALIGN))
|
|
|
|
BUILD_ASSERT(CONFIG_PRIVILEGED_STACK_SIZE % Z_ARC_MPU_ALIGN == 0,
|
|
|
|
"improper privilege stack size");
|
|
|
|
#else /* !CONFIG_MPU_STACK_GUARD */
|
|
|
|
/* Userspace enabled, but supervisor stack guards are not in use */
|
|
|
|
#ifdef CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT
|
|
|
|
/* Use defaults for everything. The privilege elevation stack is located
|
|
|
|
* in another area of memory generated at build time by gen_kobject_list.py
|
|
|
|
*
|
|
|
|
* +------------+ <- thread.arch.priv_stack_start
|
2020-04-24 16:24:46 -07:00
|
|
|
* | Priv Stack | } Z_KERNEL_STACK_LEN(CONFIG_PRIVILEGED_STACK_SIZE)
|
2020-04-23 13:55:56 -07:00
|
|
|
* +------------+
|
|
|
|
*
|
|
|
|
* +------------+ <- thread.stack_obj = thread.stack_info.start
|
|
|
|
* | Thread |
|
|
|
|
* | stack |
|
|
|
|
* | |
|
|
|
|
* +............|
|
|
|
|
* | TLS | } thread.stack_info.delta
|
|
|
|
* +------------+ <- thread.stack_info.start + thread.stack_info.size
|
|
|
|
*/
|
|
|
|
#define ARCH_THREAD_STACK_SIZE_ADJUST(size) \
|
|
|
|
Z_POW2_CEIL(ROUND_UP((size), Z_ARC_MPU_ALIGN))
|
|
|
|
#define ARCH_THREAD_STACK_OBJ_ALIGN(size) \
|
|
|
|
ARCH_THREAD_STACK_SIZE_ADJUST(size)
|
|
|
|
#define ARCH_THREAD_STACK_RESERVED 0
|
|
|
|
#else /* !CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT */
|
|
|
|
/* Reserved area of the thread object just contains the privilege stack:
|
|
|
|
*
|
|
|
|
* +------------+ <- thread.stack_obj = thread.arch.priv_stack_start
|
|
|
|
* | Priv Stack | } CONFIG_PRIVILEGED_STACK_SIZE
|
|
|
|
* +------------+ <- thread.stack_info.start
|
|
|
|
* | Thread |
|
|
|
|
* | stack |
|
|
|
|
* | |
|
|
|
|
* +............|
|
|
|
|
* | TLS | } thread.stack_info.delta
|
|
|
|
* +------------+ <- thread.stack_info.start + thread.stack_info.size
|
|
|
|
*/
|
|
|
|
#define ARCH_THREAD_STACK_RESERVED CONFIG_PRIVILEGED_STACK_SIZE
|
|
|
|
#define ARCH_THREAD_STACK_SIZE_ADJUST(size) \
|
|
|
|
(ROUND_UP((size), Z_ARC_MPU_ALIGN))
|
|
|
|
#define ARCH_THREAD_STACK_OBJ_ALIGN(size) Z_ARC_MPU_ALIGN
|
|
|
|
|
|
|
|
BUILD_ASSERT(CONFIG_PRIVILEGED_STACK_SIZE % Z_ARC_MPU_ALIGN == 0,
|
|
|
|
"improper privilege stack size");
|
|
|
|
#endif /* CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT */
|
|
|
|
#endif /* CONFIG_MPU_STACK_GUARD */
|
2020-04-02 22:53:06 +08:00
|
|
|
|
2020-04-23 13:55:56 -07:00
|
|
|
#else /* !CONFIG_USERSPACE */
|
2020-04-02 22:53:06 +08:00
|
|
|
|
2020-04-23 13:55:56 -07:00
|
|
|
#ifdef CONFIG_MPU_STACK_GUARD
|
|
|
|
/* Only supported on ARC MPU V3 and higher. Reserve some memory for the stack
|
|
|
|
* guard. This is just a minimally-sized region at the beginning of the stack
|
|
|
|
* object, which is programmed to produce an exception if written to.
|
2019-04-09 00:12:45 +08:00
|
|
|
*
|
2020-04-23 13:55:56 -07:00
|
|
|
* +------------+ <- thread.stack_obj
|
|
|
|
* | Guard | } Z_ARC_STACK_GUARD_SIZE
|
|
|
|
* +------------+ <- thread.stack_info.start
|
|
|
|
* | Thread |
|
|
|
|
* | stack |
|
|
|
|
* | |
|
|
|
|
* +............|
|
|
|
|
* | TLS | } thread.stack_info.delta
|
|
|
|
* +------------+ <- thread.stack_info.start + thread.stack_info.size
|
2019-04-09 00:12:45 +08:00
|
|
|
*/
|
2020-04-23 13:55:56 -07:00
|
|
|
#define ARCH_THREAD_STACK_RESERVED Z_ARC_STACK_GUARD_SIZE
|
|
|
|
#define ARCH_THREAD_STACK_OBJ_ALIGN(size) Z_ARC_MPU_ALIGN
|
|
|
|
/* Default for ARCH_THREAD_STACK_SIZE_ADJUST */
|
|
|
|
#else /* !CONFIG_MPU_STACK_GUARD */
|
|
|
|
/* No stack guard, no userspace, Use defaults for everything. */
|
|
|
|
#endif /* CONFIG_MPU_STACK_GUARD */
|
2020-04-02 22:53:06 +08:00
|
|
|
#endif /* CONFIG_USERSPACE */
|
arch: arc: add user space support for arc
* add the implementation of syscall
* based on 'trap_s' intruction, id = 3
* add the privilege stack
* the privilege stack is allocted with thread stack
* for the kernel thread, the privilege stack is also a
part of thread stack, the start of stack can be configured
as stack guard
* for the user thread, no stack guard, when the user stack is
overflow, it will fall into kernel memory area which requires
kernel privilege, privilege violation will be raised
* modify the linker template and add MPU_ADDR_ALIGN
* add user space corresponding codes in mpu
* the user sp aux reg will be part of thread context
* When user thread is interruptted for the 1st time, the context is
saved in user stack (U bit of IRQ_CTLR is set to 1). When nest
interrupt comes, the context is saved in thread's privilege stack
* the arc_mpu_regions.c is moved to board folder, as it's board
specific
* the above codes have been tested through tests/kernel/mem_protect/
userspace for MPU version 2
Signed-off-by: Wayne Ren <wei.ren@synopsys.com>
2018-01-23 17:13:09 +08:00
|
|
|
|
2017-12-20 16:48:45 +08:00
|
|
|
#ifdef CONFIG_ARC_MPU
|
2019-08-12 12:52:11 -05:00
|
|
|
|
|
|
|
/* Legacy case: retain containing extern "C" with C++ */
|
2017-12-20 16:48:45 +08:00
|
|
|
#include <arch/arc/v2/mpu/arc_mpu.h>
|
2015-05-26 10:21:42 -04:00
|
|
|
|
2019-03-05 10:29:16 +08:00
|
|
|
#define K_MEM_PARTITION_P_NA_U_NA AUX_MPU_ATTR_N
|
|
|
|
#define K_MEM_PARTITION_P_RW_U_RW (AUX_MPU_ATTR_UW | AUX_MPU_ATTR_UR | \
|
|
|
|
AUX_MPU_ATTR_KW | AUX_MPU_ATTR_KR)
|
|
|
|
#define K_MEM_PARTITION_P_RW_U_RO (AUX_MPU_ATTR_UR | \
|
|
|
|
AUX_MPU_ATTR_KW | AUX_MPU_ATTR_KR)
|
|
|
|
#define K_MEM_PARTITION_P_RW_U_NA (AUX_MPU_ATTR_KW | AUX_MPU_ATTR_KR)
|
|
|
|
#define K_MEM_PARTITION_P_RO_U_RO (AUX_MPU_ATTR_UR | AUX_MPU_ATTR_KR)
|
|
|
|
#define K_MEM_PARTITION_P_RO_U_NA (AUX_MPU_ATTR_KR)
|
2017-12-20 16:48:45 +08:00
|
|
|
|
|
|
|
/* Execution-allowed attributes */
|
2019-03-05 10:29:16 +08:00
|
|
|
#define K_MEM_PARTITION_P_RWX_U_RWX (AUX_MPU_ATTR_UW | AUX_MPU_ATTR_UR | \
|
|
|
|
AUX_MPU_ATTR_KW | AUX_MPU_ATTR_KR | \
|
|
|
|
AUX_MPU_ATTR_KE | AUX_MPU_ATTR_UE)
|
|
|
|
#define K_MEM_PARTITION_P_RWX_U_RX (AUX_MPU_ATTR_UR | \
|
|
|
|
AUX_MPU_ATTR_KW | AUX_MPU_ATTR_KR | \
|
|
|
|
AUX_MPU_ATTR_KE | AUX_MPU_ATTR_UE)
|
|
|
|
#define K_MEM_PARTITION_P_RX_U_RX (AUX_MPU_ATTR_UR | \
|
|
|
|
AUX_MPU_ATTR_KR | \
|
|
|
|
AUX_MPU_ATTR_KE | AUX_MPU_ATTR_UE)
|
2017-12-20 16:48:45 +08:00
|
|
|
|
|
|
|
#define K_MEM_PARTITION_IS_WRITABLE(attr) \
|
|
|
|
({ \
|
|
|
|
int __is_writable__; \
|
2019-11-28 16:27:59 +08:00
|
|
|
switch (attr & (AUX_MPU_ATTR_UW | AUX_MPU_ATTR_KW)) { \
|
2019-03-05 10:29:16 +08:00
|
|
|
case (AUX_MPU_ATTR_UW | AUX_MPU_ATTR_KW): \
|
|
|
|
case AUX_MPU_ATTR_UW: \
|
|
|
|
case AUX_MPU_ATTR_KW: \
|
2017-12-20 16:48:45 +08:00
|
|
|
__is_writable__ = 1; \
|
|
|
|
break; \
|
|
|
|
default: \
|
|
|
|
__is_writable__ = 0; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
__is_writable__; \
|
|
|
|
})
|
|
|
|
#define K_MEM_PARTITION_IS_EXECUTABLE(attr) \
|
2019-03-05 10:29:16 +08:00
|
|
|
((attr) & (AUX_MPU_ATTR_KE | AUX_MPU_ATTR_UE))
|
2017-12-20 16:48:45 +08:00
|
|
|
|
|
|
|
#if CONFIG_ARC_MPU_VER == 2
|
|
|
|
#define _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size) \
|
2020-04-23 13:55:56 -07:00
|
|
|
BUILD_ASSERT(!(((size) & ((size) - 1))) && (size) >= Z_ARC_MPU_ALIGN \
|
2020-05-27 11:26:57 -05:00
|
|
|
&& !((uint32_t)(start) & ((size) - 1)), \
|
2017-12-20 16:48:45 +08:00
|
|
|
"the size of the partition must be power of 2" \
|
|
|
|
" and greater than or equal to the mpu adddress alignment." \
|
|
|
|
"start address of the partition must align with size.")
|
2021-02-23 15:10:14 +08:00
|
|
|
#elif CONFIG_ARC_MPU_VER == 4
|
2017-12-20 16:48:45 +08:00
|
|
|
#define _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size) \
|
2020-04-23 13:55:56 -07:00
|
|
|
BUILD_ASSERT((size) % Z_ARC_MPU_ALIGN == 0 && \
|
|
|
|
(size) >= Z_ARC_MPU_ALIGN && \
|
|
|
|
(uint32_t)(start) % Z_ARC_MPU_ALIGN == 0, \
|
|
|
|
"the size of the partition must align with 32" \
|
|
|
|
" and greater than or equal to 32." \
|
|
|
|
"start address of the partition must align with 32.")
|
2017-12-20 16:48:45 +08:00
|
|
|
#endif
|
|
|
|
#endif /* CONFIG_ARC_MPU*/
|
|
|
|
|
|
|
|
/* Typedef for the k_mem_partition attribute*/
|
2020-05-27 11:26:57 -05:00
|
|
|
typedef uint32_t k_mem_partition_attr_t;
|
2018-12-04 17:15:27 -08:00
|
|
|
|
2019-11-07 12:43:29 -08:00
|
|
|
static ALWAYS_INLINE void arch_nop(void)
|
2018-12-04 17:15:27 -08:00
|
|
|
{
|
2020-07-20 18:14:58 +03:00
|
|
|
__builtin_arc_nop();
|
2018-12-04 17:15:27 -08:00
|
|
|
}
|
|
|
|
|
2017-12-20 16:48:45 +08:00
|
|
|
#endif /* _ASMLANGUAGE */
|
|
|
|
|
2015-04-10 16:44:37 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
2018-09-14 10:43:44 -07:00
|
|
|
#endif /* ZEPHYR_INCLUDE_ARCH_ARC_ARCH_H_ */
|