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