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