-
Notifications
You must be signed in to change notification settings - Fork 32
/
cpu.h
401 lines (272 loc) · 5.59 KB
/
cpu.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* Phoenix-RTOS
*
* Operating system kernel
*
* CPU related routines
*
* Copyright 2014, 2017 Phoenix Systems
* Author: Jacek Popko, Pawel Pisarczyk, Aleksander Kaminski
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/
#ifndef _HAL_CPU_H_
#define _HAL_CPU_H_
#if defined(CPU_STM32L152XD) || defined(CPU_STM32L152XE) || defined(CPU_STM32L4X6)
#define CPU_STM32
#endif
#if defined(CPU_IMXRT105X) || defined(CPU_IMXRT106X) || defined(CPU_IMXRT117X)
#define CPU_IMXRT
#endif
#define NULL 0
#define SIZE_PAGE 0x200
#ifndef SIZE_USTACK
#define SIZE_USTACK (3 * SIZE_PAGE)
#endif
#ifndef SIZE_KSTACK
#define SIZE_KSTACK (2 * 512)
#endif
#ifdef CPU_IMXRT
#define RET_HANDLER_MSP 0xffffffe1
#define RET_THREAD_MSP 0xffffffe9
#define RET_THREAD_PSP 0xffffffed
#define HWCTXSIZE (8 + 18)
#define USERCONTROL 0x7
#else
#define RET_HANDLER_MSP 0xfffffff1
#define RET_THREAD_MSP 0xfffffff9
#define RET_THREAD_PSP 0xfffffffd
#define HWCTXSIZE 8
#define USERCONTROL 0x3
#endif
#ifndef __ASSEMBLY__
#define SYSTICK_INTERVAL 1000
#define PUTONSTACK(kstack, t, v) \
do { \
(kstack) -= (sizeof(t) + 3) & ~0x3; \
*((t *)kstack) = (v); \
} while (0)
#define GETFROMSTACK(ustack, t, v, n) \
do { \
ustack = (void *)(((ptr_t)ustack + sizeof(t) - 1) & ~(sizeof(t) - 1)); \
(v) = *(t *)ustack; \
ustack += (sizeof(t) + 3) & ~0x3; \
} while (0)
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef signed char s8;
typedef short s16;
typedef int s32;
typedef long long s64;
typedef u32 addr_t;
typedef u32 cycles_t;
typedef s64 offs_t;
typedef unsigned int size_t;
typedef unsigned long long time_t;
typedef u32 ptr_t;
/* Object identifier - contains server port and object id */
typedef u32 id_t;
typedef struct _oid_t {
u32 port;
id_t id;
} oid_t;
typedef struct _cpu_context_t {
u32 savesp;
u32 fpuctx;
/* Saved by ISR */
u32 psp;
u32 r4;
u32 r5;
u32 r6;
u32 r7;
u32 r8;
u32 r9;
u32 r10;
u32 r11;
u32 irq_ret;
#ifdef CPU_IMXRT
u32 s16;
u32 s17;
u32 s18;
u32 s19;
u32 s20;
u32 s21;
u32 s22;
u32 s23;
u32 s24;
u32 s25;
u32 s26;
u32 s27;
u32 s28;
u32 s29;
u32 s30;
u32 s31;
#endif
/* Saved by hardware */
u32 r0;
u32 r1;
u32 r2;
u32 r3;
u32 r12;
u32 lr;
u32 pc;
u32 psr;
#ifdef CPU_IMXRT
u32 s0;
u32 s1;
u32 s2;
u32 s3;
u32 s4;
u32 s5;
u32 s6;
u32 s7;
u32 s8;
u32 s9;
u32 s10;
u32 s11;
u32 s12;
u32 s13;
u32 s14;
u32 s15;
u32 fpscr;
u32 pad1;
#endif
} cpu_context_t;
extern volatile cpu_context_t *_cpu_nctx;
/* interrupts */
static inline void hal_cpuDisableInterrupts(void)
{
__asm__ volatile ("cpsid if");
}
static inline void hal_cpuEnableInterrupts(void)
{
__asm__ volatile ("cpsie if");
}
/* performance */
extern time_t hal_cpuLowPower(time_t ms);
extern void hal_cpuSetDevBusy(int s);
static inline void hal_cpuHalt(void)
{
__asm__ volatile ("\
wfi; \
nop; ");
}
extern void hal_cpuGetCycles(cycles_t *cb);
/* bit operations */
static inline unsigned int hal_cpuGetLastBit(const u32 v)
{
int pos;
__asm__ volatile ("clz %0, %1" : "=r" (pos) : "r" (v));
return 31 - pos;
}
static inline unsigned int hal_cpuGetFirstBit(const u32 v)
{
unsigned pos;
__asm__ volatile ("\
rbit %0, %1; \
clz %0, %0;" : "=r" (pos) : "r" (v));
return (pos);
}
/* context management */
static inline void hal_cpuSetCtxGot(cpu_context_t *ctx, void *got)
{
ctx->r9 = (u32)got;
}
static inline void hal_cpuSetGot(void *got)
{
__asm__ volatile ("mov r9, %0" :: "r" (got));
}
static inline void *hal_cpuGetGot(void)
{
void *got;
__asm__ volatile ("mov %0, r9" : "=r" (got));
return got;
}
extern int hal_cpuCreateContext(cpu_context_t **nctx, void *start, void *kstack, size_t kstacksz, void *ustack, void *arg);
struct _spinlock_t;
extern int hal_cpuReschedule(struct _spinlock_t *spinlock, u32 *scp);
static inline void hal_cpuRestore(cpu_context_t *curr, cpu_context_t *next)
{
curr->savesp = (u32)next;
}
static inline void hal_cpuSetReturnValue(cpu_context_t *ctx, int retval)
{
ctx->r0 = retval;
}
static inline int hal_cpuSupervisorMode(cpu_context_t *ctx)
{
return 0;
}
static inline u32 hal_cpuGetPC(void)
{
void *pc;
__asm__ volatile ("mov %0, pc" : "=r" (pc));
return (u32)pc;
}
static inline void hal_cpuDataBarrier(void)
{
__asm__ volatile ("dmb");
}
static inline void hal_cpuDataSyncBarrier(void)
{
__asm__ volatile ("dsb");
}
static inline void hal_cpuInstrBarrier(void)
{
__asm__ volatile ("isb");
}
static inline void _hal_cpuSetKernelStack(void *kstack)
{
}
static inline void *hal_cpuGetSP(cpu_context_t *ctx)
{
return (void *)ctx;
}
static inline void *hal_cpuGetUserSP(cpu_context_t *ctx)
{
return (void *)ctx->psp;
}
static inline int hal_cpuPushSignal(cpu_context_t *ctx, void (*handler)(void), int sig)
{
return 0;
}
static inline void hal_longjmp(cpu_context_t *ctx)
{
__asm__ volatile
(" \
cpsid if; \
str %1, [%0]; \
bl _hal_invokePendSV; \
cpsie if; \
1: b 1b"
:
: "r" (&_cpu_nctx), "r" (ctx)
: "memory");
}
void hal_jmp(void *f, void *kstack, void *stack, int argc);
static inline void hal_cpuGuard(cpu_context_t *ctx, void *addr)
{
}
/* core management */
static inline unsigned int hal_cpuGetID(void)
{
return 0;
}
static inline unsigned int hal_cpuGetCount(void)
{
return 1;
}
extern void hal_cpuRestart(void);
extern char *hal_cpuInfo(char *info);
extern char *hal_cpuFeatures(char *features, unsigned int len);
extern void hal_wdgReload(void);
extern void _hal_cpuInit(void);
static inline void cpu_sendIPI(unsigned int cpu, unsigned int intr)
{
}
#endif
#endif