91a4e504192401162709b8dafad6d11b266e8b38
[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 sigsegv_handler(int signum)
30 {
31     pid_t pid = getpid();
32     kprintf(KWARN "SIGSEGV received on process %d\n", pid);
33     _exit(signum);
34 }
35
36 void __USER__
37 sigalrm_handler(int signum)
38 {
39     pid_t pid = getpid();
40     kprintf(KWARN "I, pid %d, have received an alarm!\n", pid);
41 }
42
43 void __USER__
44 _lxinit_main()
45 {
46 #ifdef FORK_BOMB_DEMO
47     // fork炸弹
48     for (;;) {
49         pid_t p;
50         if ((p = fork())) {
51             kprintf(KDEBUG "Pinkie Pie #%d: FUN!\n", p);
52         }
53     }
54 #endif
55
56     signal(_SIGCHLD, sigchild_handler);
57     signal(_SIGSEGV, sigsegv_handler);
58     signal(_SIGALRM, sigalrm_handler);
59
60     alarm(5);
61
62     int status;
63 #ifdef WAIT_DEMO
64     // 测试wait
65     kprintf("I am parent, going to fork my child and wait.\n");
66     if (!fork()) {
67         kprintf("I am child, going to sleep for 2 seconds\n");
68         sleep(2);
69         kprintf("I am child, I am about to terminated\n");
70         _exit(1);
71     }
72     pause();
73     pid_t child = wait(&status);
74     kprintf("I am parent, my child (%d) terminated normally with code: %d.\n",
75             child,
76             WEXITSTATUS(status));
77 #endif
78
79     pid_t p = 0;
80
81     if (!fork()) {
82         kprintf("Test no hang!\n");
83         sleep(6);
84         _exit(0);
85     }
86
87     waitpid(-1, &status, WNOHANG);
88
89     for (size_t i = 0; i < 5; i++) {
90         pid_t pid = 0;
91         if (!(pid = fork())) {
92             signal(_SIGSEGV, sigsegv_handler);
93             sleep(i);
94             if (i == 3) {
95                 i = *(int*)0xdeadc0de; // seg fault!
96             }
97             tty_put_char('0' + i);
98             tty_put_char('\n');
99             _exit(0);
100         }
101         kprintf(KINFO "Forked %d\n", pid);
102     }
103
104     while ((p = wait(&status)) >= 0) {
105         short code = WEXITSTATUS(status);
106         if (WIFSIGNALED(status)) {
107             kprintf(KINFO "Process %d terminated by signal, exit_code: %d\n",
108                     p,
109                     code);
110         } else if (WIFEXITED(status)) {
111             kprintf(KINFO "Process %d exited with code %d\n", p, code);
112         } else {
113             kprintf(KWARN "Process %d aborted with code %d\n", p, code);
114         }
115     }
116
117     char buf[64];
118
119     kprintf(KINFO "Hello processes!\n");
120
121     cpu_get_brand(buf);
122     kprintf("CPU: %s\n\n", buf);
123
124     // no lxmalloc here! This can only be used within kernel, but here, we are
125     // in a dedicated process! any access to kernel method must be done via
126     // syscall
127
128     struct kdb_keyinfo_pkt keyevent;
129     while (1) {
130         if (!kbd_recv_key(&keyevent)) {
131             yield();
132             continue;
133         }
134         if ((keyevent.state & KBD_KEY_FPRESSED) &&
135             (keyevent.keycode & 0xff00) <= KEYPAD) {
136             tty_put_char((char)(keyevent.keycode & 0x00ff));
137             // FIXME: io to vga port is privileged and cause #GP in user mode
138             // tty_sync_cursor();
139         }
140     }
141     spin();
142 }