FreeNOS
ARMCore.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __ARM_CORE_H
19 #define __ARM_CORE_H
20 
21 #include <FreeNOS/Constant.h>
22 #include <Types.h>
23 #include <Macros.h>
24 #include "ARMControl.h"
25 
44 #define IRQ(vector) (vector)
45 
51 #define mrc(coproc, opcode1, opcode2, reg, subReg) \
52 ({ \
53  ulong r; \
54  asm volatile("mrc " QUOTE(coproc) ", " QUOTE(opcode1) ", %0, " QUOTE(reg) ", " QUOTE(subReg) ", " QUOTE(opcode2) "\n" : "=r"(r) :: "memory"); \
55  r; \
56 })
57 
63 #define mcr(coproc, opcode1, opcode2, reg, subReg, value) \
64 ({ \
65  u32 val = (value); \
66  asm volatile("mcr " QUOTE(coproc) ", " QUOTE(opcode1) ", %0, " QUOTE(reg) ", " QUOTE(subReg) ", " QUOTE(opcode2) "\n" : : "r"(val) : "memory"); \
67 })
68 
74 #define mrrc(coproc, opcode1, CRm) \
75 ({ \
76  u64 r; \
77  asm volatile("mrrc " QUOTE(coproc) ", " QUOTE(opcode1) ", %Q0, %R0, " QUOTE(CRm) "\n" : "=r"(r) :: "memory"); \
78  r; \
79 })
80 
86 #define mcrr(coproc, opcode1, CRm, value) \
87 ({ \
88  u64 val = (value); \
89  asm volatile("mcrr " QUOTE(coproc) ", " QUOTE(opcode1) ", %Q0, %R0, " QUOTE(CRm) "\n" : : "r"(val) : "memory"); \
90 })
91 
97 #define timestamp() 0
98 
102 #define cpu_reboot()
103 
110 #define cpu_shutdown()
111 
115 #define idle() \
116  asm volatile ("wfi")
117 
121 #define vbar_set(addr) \
122  mcr(p15, 0, 0, c12, c0, (addr))
123 
127 #define sysctrl_read() \
128  (mrc(p15, 0, 0, c1, c0))
129 
133 #define sysctrl_write(val) \
134  mcr(p15, 0, 0, c1, c0, (val))
135 
145 #define read_core_id() \
146  (mrc(p15, 0, 5, c0, c0) & 0xff)
147 
148 #ifdef ARMV6
149 
152 #define tlb_flush_all() \
153 ({ \
154  ARMControl ctrl; \
155  ctrl.write(ARMControl::UnifiedTLBClear, 0); \
156 })
157 #else
158 
161 inline void tlb_flush_all()
162 {
163  asm volatile ("mcr p15, 0, %0, c8, c7, 0" :: "r"(0) : "memory");
164 }
165 #endif /* ARMV6 */
166 
167 #define tlb_invalidate(page) \
168 ({ \
169  mcr(p15, 0, 1, c8, c7, (page)); \
170 })
171 
180 inline void dmb()
181 {
182 #ifdef ARMV7
183  asm volatile ("dmb" ::: "memory");
184 #else
185  asm volatile ("mcr p15, 0, %0, c7, c10, 5" : : "r" (0));
186 #endif /* ARMV7 */
187 }
188 
198 inline void dsb()
199 {
200 #ifdef ARMV7
201  asm volatile ("dsb" ::: "memory");
202 #else
203  asm volatile ("mcr p15, 0, %0, c7, c10, 4" : : "r" (0));
204 #endif /* ARMV7 */
205 }
206 
211 {
212  asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0) : "memory");
213 }
214 
218 inline void isb()
219 {
220 #ifdef ARMV7
221  asm volatile ("isb" ::: "memory");
222 #else
223  asm volatile ("mcr p15, 0, %0, c7, c5, 4" : : "r" (0) : "memory");
224 #endif
225 }
226 
230 inline void flushPrefetchBuffer()
231 {
232 #ifdef ARMV7
233  isb();
234 #else
235  asm volatile ("mcr p15, 0, %0, c7, c5, 4" : : "r" (0) : "memory");
236 #endif /* ARMV7 */
237 }
238 
239 
243 typedef struct CPUState
244 {
248  u32 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12;
250 }
251 ALIGN(4) CPUState;
252 
256 class ARMCore
257 {
258  public:
259 
263  enum Result
264  {
265  Success = 0,
266  };
267 
268  public:
269 
275  void logException(CPUState *state) const;
276 
282  void logState(CPUState *state) const;
283 
291  void logRegister(const char *name, u32 reg, const char *text = "") const;
292 };
293 
300 #endif /* __ARM_CORE_H */
isb
void isb()
Instruction Synchronisation Barrier (ARMv7 and above)
Definition: ARMCore.h:218
CPUState::r12
u32 r12
Definition: ARMCore.h:248
Macros.h
Types.h
dsb
void dsb()
Data Synchronisation Barrier.
Definition: ARMCore.h:198
CPUState::r9
u32 r9
Definition: ARMCore.h:248
CPUState::sp
u32 sp
Definition: ARMCore.h:247
flushPrefetchBuffer
void flushPrefetchBuffer()
Flush Prefetch Buffer.
Definition: ARMCore.h:230
CPUState::r6
u32 r6
Definition: ARMCore.h:248
tlb_flush_all
void tlb_flush_all()
Flush the entire Translation Lookaside Buffer.
Definition: ARMCore.h:161
ALIGN
class ARMCore ALIGN
FileSystem::Success
@ Success
Definition: FileSystem.h:54
CPUState::cpsr
u32 cpsr
Definition: ARMCore.h:246
CPUState::r0
u32 r0
Definition: ARMCore.h:248
CPUState::padding
u32 padding[4]
Definition: ARMCore.h:245
CPUState::r5
u32 r5
Definition: ARMCore.h:248
u32
unsigned int u32
Unsigned 32-bit number.
Definition: Types.h:53
CPUState::r11
u32 r11
Definition: ARMCore.h:248
CPUState::pc
u32 pc
Definition: ARMCore.h:249
ARMCore::Result
Result
Result codes.
Definition: ARMCore.h:263
CPUState
Contains all the CPU registers.
Definition: ARMCore.h:243
CPUState::r7
u32 r7
Definition: ARMCore.h:248
CPUState::lr
u32 lr
Definition: ARMCore.h:247
CPUState::r1
u32 r1
Definition: ARMCore.h:248
ARMControl.h
CPUState::r8
u32 r8
Definition: ARMCore.h:248
ARMCore
Class representing an ARM processor core.
Definition: ARMCore.h:256
CPUState::r3
u32 r3
Definition: ARMCore.h:248
CPUState::r2
u32 r2
Definition: ARMCore.h:248
CPUState::r4
u32 r4
Definition: ARMCore.h:248
flushBranchPrediction
void flushBranchPrediction()
Flush branch prediction.
Definition: ARMCore.h:210
CPUState::r10
u32 r10
Definition: ARMCore.h:248
dmb
void dmb()
Data Memory Barrier.
Definition: ARMCore.h:180