refactor: make the demos into dedicated files
[lunaix-os.git] / lunaix-os / kernel / lxinit.c
1 #include <hal/cpu.h>
2 #include <lunaix/clock.h>
3 #include <lunaix/keyboard.h>
4 #include <lunaix/lunistd.h>
5 #include <lunaix/mm/kalloc.h>
6 #include <lunaix/mm/vmm.h>
7 #include <lunaix/proc.h>
8 #include <lunaix/spike.h>
9 #include <lunaix/syslog.h>
10 #include <lunaix/timer.h>
11 #include <lunaix/tty/tty.h>
12
13 extern uint8_t __kernel_start;
14
15 LOG_MODULE("INIT")
16
17 // #define FORK_BOMB_DEMO
18 #define WAIT_DEMO
19 #define IN_USER_MODE
20
21 void __USER__
22 _lxinit_main()
23 {
24 #ifdef FORK_BOMB_DEMO
25     // fork炸弹
26     for (;;) {
27         pid_t p;
28         if ((p = fork())) {
29             kprintf(KDEBUG "Pinkie Pie #%d: FUN!\n", p);
30         }
31     }
32 #endif
33
34     int status;
35 #ifdef WAIT_DEMO
36     // 测试wait
37     kprintf("I am parent, going to fork my child and wait.\n");
38     if (!fork()) {
39         kprintf("I am child, going to sleep for 2 seconds\n");
40         sleep(2);
41         kprintf("I am child, I am about to terminated\n");
42         _exit(1);
43     }
44     wait(&status);
45     pid_t child = wait(&status);
46     kprintf("I am parent, my child (%d) terminated normally with code: %d.\n",
47             child,
48             WEXITSTATUS(status));
49 #endif
50
51     pid_t p = 0;
52
53     if (!fork()) {
54         kprintf("Test no hang!\n");
55         sleep(6);
56         _exit(0);
57     }
58
59     waitpid(-1, &status, WNOHANG);
60
61     for (size_t i = 0; i < 5; i++) {
62         pid_t pid = 0;
63         if (!(pid = fork())) {
64             sleep(i);
65             if (i == 3) {
66                 i = *(int*)0xdeadc0de; // seg fault!
67             }
68             tty_put_char('0' + i);
69             tty_put_char('\n');
70             _exit(0);
71         }
72         kprintf(KINFO "Forked %d\n", pid);
73     }
74
75     while ((p = wait(&status)) >= 0) {
76         short code = WEXITSTATUS(status);
77         if (WIFEXITED(status)) {
78             kprintf(KINFO "Process %d exited with code %d\n", p, code);
79         } else {
80             kprintf(KWARN "Process %d aborted with code %d\n", p, code);
81         }
82     }
83
84     char buf[64];
85
86     kprintf(KINFO "Hello processes!\n");
87
88     cpu_get_brand(buf);
89     kprintf("CPU: %s\n\n", buf);
90
91     // no lxmalloc here! This can only be used within kernel, but here, we are
92     // in a dedicated process! any access to kernel method must be done via
93     // syscall
94
95     struct kdb_keyinfo_pkt keyevent;
96     while (1) {
97         if (!kbd_recv_key(&keyevent)) {
98             yield();
99             continue;
100         }
101         if ((keyevent.state & KBD_KEY_FPRESSED) &&
102             (keyevent.keycode & 0xff00) <= KEYPAD) {
103             tty_put_char((char)(keyevent.keycode & 0x00ff));
104             // FIXME: io to vga port is privileged and cause #GP in user mode
105             // tty_sync_cursor();
106         }
107     }
108     spin();
109 }