nckernel
0.1
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Groups
Pages
common
src
main.c
Go to the documentation of this file.
1
#include <
sys/types.h
>
2
#include <
sys/syscall.h
>
3
#include <
stdio.h
>
4
#include <
time.h
>
5
#include <
stddef.h
>
6
#include <
stdint.h
>
7
#include <
unistd.h
>
8
#include <
stdlib.h
>
9
#include <
pthread.h
>
10
#include <
errno.h
>
11
#include <
semaphore.h
>
12
#include <
fcntl.h
>
13
14
#include <
interrupt.h
>
15
#include <
isr.h
>
16
17
#include <
debug.h
>
18
19
#include <
list.h
>
20
#include <
zone.h
>
21
#include <
page_frame.h
>
22
#include <
onetime_map.h
>
23
#include <
string.h
>
24
25
extern
unsigned
long
end
;
26
extern
unsigned
long
etext
;
27
extern
unsigned
long
edata
;
28
29
30
#if defined(MUTEX_TEST)
31
static
pthread_mutex_t
lock;
32
#define LOCK_INIT() do { pthread_mutex_init(&lock, NULL); } while (0)
33
#define LOCK() do { pthread_mutex_lock(&lock); } while (0)
34
#define UNLOCK() do { pthread_mutex_unlock(&lock); } while (0)
35
#elif defined(SEM_TEST)
36
static
sem_t
lock;
37
#define LOCK_INIT() do { sem_init(&lock, 0, 0); } while (0)
38
#define LOCK() do { sem_wait(&lock); } while (0)
39
#define UNLOCK() do { sem_post(&lock); } while (0)
40
#else
41
#define LOCK_INIT()
42
#define LOCK()
43
#define UNLOCK()
44
#endif
45
46
static
void
*t_func2(
void
*data)
47
{
48
int
id
;
49
int
ret = 0;
50
51
LOCK
();
52
printf
(
"Thread[%s]: %p, arg: %p\n"
, __func__,
pthread_self
(), data);
53
UNLOCK
();
54
for
(
id
= 0; ;
id
++) {
55
LOCK
();
56
printf
(
" T[%d] - ret: [%d]\r"
,
id
, ret);
57
UNLOCK
();
58
ret =
pthread_yield
();
59
}
60
61
return
(
void
*)
NULL
;
62
}
63
64
static
void
*t_func1(
void
*data)
65
{
66
int
id
;
67
68
LOCK
();
69
printf
(
"Thread[%s]: %p, arg: %p\n"
, __func__,
pthread_self
(), data);
70
UNLOCK
();
71
for
(
id
= 0;
id
< 100;
id
++) {
72
LOCK
();
73
printf
(
" A [%d]\r"
,
id
);
74
UNLOCK
();
75
pthread_yield
();
76
}
77
78
return
(
void
*)
NULL
;
79
}
80
81
static
inline
void
pf_test(
void
)
82
{
83
char
*test;
84
85
test = (
char
*)0xba481030;
86
*test =
'a'
;
87
88
printf
(
">>>>>>>>> %p [%c]\n"
, test, *test);
89
}
90
91
static
inline
int
sys_open(
const
char
*path, ...)
92
{
93
return
-
ENOSYS
;
94
}
95
96
static
inline
int
sys_close(
int
fd)
97
{
98
return
-
ENOSYS
;
99
}
100
101
static
inline
int
sys_read(
int
fd,
char
*
buffer
,
int
count)
102
{
103
return
-
ENOSYS
;
104
}
105
106
static
inline
int
sys_write(
int
fd,
const
char
*
buffer
,
int
count)
107
{
108
return
-
ENOSYS
;
109
}
110
111
static
inline
int
sys_lseek(
int
fd,
off_t
offset,
int
whence)
112
{
113
return
-
ENOSYS
;
114
}
115
120
static
inline
int
sys_opendir(
const
char
*path)
121
{
122
return
-
ENOSYS
;
123
}
124
129
static
inline
int
sys_closedir(
int
fd)
130
{
131
return
-
ENOSYS
;
132
}
133
138
static
inline
int
sys_readdir(
int
fd,
void
*ret)
139
{
140
return
-
ENOSYS
;
141
}
142
143
static
inline
int
sys_yield(
void
)
144
{
145
return
-
ENOSYS
;
146
}
147
148
static
int
sw_intr_cb(
int
sub_irq,
void
*_info,
void
*data)
149
{
150
static
int
call_count = 0;
151
struct
intr_info
*
info
= _info;
152
int
ret
;
153
154
call_count++;
155
156
printf
(
"sw_intr_cb [%d] argc: %d, argv: %p\r"
, call_count, info->
argc
, info->
argv
);
157
158
ret = -
EINVAL
;
159
switch
(sub_irq) {
160
case
SYS_NR_OPEN
:
161
if
(info->
argc
!= 3) {
162
break
;
163
}
164
ret = sys_open((
const
char
*)info->
argv
[0], info->
argv
[1], info->
argv
[2]);
165
break
;
166
case
SYS_NR_CLOSE
:
167
if
(info->
argc
!= 1) {
168
break
;
169
}
170
ret = sys_close((
int
)info->
argv
[0]);
171
break
;
172
case
SYS_NR_READ
:
173
if
(info->
argc
!= 3) {
174
break
;
175
}
176
ret = sys_read((
int
)info->
argv
[0], (
char
*)info->
argv
[1], (
int
)info->
argv
[2]);
177
break
;
178
case
SYS_NR_WRITE
:
179
if
(info->
argc
!= 3) {
180
break
;
181
}
182
ret = sys_write((
int
)info->
argv
[0], (
const
char
*)info->
argv
[1], (
int
)info->
argv
[2]);
183
break
;
184
case
SYS_NR_SEEK
:
185
if
(info->
argc
!= 3) {
186
break
;
187
}
188
ret = sys_lseek((
int
)info->
argv
[0], (
off_t
)info->
argv
[1], (
int
)info->
argv
[2]);
189
break
;
190
case
SYS_NR_OPENDIR
:
191
if
(info->
argc
!= 1) {
192
break
;
193
}
194
ret = sys_opendir((
const
char
*)info->
argv
[0]);
195
break
;
196
case
SYS_NR_CLOSEDIR
:
197
if
(info->
argc
!= 1) {
198
break
;
199
}
200
ret = sys_closedir((
int
)info->
argv
[0]);
201
break
;
202
case
SYS_NR_READDIR
:
203
if
(info->
argc
!= 2) {
204
break
;
205
}
206
ret = sys_readdir((
int
)info->
argv
[0], (
void
*)info->
argv
[1]);
207
break
;
208
case
SYS_NR_YIELD
:
209
if
(info->
argc
) {
210
break
;
211
}
212
ret = sys_yield();
213
break
;
214
default
:
215
ret = -
ENOSYS
;
216
break
;
217
}
218
219
return
ret
;
220
}
221
222
static
inline
void
buddy_test(
void
)
223
{
224
unsigned
long
*page[5];
225
register
int
i;
226
227
printf
(
"[%s:%d]\n"
, __func__, __LINE__);
228
229
for
(i = 0; i < (
sizeof
(page) /
sizeof
(*page)); i++) {
230
page[i] =
page_frame_alloc
(
ZONE_NORMAL
, 1);
231
printf
(
"Page[%d] : %p\n"
, i, page[i]);
232
}
233
234
for
(i = 0; i < (
sizeof
(page) /
sizeof
(*page)); i++) {
235
page_frame_free
(page[i]);
236
}
237
238
for
(i = 0; i < (
sizeof
(page) /
sizeof
(*page)); i++) {
239
page[i] =
page_frame_alloc
(
ZONE_NORMAL
, 1);
240
printf
(
"Page[%d] : %p\n"
, i, page[i]);
241
}
242
243
for
(i = 0; i < (
sizeof
(page) /
sizeof
(*page)); i++) {
244
page_frame_free
(page[i]);
245
}
246
}
247
248
static
inline
void
thread_test(
void
)
249
{
250
pthread_t
thid;
251
252
printf
(
"[%s:%d]\n"
, __func__, __LINE__);
253
254
LOCK_INIT
();
255
pthread_create
(&thid,
NULL
, t_func1, (
void
*)1);
256
pthread_create
(&thid,
NULL
, t_func2, thid);
257
}
258
259
static
void
*elf_func(
void
*data)
260
{
261
char
*tmp;
262
char
*
argv
[] = {
263
"/fat/hello-world"
,
264
NULL
,
265
};
266
267
tmp =
page_frame_alloc
(
ZONE_NORMAL
, 1);
268
if
(tmp) {
269
tmp =
onetime_map
(tmp);
270
memset
(tmp, 0,
sysconf
(
_SC_PAGESIZE
));
271
tmp =
onetime_unmap
(tmp);
272
page_frame_free
(tmp);
273
printf
(
"Onetime mapper testing is done\n"
);
274
}
275
276
//printf("Executing %s\n", argv[0]);
277
execv
(argv[0], argv);
278
279
while
(1) {
280
}
281
return
NULL
;
282
}
283
284
static
inline
void
elf_test(
void
)
285
{
286
pthread_t
thid;
287
printf
(
"Create a new thread for loading a ELF\n"
);
288
pthread_create
(&thid,
NULL
, elf_func,
NULL
);
289
}
290
291
static
inline
void
file_test(
void
)
292
{
293
int
fd;
294
char
ch[
BUFSIZ
];
295
int
ret
;
296
register
int
i;
297
char
check;
298
int
validate_idx;
299
300
printf
(
"Open a file hello-world.txt\n"
);
301
fd =
open
(
"/fat/hello-world.txt"
,
O_RDONLY
);
302
if
(fd < 0) {
303
printf
(
"Failed to open a text pattern file\n"
);
304
return
;
305
}
306
307
check =
'0'
;
308
validate_idx = 0;
309
while
((ret =
read
(fd, ch,
sizeof
(ch))) > 0) {
310
for
(i = 0; i < ret && ch[i] == (validate_idx + check); i++);
311
if
(i != ret) {
312
printf
(
"Mismatched %c[%d] / %d/%d\n"
,
313
ch[i], validate_idx, i, ret);
314
break
;
315
}
316
printf
(
"%d Matched %d pattern [%d]\n"
,
317
lseek
(fd, 0L,
SEEK_CUR
), i, validate_idx);
318
validate_idx++;
319
}
320
printf
(
"End of verifying read:ret = %d [%d]\n"
, ret, validate_idx);
321
322
close
(fd);
323
}
324
325
int
main
(
int
argc
,
char
*argv[],
char
*envp[])
326
{
327
register
int
i;
328
int
ret
;
329
330
for
(i = 0; envp[i]; i++) {
331
printf
(
">> ENV[%d] %s\n"
, i, envp[i]);
332
}
333
334
printf
(
"[%s:%d] =====> \n"
, __func__, __LINE__);
335
pf_test();
336
printf
(
"[%s:%d] =====> \n"
, __func__, __LINE__);
337
338
ret =
register_irq
(
IRQ_NR_SW
,
NORMAL_PRIORITY
, sw_intr_cb,
NULL
);
339
if
(ret < 0) {
340
printf
(
"Failed to register irq handler for 0x%x\n"
,
IRQ_NR_SW
);
341
}
342
343
file_test();
344
elf_test();
345
buddy_test();
346
thread_test();
347
348
for
(i = 0; ; i++) {
349
LOCK
();
350
printf
(
"M [%d]\r"
, i);
351
UNLOCK
();
352
ret =
pthread_yield
();
353
}
354
return
0;
355
}
356
357
/* End of a file */
Generated on Thu Nov 7 2013 02:45:25 for nckernel by
1.8.4