GadgetSeed  0.9.6
syscalls.c
[詳解]
1 /** @file
2  @brief newlib システムコール
3 
4  @date 2017.04.29
5  @author Takashi SHUDO
6 */
7 
8 #include "tkprintf.h"
9 #include "console.h"
10 
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdbool.h>
14 #include <reent.h>
15 #include <errno.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 
19 //#define DEBUGKBITS 0x01
20 #include "dkprintf.h"
21 
22 
23 _ssize_t _read_r(struct _reent *r, int file, void *ptr, size_t len)
24 {
25  return 0;
26 }
27 
28 _ssize_t _write_r(struct _reent *r, int file, const void *ptr, size_t len)
29 {
30  int i;
31  const unsigned char *p;
32 
33  p = (const unsigned char *)ptr;
34 
35  for(i = 0; i < len; i++) {
36  if(*p == '\n') {
37  cputc('\r');
38  }
39  cputc(*p++);
40  }
41 
42  return len;
43 }
44 
45 void _fini(void)
46 {
47  // [TODO]
48 }
49 
50 int _close_r(struct _reent *r, int file)
51 {
52  // [TODO]
53  return 0;
54 }
55 
56 _off_t _lseek_r(struct _reent *r, int file, _off_t ptr, int dir)
57 {
58  // [TODO]
59  return (_off_t)0;
60 }
61 
62 int _fstat_r(struct _reent *r, int file, struct stat *st)
63 {
64  // [TODO]
65  st->st_mode = S_IFCHR;
66 
67  return 0;
68 }
69 
70 
71 extern char end[]; // .bssの最終アドレス、ヒープの先頭になる
72 extern char _heap_end[]; // ヒープの最終アドレス
73 static char *heap_ptr = NULL; // 現在のヒープポインタ
74 
75 void * _sbrk_r(struct _reent *_s_r, ptrdiff_t nbytes)
76 {
77  char *base;
78 
79  // MUTEX Lock [TODO]
80  DKPRINTF(0x01, "_sbrk_r reqest bytes %d\n", nbytes);
81 
82  if(heap_ptr == 0) {
83  heap_ptr = end;
84  }
85 
86  base = heap_ptr;
87 
88  if((heap_ptr + nbytes) > _heap_end) {
89  errno = ENOMEM;
90  SYSERR_PRINT("No Left Memory\n");
91  SYSERR_PRINT("Request %8d, Left %p-%p(%d)\n", nbytes, base, _heap_end,
92  (unsigned int)_heap_end - (unsigned int)base);
93  return (caddr_t) -1;
94  }
95 
96  heap_ptr += nbytes;
97 
98  DKPRINTF(0x01, "_sbrk_r %8d %p-%p(%d)\n", nbytes, base, _heap_end,
99  (unsigned int)_heap_end - (unsigned int)base);
100 
101  // MUTEX UnLock [TODO]
102 
103  return base;
104 }
105 
106 unsigned int system_heap_size(void)
107 {
108  return (unsigned int)_heap_end - (unsigned int)heap_ptr;
109 }
110 
111 unsigned int system_heap_total_size(void)
112 {
113  tkprintf("Heap area : %p - %p (%d)\n", end, _heap_end,
114  (unsigned int)_heap_end - (unsigned int)end);
115 
116  return (unsigned int)_heap_end - (unsigned int)end;
117 }
118 
119 int isatty(int file)
120 {
121  // [TODO]
122  return 1;
123 }
124 
125 int _getpid(int file)
126 {
127  // [TODO]
128  return 1;
129 }
130 
131 int _isatty(int fd)
132 {
133  return 1;
134 }
135 
136 #if 0
137 void * _sbrk(ptrdiff_t incr)
138 {
139  char *base;
140 
141  if(heap_ptr == 0) {
142  heap_ptr = end;
143  }
144 
145  base = heap_ptr;
146 
147  if((heap_ptr + incr) > _heap_end) {
148  errno = ENOMEM;
149  return (caddr_t) -1;
150  }
151 
152  heap_ptr += incr;
153 
154  return base;
155 }
156 
157 int _open(const char *path, int flags, ...)
158 {
159  // [TODO]
160  return 1;
161 }
162 
163 int _close(int fd)
164 {
165  // [TODO]
166  return 0;
167 }
168 
169 int _fstat(int fd, struct stat *st)
170 {
171  st->st_mode = S_IFCHR;
172  return 0;
173 }
174 
175 int _lseek(int fd, off_t pos, int whence)
176 {
177  return 0;
178 }
179 
180 int _read(int fd, char *buf, size_t cnt)
181 {
182  // [TODO]
183  return 0;
184 }
185 
186 int _write(int fd, const char *buf, size_t cnt)
187 {
188  int i;
189 
190  for (i = 0; i < cnt; i++)
191  cputc(buf[i]);
192 
193  return cnt;
194 }
195 #endif
196 
197 /*
198  *
199  */
200 
201 int _times(struct tms *buf)
202 {
203  // [TODO]
204  return -1;
205 }
206 
207 int _init(struct tms *buf)
208 {
209  // [TODO]
210  return -1;
211 }
212 
213 int _link(char *old, char *new)
214 {
215  errno = EMLINK;
216  return -1;
217 }
218 
219 int _unlink(char *name)
220 {
221  errno = ENOENT;
222  return -1;
223 }
224 
225 int _wait(int *status)
226 {
227  errno = ECHILD;
228  return -1;
229 }
230 
231 int _execve(char *name, char **argv, char **env)
232 {
233  errno = ENOMEM;
234  return -1;
235 }
236 
237 int _fork(void)
238 {
239  errno = EAGAIN;
240  return -1;
241 }
242 
243 void _exit(int n)
244 {
245  // [TODO]
246  while(1);
247 }
248 
249 int _kill(int pid, int sig)
250 {
251  // [TODO]
252  errno = EINVAL;
253  return -1;
254 }
int tkprintf(const char *fmt,...)
非タスクコンテキスト実行用メッセージ出力
Definition: tkprintf.c:100
カーネル用機能限定printf
int cputc(unsigned char td)
標準出力より1文字を出力する
Definition: console.c:163
unsigned int system_heap_size(void)
< $gsc ヒープメモリ管理をnewlibで行う
Definition: syscalls.c:106
コンソールI/O
カーネル、ドライバ(非タスク)デバッグ用マクロ