1 | /*
|
---|
2 | * TOPPERS/ASP Kernel
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
4 | * Advanced Standard Profile Kernel
|
---|
5 | *
|
---|
6 | * Copyright (C) 2007-2016 by Embedded and Real-Time Systems Laboratory
|
---|
7 | * Graduate School of Information Science, Nagoya Univ., JAPAN
|
---|
8 | *
|
---|
9 | * ä¸è¨èä½æ¨©è
|
---|
10 | ã¯ï¼ä»¥ä¸ã®(1)ï½(4)ã®æ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§
|
---|
11 | * ã¢ï¼æ¬ã½ããã¦ã§ã¢ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹
|
---|
12 | * å¤ã»åé
|
---|
13 | å¸ï¼ä»¥ä¸ï¼å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
14 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
15 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
16 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
17 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
18 | * ç¨ã§ããå½¢ã§åé
|
---|
19 | å¸ããå ´åã«ã¯ï¼åé
|
---|
20 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
21 | * è
|
---|
22 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®èä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
23 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
24 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
25 | * ç¨ã§ããªãå½¢ã§åé
|
---|
26 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
27 | * ã¨ï¼
|
---|
28 | * (a) åé
|
---|
29 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
30 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
31 | * ä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
32 | * (b) åé
|
---|
33 | å¸ã®å½¢æ
|
---|
34 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
35 | * å ±åãããã¨ï¼
|
---|
36 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
37 | * 害ãããï¼ä¸è¨èä½æ¨©è
|
---|
38 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
39 | 責ãããã¨ï¼
|
---|
40 | * ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®ã¦ã¼ã¶ã¾ãã¯ã¨ã³ãã¦ã¼ã¶ããã®ãããªãç
|
---|
41 | * ç±ã«åºã¥ãè«æ±ãããï¼ä¸è¨èä½æ¨©è
|
---|
42 | ããã³TOPPERSããã¸ã§ã¯ãã
|
---|
43 | * å
|
---|
44 | 責ãããã¨ï¼
|
---|
45 | *
|
---|
46 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨èä½æ¨©è
|
---|
47 | ã
|
---|
48 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ç¹å®ã®ä½¿ç¨ç®ç
|
---|
49 | * ã«å¯¾ããé©åæ§ãå«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§
|
---|
50 | * ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ害ã«é¢ãã¦ãï¼ã
|
---|
51 | * ã®è²¬ä»»ãè² ããªãï¼
|
---|
52 | *
|
---|
53 | * $Id$
|
---|
54 | */
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * ã«ã¼ãã«ã®ã¿ã¼ã²ããä¾åé¨ï¼GR-PEACHç¨ï¼
|
---|
58 | */
|
---|
59 |
|
---|
60 | #include <kernel.h>
|
---|
61 | #include <sil.h>
|
---|
62 | #include "us_ticker_api.h"
|
---|
63 | #include <sys/types.h>
|
---|
64 | #include <errno.h>
|
---|
65 | #include "kernel_cfg.h"
|
---|
66 | #include "t_syslog.h"
|
---|
67 |
|
---|
68 | extern void musl_start(const char *const args);
|
---|
69 |
|
---|
70 | int main()
|
---|
71 | {
|
---|
72 | static const char *const args[] = {
|
---|
73 | (char *)1,
|
---|
74 | "asp3",
|
---|
75 | 0,
|
---|
76 | // envp
|
---|
77 | "TZ=JST-9",
|
---|
78 | 0,
|
---|
79 | // auxv
|
---|
80 | 0
|
---|
81 | };
|
---|
82 | musl_start(args);
|
---|
83 | return 0;
|
---|
84 | }
|
---|
85 |
|
---|
86 | void wait_us(int us);
|
---|
87 |
|
---|
88 | void wait(float s)
|
---|
89 | {
|
---|
90 | wait_us(s * 1000000.0f);
|
---|
91 | }
|
---|
92 |
|
---|
93 | void wait_ms(int ms)
|
---|
94 | {
|
---|
95 | wait_us(ms * 1000);
|
---|
96 | }
|
---|
97 |
|
---|
98 | void wait_us(int us)
|
---|
99 | {
|
---|
100 | ER ercd;
|
---|
101 |
|
---|
102 | ercd = dly_tsk(us);
|
---|
103 | if (ercd == E_CTX){
|
---|
104 | uint32_t start = us_ticker_read();
|
---|
105 | while ((us_ticker_read() - start) < (uint32_t)us);
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | int32_t IRQ_Initialize (void)
|
---|
110 | {
|
---|
111 | GIC_Enable();
|
---|
112 | return (0);
|
---|
113 | }
|
---|
114 |
|
---|
115 | __attribute__((weak))
|
---|
116 | long SYS_poll()
|
---|
117 | {
|
---|
118 | return -ENOSYS;
|
---|
119 | }
|
---|
120 |
|
---|
121 | __attribute__((weak))
|
---|
122 | long SYS_open()
|
---|
123 | {
|
---|
124 | return -ENOSYS;
|
---|
125 | }
|
---|
126 |
|
---|
127 | void *tid_address;
|
---|
128 |
|
---|
129 | __attribute__((weak))
|
---|
130 | long SYS_set_tid_address(long a) {
|
---|
131 | tid_address = (void *)a;
|
---|
132 | return 0;
|
---|
133 | }
|
---|
134 |
|
---|
135 | __attribute__((weak))
|
---|
136 | long ARM_SYS_set_tls(long a)
|
---|
137 | {
|
---|
138 | __asm__ __volatile__("mcr p15,0,%0,c13,c0,3" :: "r"(a));
|
---|
139 | return 0;
|
---|
140 | }
|
---|
141 |
|
---|
142 | __attribute__((weak))
|
---|
143 | long SYS_exit_group()
|
---|
144 | {
|
---|
145 | ext_ker();
|
---|
146 | return 0;
|
---|
147 | }
|
---|
148 |
|
---|
149 | __attribute__((weak))
|
---|
150 | long SYS_exit()
|
---|
151 | {
|
---|
152 | ext_ker();
|
---|
153 | return 0;
|
---|
154 | }
|
---|
155 |
|
---|
156 | __attribute__((weak))
|
---|
157 | long SYS_futex(long a, long b, long c, long d, long e, long f) {
|
---|
158 | //int futex(int *uaddr, int op, int val, const struct timespec *timeout, int *uaddr2, int val3);
|
---|
159 | return -ENOSYS;
|
---|
160 | }
|
---|
161 |
|
---|
162 | #ifndef _MSC_VER
|
---|
163 | extern uint32_t __HeapBase;
|
---|
164 | extern uint32_t __HeapLimit;
|
---|
165 | extern uint32_t __end__;
|
---|
166 | #else
|
---|
167 | uint8_t __HeapBase[14 * 4096];
|
---|
168 | #define __HeapLimit __HeapBase[sizeof(__HeapBase)]
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | void *_sbrk(int incr)
|
---|
172 | {
|
---|
173 | static unsigned char *heap = (unsigned char *)&__end__;
|
---|
174 | unsigned char *prev_heap = heap;
|
---|
175 | unsigned char *new_heap = heap + incr;
|
---|
176 |
|
---|
177 | if (new_heap >= (unsigned char *)&__HeapLimit) {
|
---|
178 | errno = ENOMEM;
|
---|
179 | return (void *) -1;
|
---|
180 | }
|
---|
181 |
|
---|
182 | heap = new_heap;
|
---|
183 | return (void *) prev_heap;
|
---|
184 | }
|
---|
185 |
|
---|
186 | void *shell_brk(void *addr)
|
---|
187 | {
|
---|
188 | if (addr == 0) {
|
---|
189 | return (void *)(&__HeapBase);
|
---|
190 | }
|
---|
191 | if ((addr >= (void *)&__HeapBase) && (addr < (void *)&__HeapLimit)) {
|
---|
192 | return addr;
|
---|
193 | }
|
---|
194 | return (void *)-1;
|
---|
195 | }
|
---|
196 |
|
---|
197 | void *shell_mmap2(void *start, size_t length, int prot, int flags, int fd, off_t pgoffset)
|
---|
198 | {
|
---|
199 | if (fd != -1)
|
---|
200 | return (void *)-EINVAL;
|
---|
201 |
|
---|
202 | if ((length >= 0) && (length <= sizeof(&__HeapBase))) {
|
---|
203 | return &__HeapBase;
|
---|
204 | }
|
---|
205 | return (void *)-1;
|
---|
206 | }
|
---|
207 |
|
---|
208 | int shell_mprotect(void *addr, size_t len, int prot)
|
---|
209 | {
|
---|
210 | //if ((addr >= (void *)&__HeapBase) && (addr + len < (void *)&__HeapLimit)) {
|
---|
211 | return 0;
|
---|
212 | //}
|
---|
213 | //return -1;
|
---|
214 | }
|
---|
215 |
|
---|
216 | __attribute__((weak))
|
---|
217 | long SYS_brk(long a) {
|
---|
218 | return (long)shell_brk((void *)a);
|
---|
219 | }
|
---|
220 |
|
---|
221 | __attribute__((weak))
|
---|
222 | long SYS_mmap2(long a, long b, long c, long d, long e, long f, long g) {
|
---|
223 | return (int)shell_mmap2((void *)a, (size_t)b, (int)c, (int)d, (int)e, ((off_t)f << 32) | (off_t)g);
|
---|
224 | }
|
---|
225 |
|
---|
226 | __attribute__((weak))
|
---|
227 | long SYS_mprotect(long a, long b, long c) {
|
---|
228 | return shell_mprotect((void *)a, (size_t)b, (int)c);
|
---|
229 | }
|
---|
230 |
|
---|
231 | __attribute__((weak))
|
---|
232 | long SYS_madvise()
|
---|
233 | {
|
---|
234 | return -ENOSYS;
|
---|
235 | }
|
---|
236 |
|
---|
237 | __attribute__((weak))
|
---|
238 | long SYS_mremap()
|
---|
239 | {
|
---|
240 | return -ENOSYS;
|
---|
241 | }
|
---|
242 |
|
---|
243 | __attribute__((weak))
|
---|
244 | long SYS_munmap()
|
---|
245 | {
|
---|
246 | return -ENOSYS;
|
---|
247 | }
|
---|
248 |
|
---|
249 | int malloc_lock_sem_count[TNUM_TSKID];
|
---|
250 |
|
---|
251 | __attribute__((weak))
|
---|
252 | void __malloc_lock(struct _reent *_r)
|
---|
253 | {
|
---|
254 | ER ercd;
|
---|
255 | ID tskid = 0;
|
---|
256 |
|
---|
257 | ercd = get_tid(&tskid);
|
---|
258 | if (ercd != E_OK) {
|
---|
259 | goto error;
|
---|
260 | }
|
---|
261 |
|
---|
262 | if (malloc_lock_sem_count[tskid - 1] == 0) {
|
---|
263 | ercd = wai_sem(SEM_MALLOC);
|
---|
264 | if (ercd != E_OK) {
|
---|
265 | goto error;
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | malloc_lock_sem_count[tskid - 1]++;
|
---|
270 | return;
|
---|
271 | error:
|
---|
272 | syslog(LOG_ERROR, "%s (%d) __malloc_lock error.",
|
---|
273 | itron_strerror(ercd), SERCD(ercd));
|
---|
274 | asm("bkpt #0");
|
---|
275 | while(0);
|
---|
276 | }
|
---|
277 |
|
---|
278 | __attribute__((weak))
|
---|
279 | void __malloc_unlock(struct _reent *_r)
|
---|
280 | {
|
---|
281 | ER ercd;
|
---|
282 | ID tskid = 0;
|
---|
283 | int count;
|
---|
284 |
|
---|
285 | ercd = get_tid(&tskid);
|
---|
286 | if (ercd != E_OK) {
|
---|
287 | goto error;
|
---|
288 | }
|
---|
289 |
|
---|
290 | malloc_lock_sem_count[tskid - 1]--;
|
---|
291 | if (malloc_lock_sem_count[tskid - 1] > 0)
|
---|
292 | return;
|
---|
293 |
|
---|
294 | if (malloc_lock_sem_count[tskid - 1] < 0) {
|
---|
295 | goto error;
|
---|
296 | }
|
---|
297 |
|
---|
298 | ercd = sig_sem(SEM_MALLOC);
|
---|
299 | if (ercd != E_OK) {
|
---|
300 | goto error;
|
---|
301 | }
|
---|
302 | return;
|
---|
303 | error:
|
---|
304 | syslog(LOG_ERROR, "%s (%d) __malloc_unlock error.",
|
---|
305 | itron_strerror(ercd), SERCD(ercd));
|
---|
306 | asm("bkpt #0");
|
---|
307 | while(0);
|
---|
308 | }
|
---|