1485c3411e4d95266642876f8481f0b9b3168173
[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     pid_t child = wait(&status);
54     kprintf("I am parent, my child (%d) terminated normally with code: %d.\n",
55             child,
56             WEXITSTATUS(status));
57 #endif
58
59     pid_t p = 0;
60
61     if (!fork()) {
62         kprintf("Test no hang!\n");
63         sleep(6);
64         _exit(0);
65     }
66
67     waitpid(-1, &status, WNOHANG);
68
69     for (size_t i = 0; i < 10; i++) {
70         pid_t pid = 0;
71         if (!(pid = fork())) {
72             sleep(i);
73             if (i == 3) {
74                 i = *(int*)0xdeadc0de; // seg fault!
75             }
76             tty_put_char('0' + i);
77             tty_put_char('\n');
78             _exit(0);
79         }
80         kprintf(KINFO "Forked %d\n", pid);
81     }
82
83     while ((p = wait(&status)) >= 0) {
84         short code = WEXITSTATUS(status);
85         if (WIFEXITED(status)) {
86             kprintf(KINFO "Process %d exited with code %d\n", p, code);
87         } else {
88             kprintf(KWARN "Process %d aborted with code %d\n", p, code);
89         }
90     }
91
92     char buf[64];
93
94     kprintf(KINFO "Hello processes!\n");
95
96     cpu_get_brand(buf);
97     kprintf("CPU: %s\n\n", buf);
98
99     // no lxmalloc here! This can only be used within kernel, but here, we are
100     // in a dedicated process! any access to kernel method must be done via
101     // syscall
102
103     struct kdb_keyinfo_pkt keyevent;
104     while (1) {
105         if (!kbd_recv_key(&keyevent)) {
106             yield();
107             continue;
108         }
109         if ((keyevent.state & KBD_KEY_FPRESSED) &&
110             (keyevent.keycode & 0xff00) <= KEYPAD) {
111             tty_put_char((char)(keyevent.keycode & 0x00ff));
112             // FIXME: io to vga port is privileged and cause #GP in user mode
113             // tty_sync_cursor();
114         }
115     }
116     spin();
117 }