2 ** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
8 #include <sys/syscalls.h>
10 #include <sys/resource.h>
11 #include <newos/errors.h>
12 #include <newos/drivers.h>
14 static int pipe_test(void);
16 static int pipe_test(void);
18 static void port_test(void);
19 static int port_test_thread_func(void* arg
);
21 static int test_thread(void *args
)
27 printf("%c", 'a' + i
);
32 static int dummy_thread(void *args
)
37 static int cpu_eater_thread(void *args
)
43 static int fpu_cruncher_thread(void *args
)
45 double y
= *(double *)args
;
51 if(y
!= *(double *)args
)
52 printf("error: y %f\n", y
);
56 int main(int argc
, char **argv
)
65 for(cnt
= 0; cnt
< argc
; cnt
++) {
66 printf("arg %d = %s \n",cnt
,argv
[cnt
]);
69 printf("my thread id is %d\n", sys_get_current_thread_id());
71 printf("enter something: ");
87 printf("waiting 5 seconds\n");
91 fd
= sys_open("/dev/net/rtl8139/0", "", STREAM_TYPE_DEVICE
);
96 0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0x0F, 0x00, 0x0F, 0x00, 0x0F,
97 0x00, 0x08, 0x00, 0x45, 0x00, 0x00, 0x28, 0xF9, 0x55, 0x40, 0x00,
98 0x40, 0x06, 0xC0, 0x02, 0xC0, 0xA8, 0x00, 0x01, 0xC0, 0xA8, 0x00,
99 0x26, 0x00, 0x50, 0x0B, 0x5C, 0x81, 0xD6, 0xFA, 0x48, 0xBB, 0x17,
100 0x03, 0xC9, 0x50, 0x10, 0x7B, 0xB0, 0x6C, 0x00, 0x00, 0x00, 0x00,
101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
104 sys_write(fd
, buf
, 0, &len
);
109 fd
= sys_open("/dev/net/rtl8139/0", "", STREAM_TYPE_DEVICE
);
117 sys_read(fd
, buf
, 0, &len
);
118 printf("%d read %d bytes\n", foo
++, len
);
123 // XXX dangerous! This overwrites the beginning of your hard drive
129 printf("opening /dev/bus/ide/0/0/raw\n");
130 fd
= sys_open("/dev/bus/ide/0/0/raw", "", STREAM_TYPE_DEVICE
);
131 printf("fd = %d\n", fd
);
134 rc
= sys_read(fd
, buf
, 0, &bytes_read
);
135 printf("rc = %d, bytes_read = %d\n", rc
, bytes_read
);
144 rc
= sys_write(fd
, buf
, 1024, &bytes_read
);
145 printf("rc = %d, bytes_read = %d\n", rc
, bytes_read
);
155 for(i
=0; i
<10; i
++) {
156 tids
[i
] = sys_thread_create_thread("foo", &test_thread
, (void *)i
);
157 sys_thread_resume_thread(tids
[i
]);
161 sys_proc_kill_proc(sys_get_current_proc_id());
164 for(i=0; i<10; i++) {
165 sys_thread_kill_thread(tids[i]);
167 printf("thread_is dead\n");
175 sys_proc_create_proc("/boot/bin/true", "true", 32);
180 void *buf
= (void *)0x60000000;
185 fd
= sys_open("/boot/testapp", "", STREAM_TYPE_FILE
);
187 rc
= sys_read(fd
, buf
, 0, &len
);
188 printf("rc from read = 0x%x\n", rc
);
197 fd
= sys_open("/dev/audio/pcbeep/1", STREAM_TYPE_DEVICE
, 0);
199 printf("writing to the speaker\n");
201 sys_write(fd
, &data
, 0, 1);
204 sys_write(fd
, &data
, 0, 1);
220 fd
= sys_open("/dev/ps2mouse", STREAM_TYPE_DEVICE
, 0);
222 printf("failed to open device\n");
226 bytes_read
= sys_read(fd
, buf
, -1, 3);
228 printf("failed to read device\n");
232 printf("Status: %X\nDelta X: %d\nDelta Y: %d\n", buf
[0], buf
[1], buf
[2]);
241 fd
= sys_open("/dev/disk/netblock/0/raw", STREAM_TYPE_DEVICE
, 0);
243 printf("could not open netblock\n");
247 sys_ioctl(fd
, 90001, NULL
, 0);
249 for(i
=0; i
<1*1024*1024; i
+= 512) {
250 for(j
=0; j
<512/4; j
++)
252 sys_write(fd
, buf
, -1, sizeof(buf
));
255 // sys_read(fd, buf, 0, sizeof(buf));
256 // sys_write(fd, buf, 512, sizeof(buf));
266 rc
= getrlimit(RLIMIT_NOFILE
, &rl
);
268 printf("error in getrlimit\n");
272 printf("RLIMIT_NOFILE = %lu\n", rl
.rlim_cur
);
274 for(i
= 0; i
< NUM_FDS
; i
++) {
275 fds
[i
] = open("/boot/bin/testapp", 0);
281 printf("opened %d files\n", i
);
285 printf("Setting RLIMIT_NOFILE to %lu\n", rl
.rlim_cur
);
286 rc
= setrlimit(RLIMIT_NOFILE
, &rl
);
288 printf("error in setrlimit\n");
293 rc
= getrlimit(RLIMIT_NOFILE
, &rl
);
295 printf("error in getrlimit\n");
299 printf("RLIMIT_NOFILE = %lu\n", rl
.rlim_cur
);
301 for(;i
< NUM_FDS
; i
++) {
302 fds
[i
] = open("/boot/bin/testapp", 0);
308 printf("opened a total of %d files\n", i
);
316 rid
= sys_vm_map_file("netblock", &ptr
, REGION_ADDR_ANY_ADDRESS
, 16*1024, LOCK_RW
,
317 REGION_NO_PRIVATE_MAP
, "/dev/disk/netblock/0/raw", 0);
319 printf("error mmaping device\n");
323 // play around with it
324 printf("mmaped device at %p\n", ptr
);
325 printf("%d\n", *(int *)ptr
);
326 printf("%d\n", *((int *)ptr
+ 1));
327 printf("%d\n", *((int *)ptr
+ 2));
328 printf("%d\n", *((int *)ptr
+ 3));
335 // printf("spawning %d copies of true\n", 10000);
343 t
= sys_system_time();
345 id
= sys_proc_create_proc("/boot/bin/true", "true", NULL
, 0, 20);
347 printf("new proc returned 0x%x!\n", id
);
350 sys_proc_wait_on_proc(id
, NULL
);
352 printf("done (%Ld usecs)\n", sys_system_time() - t
);
360 printf("spawning two cpu eaters\n");
362 // sys_thread_resume_thread(sys_thread_create_thread("cpu eater 1", &cpu_eater_thread, 0));
363 // sys_thread_resume_thread(sys_thread_create_thread("cpu eater 2", &cpu_eater_thread, 0));
365 printf("spawning %d threads\n", 10000);
367 for(i
=0; i
<10000; i
++) {
373 t
= sys_system_time();
375 id
= sys_thread_create_thread("testthread", &dummy_thread
, 0);
376 sys_thread_resume_thread(id
);
377 sys_thread_wait_on_thread(id
, NULL
);
379 printf("done (%Ld usecs)\n", sys_system_time() - t
);
386 static double f
[5] = { 2.43, 5.23, 342.34, 234123.2, 1.4 };
388 printf("spawning a few floating point crunchers\n");
390 id
= sys_thread_create_thread("fpu thread0", &fpu_cruncher_thread
, &f
[0]);
391 sys_thread_resume_thread(id
);
393 id
= sys_thread_create_thread("fpu thread1", &fpu_cruncher_thread
, &f
[1]);
394 sys_thread_resume_thread(id
);
396 id
= sys_thread_create_thread("fpu thread2", &fpu_cruncher_thread
, &f
[2]);
397 sys_thread_resume_thread(id
);
399 id
= sys_thread_create_thread("fpu thread3", &fpu_cruncher_thread
, &f
[3]);
400 sys_thread_resume_thread(id
);
402 id
= sys_thread_create_thread("fpu thread4", &fpu_cruncher_thread
, &f
[4]);
403 sys_thread_resume_thread(id
);
406 printf("passed the test\n");
416 devfs_framebuffer_info fb
;
420 fd
= sys_open("/dev/graphics/fb/0", STREAM_TYPE_DEVICE
, 0);
422 printf("error opening framebuffer device\n");
426 err
= sys_ioctl(fd
, IOCTL_DEVFS_GET_FRAMEBUFFER_INFO
, &fb
, sizeof(fb
));
428 printf("error getting framebuffer info\n");
432 err
= sys_ioctl(fd
, IOCTL_DEVFS_MAP_FRAMEBUFFER
, &framebuffer
, sizeof(framebuffer
));
434 printf("error mapping framebuffer\n");
439 for(i
=0; i
<fb
.height
; i
++) {
440 uint16 row
[fb
.width
];
441 for(j
=0; j
<fb
.width
; j
++) {
442 uint16 color
= ((j
+i
+k
) & 0x1f) << 11;
445 memcpy(framebuffer
+ i
*fb
.width
*2, row
, sizeof(row
));
454 printf("mounting nfs filesystem\n");
456 sys_create("/nfs", STREAM_TYPE_DIR
);
457 err
= sys_mount("/nfs", "192.168.0.4:/disk", "nfs", NULL
);
458 printf("mount returns %d\n", err
);
465 fd
= sys_open("/nfs", STREAM_TYPE_DIR
, 0);
467 for(i
=0; i
<16; i
++) {
468 memset(buf
, 0, sizeof(buf
));
469 err
= sys_read(fd
, buf
, -1, sizeof(buf
));
470 printf("read returns %d '%s'\n", err
, buf
);
475 printf("exiting w/return code %d\n", rc
);
479 static int pipe_read_thread(void *args
)
481 int fd
= *(int *)args
;
487 err
= read(fd
, buf
, sizeof(buf
));
488 printf("pipe_read_thread: read returns %d\n", err
);
494 printf("%c", buf
[i
]);
501 static int pipe_test(void)
509 printf("pipe returns %d\n", err
);
510 printf("%d %d\n", fds
[0], fds
[1]);
513 id
= sys_thread_create_thread("pipe read thread", &pipe_read_thread
, &fds
[1]);
514 sys_thread_resume_thread(id
);
518 err
= write(fds
[0], "this is a test", sizeof("this is a test"));
519 printf("write returns %d\n", err
);
529 // close the reader end and write to it
531 err
= write(fds
[0], "this is a test", sizeof("this is a test"));
532 printf("write returns %d\n", err
);
543 port_id test_p1
, test_p2
, test_p3
, test_p4
;
545 static void port_test(void)
553 strcpy(testdata
, "abcd");
555 printf("porttest: port_create()\n");
556 test_p1
= sys_port_create(1, "test port #1");
557 test_p2
= sys_port_create(10, "test port #2");
558 test_p3
= sys_port_create(1024, "test port #3");
559 test_p4
= sys_port_create(1024, "test port #4");
561 printf("porttest: port_find()\n");
562 printf("'test port #1' has id %d (should be %d)\n", sys_port_find("test port #1"), test_p1
);
564 printf("porttest: port_write() on 1, 2 and 3\n");
565 sys_port_write(test_p1
, 1, &testdata
, sizeof(testdata
));
566 sys_port_write(test_p2
, 666, &testdata
, sizeof(testdata
));
567 sys_port_write(test_p3
, 999, &testdata
, sizeof(testdata
));
568 printf("porttest: port_count(test_p1) = %d\n", sys_port_count(test_p1
));
570 printf("porttest: port_write() on 1 with timeout of 1 sec (blocks 1 sec)\n");
571 sys_port_write_etc(test_p1
, 1, &testdata
, sizeof(testdata
), PORT_FLAG_TIMEOUT
, 1000000);
572 printf("porttest: port_write() on 2 with timeout of 1 sec (wont block)\n");
573 res
= sys_port_write_etc(test_p2
, 777, &testdata
, sizeof(testdata
), PORT_FLAG_TIMEOUT
, 1000000);
574 printf("porttest: res=%d, %s\n", res
, res
== 0 ? "ok" : "BAD");
576 printf("porttest: port_read() on empty port 4 with timeout of 1 sec (blocks 1 sec)\n");
577 res
= sys_port_read_etc(test_p4
, &dummy
, &dummy2
, sizeof(dummy2
), PORT_FLAG_TIMEOUT
, 1000000);
578 printf("porttest: res=%d, %s\n", res
, res
== ERR_PORT_TIMED_OUT
? "ok" : "BAD");
580 printf("porttest: spawning thread for port 1\n");
581 t
= sys_thread_create_thread("port_test", port_test_thread_func
, NULL
);
583 sys_thread_resume_thread(t
);
585 printf("porttest: write\n");
586 sys_port_write(test_p1
, 1, &testdata
, sizeof(testdata
));
588 // now we can write more (no blocking)
589 printf("porttest: write #2\n");
590 sys_port_write(test_p1
, 2, &testdata
, sizeof(testdata
));
591 printf("porttest: write #3\n");
592 sys_port_write(test_p1
, 3, &testdata
, sizeof(testdata
));
594 printf("porttest: waiting on spawned thread\n");
595 sys_thread_wait_on_thread(t
, NULL
);
597 printf("porttest: close p1\n");
598 sys_port_close(test_p2
);
599 printf("porttest: attempt write p1 after close\n");
600 res
= sys_port_write(test_p2
, 4, &testdata
, sizeof(testdata
));
601 printf("porttest: port_write ret %d\n", res
);
603 printf("porttest: testing delete p2\n");
604 sys_port_delete(test_p2
);
606 printf("porttest: end test main thread\n");
609 static int port_test_thread_func(void* arg
)
615 printf("porttest: port_test_thread_func()\n");
617 n
= sys_port_read(test_p1
, &msg_code
, &buf
, 3);
618 printf("port_read #1 code %d len %d buf %3s\n", msg_code
, n
, buf
);
619 n
= sys_port_read(test_p1
, &msg_code
, &buf
, 4);
620 printf("port_read #1 code %d len %d buf %4s\n", msg_code
, n
, buf
);
622 n
= sys_port_read(test_p1
, &msg_code
, &buf
, 5);
623 printf("port_read #1 code %d len %d buf %5s\n", msg_code
, n
, buf
);
625 printf("porttest: testing delete p1 from other thread\n");
626 sys_port_delete(test_p1
);
627 printf("porttest: end port_test_thread_func()\n");