refactor: add user space printf.
authorMinep <zelong56@gmail.com>
Mon, 22 Aug 2022 15:41:45 +0000 (16:41 +0100)
committerMinep <zelong56@gmail.com>
Mon, 22 Aug 2022 15:41:45 +0000 (16:41 +0100)
14 files changed:
lunaix-os/includes/klibc/stdio.h
lunaix-os/includes/ulibc/stdio.h [new file with mode: 0644]
lunaix-os/kernel/asm/x86/intr_routines.c
lunaix-os/kernel/demos/dir_read.c
lunaix-os/kernel/demos/input_test.c
lunaix-os/kernel/demos/iotest.c
lunaix-os/kernel/demos/signal_demo.c
lunaix-os/kernel/device/device.c
lunaix-os/kernel/kprintf.c
lunaix-os/kernel/lxconsole.c
lunaix-os/kernel/proc0.c
lunaix-os/kernel/spike.c
lunaix-os/libs/klibc/stdio/ksprintf.c [moved from lunaix-os/libs/klibc/stdio/sprintf.c with 95% similarity]
lunaix-os/libs/ulibc/printf.c [new file with mode: 0644]

index d97f0285968d09e16bac247760d29b2157381280..9af99e9f811279dc477c4fa1e41b784f60d07022 100644 (file)
@@ -4,10 +4,10 @@
 #include <stddef.h>
 
 size_t
-__sprintf_internal(char* buffer, char* fmt, size_t max_len, va_list vargs);
+__ksprintf_internal(char* buffer, char* fmt, size_t max_len, va_list vargs);
 
 size_t
-sprintf(char* buffer, char* fmt, ...);
+ksprintf(char* buffer, char* fmt, ...);
 size_t
-snprintf(char* buffer, size_t n, char* fmt, ...);
+ksnprintf(char* buffer, size_t n, char* fmt, ...);
 #endif /* __LUNAIX_STDIO_H */
diff --git a/lunaix-os/includes/ulibc/stdio.h b/lunaix-os/includes/ulibc/stdio.h
new file mode 100644 (file)
index 0000000..e9496a8
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef __LUNAIX_USTDIO_H
+#define __LUNAIX_USTDIO_H
+
+#define stdout 0
+#define stdin 1
+
+void
+printf(const char* fmt, ...);
+
+#endif /* __LUNAIX_USTDIO_H */
index 813490c2fe84363b9f49c6cadb3eafe444505c01..402ceeaf2648090ef030d6542538d81504f7abb8 100644 (file)
@@ -79,7 +79,7 @@ intr_routine_apic_error(const isr_param* param)
 {
     uint32_t error_reg = apic_read_reg(APIC_ESR);
     char buf[32];
-    sprintf(buf, "APIC error, ESR=0x%x", error_reg);
+    ksprintf(buf, "APIC error, ESR=0x%x", error_reg);
     console_flush();
     __print_panic_msg(buf, param);
     spin();
index 0d62a79b38e36bd6887621e8af6d20c328d007ca..54ffd97b20147425350d7ed44ac4028cf4955e6f 100644 (file)
@@ -2,32 +2,29 @@
 #include <lunaix/fctrl.h>
 #include <lunaix/lunistd.h>
 #include <lunaix/proc.h>
-#include <lunaix/syslog.h>
-
-LOG_MODULE("RDDIR")
 
 void
 _readdir_main()
 {
     int fd = open("/dev/./../dev/.", 0);
     if (fd == -1) {
-        kprintf(KERROR "fail to open (%d)\n", geterrno());
+        printf("fail to open (%d)\n", geterrno());
         return;
     }
 
     char path[129];
     int len = realpathat(fd, path, 128);
     if (len < 0) {
-        kprintf(KERROR "fail to read (%d)\n", geterrno());
+        printf("fail to read (%d)\n", geterrno());
     } else {
         path[len] = 0;
-        kprintf("%s\n", path);
+        printf("%s\n", path);
     }
 
     struct dirent ent = { .d_offset = 0 };
 
     while (readdir(fd, &ent) == 1) {
-        kprintf(KINFO "%s\n", ent.d_name);
+        printf("%s\n", ent.d_name);
     }
 
     close(fd);
index ebb93756d9e81d2beee9f908a38be6db48cf8331..b85f85191afec1c559cc0a809cd10ee62c7e2306 100644 (file)
@@ -2,6 +2,7 @@
 #include <lunaix/foptions.h>
 #include <lunaix/input.h>
 #include <lunaix/lunistd.h>
+#include <ulibc/stdio.h>
 
 #define STDIN 1
 #define STDOUT 0
@@ -12,21 +13,26 @@ input_test()
     int fd = open("/dev/input/i8042-kbd", 0);
 
     if (fd < 0) {
-        write(STDOUT, "fail to open", 13);
+        printf("fail to open (%d)", fd);
         return;
     }
 
     struct input_evt_pkt event;
 
     while (read(fd, &event, sizeof(event)) > 0) {
+        char* action;
         if (event.pkt_type == PKT_PRESS) {
-            write(STDOUT, "PRESSED: ", 10);
+            action = "pressed";
         } else {
-            write(STDOUT, "RELEASE: ", 10);
+            action = "release";
         }
-        char c = event.sys_code & 0xff;
-        write(STDOUT, &c, 1);
-        write(STDOUT, "\n", 2);
+
+        printf("%u: %s '%c', class=0x%x, scan=%d\n",
+               event.timestamp,
+               action,
+               event.sys_code & 0xff,
+               (event.sys_code & 0xff00) >> 8,
+               event.scan_code);
     }
     return;
 }
\ No newline at end of file
index 0ebe696eb48c5e1cc79fcb5c018945a5a8f61683..643452c81f58b0a145c5457f428311c7d9ae89f6 100644 (file)
@@ -2,12 +2,7 @@
 #include <lunaix/foptions.h>
 #include <lunaix/lunistd.h>
 #include <lunaix/proc.h>
-#include <lunaix/syslog.h>
-
-LOG_MODULE("IOTEST")
-
-#define STDIN 1
-#define STDOUT 0
+#include <ulibc/stdio.h>
 
 void
 _iotest_main()
@@ -20,14 +15,12 @@ _iotest_main()
     // 切换工作目录至 /dev
     int errno = chdir("/dev");
     if (errno) {
-        write(STDOUT, "fail to chdir", 15);
+        write(stdout, "fail to chdir", 15);
         return;
     }
 
     if (getcwd(read_out, sizeof(read_out))) {
-        write(STDOUT, "current working dir: ", 22);
-        write(STDOUT, read_out, 256);
-        write(STDOUT, "\n", 2);
+        printf("current working dir: %s\n", read_out);
     }
 
     // sda 设备 - 硬盘
@@ -36,7 +29,7 @@ _iotest_main()
     int fd = open("./sda", 0);
 
     if (fd < 0) {
-        kprintf(KERROR "fail to open (%d)\n", geterrno());
+        printf("fail to open (%d)\n", geterrno());
         return;
     }
 
@@ -52,24 +45,21 @@ _iotest_main()
     lseek(fd, 4 * 4096, FSEEK_SET);
     write(fd, test_sequence, sizeof(test_sequence));
 
-    write(STDOUT, "input: ", 8);
-    int size = read(STDIN, read_out, 256);
+    printf("input: ");
+    int size = read(stdin, read_out, 256);
+
+    printf("your said: %s\n", read_out);
 
-    write(STDOUT, "your input: ", 13);
-    write(STDOUT, read_out, size);
     write(fd, read_out, size);
-    write(STDOUT, "\n", 1);
 
     // 读出我们写的内容
     lseek(fd, 512, FSEEK_SET);
     read(fd, read_out, sizeof(read_out));
 
     // 将读出的内容直接写入tty设备
-    write(STDOUT, read_out, sizeof(read_out));
-    write(STDOUT, "\n", 1);
+    write(stdout, read_out, sizeof(read_out));
+    write(stdout, "\n", 1);
 
     // 关闭文件,这同时会将页缓存中的数据下发到底层驱动
     close(fd);
-
-    kprint_hex(read_out, sizeof(read_out));
 }
\ No newline at end of file
index 616a38e9e090773b1b39fba1038dd0eb3d37ad6d..27d78142ef0129b8d806eb5e6d066c8469061cad 100644 (file)
@@ -2,22 +2,20 @@
 #include <lunaix/proc.h>
 #include <lunaix/signal.h>
 #include <lunaix/spike.h>
-#include <lunaix/syslog.h>
 #include <lunaix/types.h>
-
-LOG_MODULE("SIGDEMO")
+#include <ulibc/stdio.h>
 
 void __USER__
 sigchild_handler(int signum)
 {
-    kprintf(KINFO "SIGCHLD received\n");
+    printf("SIGCHLD received\n");
 }
 
 void __USER__
 sigsegv_handler(int signum)
 {
     pid_t pid = getpid();
-    kprintf(KWARN "SIGSEGV received on process %d\n", pid);
+    printf("SIGSEGV received on process %d\n", pid);
     _exit(signum);
 }
 
@@ -25,7 +23,7 @@ void __USER__
 sigalrm_handler(int signum)
 {
     pid_t pid = getpid();
-    kprintf(KWARN "I, pid %d, have received an alarm!\n", pid);
+    printf("I, pid %d, have received an alarm!\n", pid);
 }
 
 void __USER__
@@ -40,7 +38,7 @@ _signal_demo_main()
     int status;
     pid_t p = 0;
 
-    kprintf(KINFO "Child sleep 3s, parent pause.\n");
+    printf("Child sleep 3s, parent pause.\n");
     if (!fork()) {
         sleep(3);
         _exit(0);
@@ -48,7 +46,7 @@ _signal_demo_main()
 
     pause();
 
-    kprintf("Parent resumed on SIGCHILD\n");
+    printf("Parent resumed on SIGCHILD\n");
 
     for (int i = 0; i < 5; i++) {
         pid_t pid = 0;
@@ -58,26 +56,22 @@ _signal_demo_main()
             if (i == 3) {
                 i = *(int*)0xdeadc0de; // seg fault!
             }
-            kprintf(KINFO "%d\n", i);
+            printf("%d\n", i);
             _exit(0);
         }
-        kprintf(KINFO "Forked %d\n", pid);
+        printf("Forked %d\n", pid);
     }
 
     while ((p = wait(&status)) >= 0) {
         short code = WEXITSTATUS(status);
         if (WIFSIGNALED(status)) {
-            kprintf(KINFO "Process %d terminated by signal, exit_code: %d\n",
-                    p,
-                    code);
+            printf("Process %d terminated by signal, exit_code: %d\n", p, code);
         } else if (WIFEXITED(status)) {
-            kprintf(KINFO "Process %d exited with code %d\n", p, code);
+            printf("Process %d exited with code %d\n", p, code);
         } else {
-            kprintf(KWARN "Process %d aborted with code %d\n", p, code);
+            printf("Process %d aborted with code %d\n", p, code);
         }
     }
 
-    kprintf("done\n");
-
-    _exit(0);
+    printf("done\n");
 }
\ No newline at end of file
index 0990c56d39e7f3b8e2d7598b9661c35b21e93fbf..1512e6f1cb56bf2fc8f402a6243413524140a60d 100644 (file)
@@ -22,7 +22,7 @@ device_add(struct device* parent,
     }
 
     size_t strlen =
-      __sprintf_internal(dev->name_val, name_fmt, DEVICE_NAME_SIZE, args);
+      __ksprintf_internal(dev->name_val, name_fmt, DEVICE_NAME_SIZE, args);
 
     dev->dev_id = devid++;
     dev->name = HSTR(dev->name_val, strlen);
index d18691ac2fd1fa6d9ccd3389dcb7058c5710408d..fd005c09b9295f23b33ea737df3dc4612af118fc 100644 (file)
@@ -22,51 +22,51 @@ __kprintf(const char* component, const char* fmt, va_list args)
 
     switch (log_level) {
         case '0':
-            snprintf(expanded_fmt,
-                     MAX_XFMT_SIZE,
-                     "[%s] (%s) %s",
-                     "INFO",
-                     component,
-                     fmt);
+            ksnprintf(expanded_fmt,
+                      MAX_XFMT_SIZE,
+                      "[%s] (%s) %s",
+                      "INFO",
+                      component,
+                      fmt);
             break;
         case '1':
             // tty_set_theme(VGA_COLOR_BROWN, current_theme >> 12);
-            snprintf(expanded_fmt,
-                     MAX_XFMT_SIZE,
-                     "\033[6;0m[%s] (%s) %s\033[39;49m",
-                     "WARN",
-                     component,
-                     fmt);
+            ksnprintf(expanded_fmt,
+                      MAX_XFMT_SIZE,
+                      "\033[6;0m[%s] (%s) %s\033[39;49m",
+                      "WARN",
+                      component,
+                      fmt);
             break;
         case '2':
             // tty_set_theme(VGA_COLOR_LIGHT_RED, current_theme >> 12);
-            snprintf(expanded_fmt,
-                     MAX_XFMT_SIZE,
-                     "\033[12;0m[%s] (%s) %s\033[39;49m",
-                     "EROR",
-                     component,
-                     fmt);
+            ksnprintf(expanded_fmt,
+                      MAX_XFMT_SIZE,
+                      "\033[12;0m[%s] (%s) %s\033[39;49m",
+                      "EROR",
+                      component,
+                      fmt);
             break;
         case '3':
             // tty_set_theme(VGA_COLOR_LIGHT_BLUE, current_theme >> 12);
-            snprintf(expanded_fmt,
-                     MAX_XFMT_SIZE,
-                     "\033[9;0m[%s] (%s) %s\033[39;49m",
-                     "DEBG",
-                     component,
-                     fmt);
+            ksnprintf(expanded_fmt,
+                      MAX_XFMT_SIZE,
+                      "\033[9;0m[%s] (%s) %s\033[39;49m",
+                      "DEBG",
+                      component,
+                      fmt);
             break;
         default:
-            snprintf(expanded_fmt,
-                     MAX_XFMT_SIZE,
-                     "[%s] (%s) %s",
-                     "LOG",
-                     component,
-                     fmt);
+            ksnprintf(expanded_fmt,
+                      MAX_XFMT_SIZE,
+                      "[%s] (%s) %s",
+                      "LOG",
+                      component,
+                      fmt);
             break;
     }
 
-    __sprintf_internal(buf, expanded_fmt, MAX_KPRINTF_BUF_SIZE, args);
+    __ksprintf_internal(buf, expanded_fmt, MAX_KPRINTF_BUF_SIZE, args);
     console_write_str(buf);
 }
 
@@ -80,7 +80,7 @@ kprint_panic(const char* fmt, ...)
     tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_RED);
     tty_clear_line(24);
 
-    __sprintf_internal(buf, fmt, MAX_KPRINTF_BUF_SIZE, args);
+    __ksprintf_internal(buf, fmt, MAX_KPRINTF_BUF_SIZE, args);
     tty_put_str_at(buf, 0, 24);
 
     va_end(args);
@@ -98,12 +98,12 @@ kprint_hex(const void* buffer, unsigned int size)
     ch_cache[0] = '|';
     ch_cache[1] = ' ';
     while (size) {
-        snprintf(buf, 64, " %.4p: ", ptr);
+        ksnprintf(buf, 64, " %.4p: ", ptr);
         console_write_str(buf);
         for (i = 0; i < 8 && size; i++, size--, ptr++) {
             unsigned char c = *(data + ptr) & 0xff;
             ch_cache[2 + i] = (32 <= c && c < 127) ? c : '.';
-            snprintf(buf, 64, "%.2x  ", c);
+            ksnprintf(buf, 64, "%.2x  ", c);
             console_write_str(buf);
         }
         ch_cache[2 + i] = '\0';
index c718b7124c82f284586c39c906e11150d36e4165..77949c897822c699c733fc03ac683b493e48a44f 100644 (file)
@@ -50,23 +50,9 @@ void
 lxconsole_init()
 {
     memset(&lx_console, 0, sizeof(lx_console));
-    fifo_init(&lx_console.output, VGA_BUFFER_VADDR + 0x1000, 8192, 0);
+    fifo_init(&lx_console.output, vzalloc(8192), 8192, 0);
     fifo_init(&lx_console.input, valloc(4096), 4096, 0);
 
-    // FIXME use valloc to allocate console buffer.
-    // In doing this, the console buffer can only be accessed from kernel mode
-    //  any direct write to this buffer from user land should be purged!
-
-    // 分配控制台缓存
-    for (size_t i = 0; i < PG_ALIGN(lx_console.output.size); i += PG_SIZE) {
-        uintptr_t pa = pmm_alloc_page(KERNEL_PID, 0);
-        vmm_set_mapping(PD_REFERENCED,
-                        (uintptr_t)lx_console.output.data + i,
-                        pa,
-                        PG_PREM_URW,
-                        0);
-    }
-
     lx_console.flush_timer = NULL;
 
     struct device* tty_dev = device_addseq(NULL, &lx_console, "tty");
index c02cbe1f3087f35ad9f2e4a6587c57a15f2c4411..f926ffda7df7926c6a063aabdaab1f0fa7b36898 100644 (file)
@@ -27,6 +27,8 @@
 
 #include <klibc/string.h>
 
+#include <ulibc/stdio.h>
+
 LOG_MODULE("PROC0")
 
 extern void
@@ -74,8 +76,8 @@ __proc0_usr()
     // 打开tty设备(控制台),作为标准输入输出。
     //  tty设备属于序列设备(Sequential Device),该类型设备的上层读写
     //  无须经过Lunaix的缓存层,而是直接下发到底层驱动。(不受FO_DIRECT的影响)
-    int stdout = open("/dev/tty", 0);
-    int stdin = dup2(stdout, 1);
+    int fdstdout = open("/dev/tty", 0);
+    int fdstdin = dup2(stdout, 1);
 
     pid_t p;
     // if (!fork()) {
@@ -96,6 +98,7 @@ __proc0_usr()
 #else
         _lxinit_main();
 #endif
+        printf("==== test end ====\n");
         _exit(0);
     }
 
@@ -148,9 +151,6 @@ init_platform()
     // 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
     lock_reserved_memory();
 
-    // we are using no kalloc!
-    // assert_msg(kalloc_init(), "Fail to initialize heap");
-
     rtc_init();
     acpi_init(_k_init_mb_info);
     apic_init();
@@ -161,8 +161,6 @@ init_platform()
     pci_init();
     block_init();
     ahci_init();
-    // ahci_list_device();
-    // cake_stats();
 
     syscall_install();
 
index 87726b01fce4ab150fbc6f4f82513019577119b1..860b201d8f2b6542b0990f34f0500a3478e64812 100644 (file)
@@ -7,7 +7,7 @@ static char buffer[1024];
 void
 __assert_fail(const char* expr, const char* file, unsigned int line)
 {
-    sprintf(buffer, "%s (%s:%u)", expr, file, line);
+    ksprintf(buffer, "%s (%s:%u)", expr, file, line);
 
     // Here we load the buffer's address into %edi ("D" constraint)
     //  This is a convention we made that the LUNAIX_SYS_PANIC syscall will
@@ -30,7 +30,7 @@ panickf(const char* fmt, ...)
 {
     va_list args;
     va_start(args, fmt);
-    __sprintf_internal(buffer, fmt, 1024, args);
+    __ksprintf_internal(buffer, fmt, 1024, args);
     va_end(args);
 
     asm("int %0" ::"i"(LUNAIX_SYS_PANIC), "D"(buffer));
similarity index 95%
rename from lunaix-os/libs/klibc/stdio/sprintf.c
rename to lunaix-os/libs/klibc/stdio/ksprintf.c
index ade37bbf1ea97a723bb1e9e7065ebc0ce26ec655..38b90319d82aa33e737f0060b4a2c70f6374f33a 100644 (file)
@@ -20,7 +20,7 @@ static const char flag_chars[] = "#0- +";
 #define FLAG_CAPS (1 << 9)
 
 size_t
-__sprintf_internal(char* buffer, char* fmt, size_t max_len, va_list vargs)
+__ksprintf_internal(char* buffer, char* fmt, size_t max_len, va_list vargs)
 {
     // This sprintf just a random implementation I found it on Internet . lol.
     //      Of course, with some modifications for porting to LunaixOS :)
@@ -196,21 +196,21 @@ __sprintf_internal(char* buffer, char* fmt, size_t max_len, va_list vargs)
 }
 
 size_t
-sprintf(char* buffer, char* fmt, ...)
+ksprintf(char* buffer, char* fmt, ...)
 {
     va_list args;
     va_start(args, fmt);
-    size_t len = __sprintf_internal(buffer, fmt, 0, args);
+    size_t len = __ksprintf_internal(buffer, fmt, 0, args);
     va_end(args);
     return len;
 }
 
 size_t
-snprintf(char* buffer, size_t n, char* fmt, ...)
+ksnprintf(char* buffer, size_t n, char* fmt, ...)
 {
     va_list args;
     va_start(args, fmt);
-    size_t len = __sprintf_internal(buffer, fmt, n, args);
+    size_t len = __ksprintf_internal(buffer, fmt, n, args);
     va_end(args);
     return len;
 }
\ No newline at end of file
diff --git a/lunaix-os/libs/ulibc/printf.c b/lunaix-os/libs/ulibc/printf.c
new file mode 100644 (file)
index 0000000..18e2387
--- /dev/null
@@ -0,0 +1,23 @@
+#include <klibc/stdio.h>
+#include <lunaix/lunistd.h>
+#include <lunaix/spike.h>
+#include <ulibc/stdio.h>
+
+// This is VERY bad implementation as it mixes both kernel and user space
+// code together. It is here however, just for the convenience of our testing
+// program.
+// FIXME Eliminate this when we're able to load program.
+
+void __USER__
+printf(const char* fmt, ...)
+{
+    const char buf[512];
+    va_list args;
+    va_start(args, fmt);
+
+    size_t sz = __ksprintf_internal(buf, fmt, 512, args);
+
+    write(stdout, buf, sz);
+
+    va_end(args);
+}
\ No newline at end of file