Zephyr Project API  3.3.0
A Scalable Open Source RTOS
asm_inline_gcc.h
Go to the documentation of this file.
1/* ARM AArch32 GCC specific public inline assembler functions and macros */
2
3/*
4 * Copyright (c) 2015, Wind River Systems, Inc.
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9/* Either public functions or macros or invoked by public functions */
10
11#ifndef ZEPHYR_INCLUDE_ARCH_ARM_AARCH32_ASM_INLINE_GCC_H_
12#define ZEPHYR_INCLUDE_ARCH_ARM_AARCH32_ASM_INLINE_GCC_H_
13
14/*
15 * The file must not be included directly
16 * Include arch/cpu.h instead
17 */
18
19#ifndef _ASMLANGUAGE
20
21#include <zephyr/types.h>
23
24#if defined(CONFIG_CPU_AARCH32_CORTEX_R) || defined(CONFIG_CPU_AARCH32_CORTEX_A)
26#endif
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/* On ARMv7-M and ARMv8-M Mainline CPUs, this function prevents regular
33 * exceptions (i.e. with interrupt priority lower than or equal to
34 * _EXC_IRQ_DEFAULT_PRIO) from interrupting the CPU. NMI, Faults, SVC,
35 * and Zero Latency IRQs (if supported) may still interrupt the CPU.
36 *
37 * On ARMv6-M and ARMv8-M Baseline CPUs, this function reads the value of
38 * PRIMASK which shows if interrupts are enabled, then disables all interrupts
39 * except NMI.
40 */
41
42static ALWAYS_INLINE unsigned int arch_irq_lock(void)
43{
44 unsigned int key;
45
46#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
47 __asm__ volatile("mrs %0, PRIMASK;"
48 "cpsid i"
49 : "=r" (key)
50 :
51 : "memory");
52#elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
53 unsigned int tmp;
54
55 __asm__ volatile(
56 "mov %1, %2;"
57 "mrs %0, BASEPRI;"
58 "msr BASEPRI_MAX, %1;"
59 "isb;"
60 : "=r"(key), "=r"(tmp)
61 : "i"(_EXC_IRQ_DEFAULT_PRIO)
62 : "memory");
63#elif defined(CONFIG_ARMV7_R) || defined(CONFIG_AARCH32_ARMV8_R) \
64 || defined(CONFIG_ARMV7_A)
65 __asm__ volatile(
66 "mrs %0, cpsr;"
67 "and %0, #" TOSTR(I_BIT) ";"
68 "cpsid i;"
69 : "=r" (key)
70 :
71 : "memory", "cc");
72#else
73#error Unknown ARM architecture
74#endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
75
76 return key;
77}
78
79
80/* On Cortex-M0/M0+, this enables all interrupts if they were not
81 * previously disabled.
82 */
83
84static ALWAYS_INLINE void arch_irq_unlock(unsigned int key)
85{
86#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
87 if (key != 0U) {
88 return;
89 }
90 __asm__ volatile(
91 "cpsie i;"
92 "isb"
93 : : : "memory");
94#elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
95 __asm__ volatile(
96 "msr BASEPRI, %0;"
97 "isb;"
98 : : "r"(key) : "memory");
99#elif defined(CONFIG_ARMV7_R) || defined(CONFIG_AARCH32_ARMV8_R) \
100 || defined(CONFIG_ARMV7_A)
101 if (key != 0U) {
102 return;
103 }
104 __asm__ volatile(
105 "cpsie i;"
106 : : : "memory", "cc");
107#else
108#error Unknown ARM architecture
109#endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
110}
111
112static ALWAYS_INLINE bool arch_irq_unlocked(unsigned int key)
113{
114 /* This convention works for both PRIMASK and BASEPRI */
115 return key == 0U;
116}
117
118#ifdef __cplusplus
119}
120#endif
121
122#endif /* _ASMLANGUAGE */
123
124#endif /* ZEPHYR_INCLUDE_ARCH_ARM_AARCH32_ASM_INLINE_GCC_H_ */
#define TOSTR(s)
Definition: irq.h:80
static ALWAYS_INLINE unsigned int arch_irq_lock(void)
Definition: asm_inline_gcc.h:42
static ALWAYS_INLINE void arch_irq_unlock(unsigned int key)
Definition: asm_inline_gcc.h:84
static ALWAYS_INLINE bool arch_irq_unlocked(unsigned int key)
Definition: asm_inline_gcc.h:112
ARM AArch32 public exception handling.
#define ALWAYS_INLINE
Definition: common.h:124
#define I_BIT
Definition: cpu.h:30
static k_spinlock_key_t key
Definition: spinlock_error_case.c:15