From: Minep Date: Mon, 22 Aug 2022 15:41:45 +0000 (+0100) Subject: refactor: add user space printf. X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/commitdiff_plain/e5c3c8accffbcd77fea12ccf2b0accc70c645aaa?hp=5ea8e2ba737f903db81d49b56778e883634512a5 refactor: add user space printf. --- diff --git a/lunaix-os/includes/klibc/stdio.h b/lunaix-os/includes/klibc/stdio.h index d97f028..9af99e9 100644 --- a/lunaix-os/includes/klibc/stdio.h +++ b/lunaix-os/includes/klibc/stdio.h @@ -4,10 +4,10 @@ #include 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 index 0000000..e9496a8 --- /dev/null +++ b/lunaix-os/includes/ulibc/stdio.h @@ -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 */ diff --git a/lunaix-os/kernel/asm/x86/intr_routines.c b/lunaix-os/kernel/asm/x86/intr_routines.c index 813490c..402ceea 100644 --- a/lunaix-os/kernel/asm/x86/intr_routines.c +++ b/lunaix-os/kernel/asm/x86/intr_routines.c @@ -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(); diff --git a/lunaix-os/kernel/demos/dir_read.c b/lunaix-os/kernel/demos/dir_read.c index 0d62a79..54ffd97 100644 --- a/lunaix-os/kernel/demos/dir_read.c +++ b/lunaix-os/kernel/demos/dir_read.c @@ -2,32 +2,29 @@ #include #include #include -#include - -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); diff --git a/lunaix-os/kernel/demos/input_test.c b/lunaix-os/kernel/demos/input_test.c index ebb9375..b85f851 100644 --- a/lunaix-os/kernel/demos/input_test.c +++ b/lunaix-os/kernel/demos/input_test.c @@ -2,6 +2,7 @@ #include #include #include +#include #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 diff --git a/lunaix-os/kernel/demos/iotest.c b/lunaix-os/kernel/demos/iotest.c index 0ebe696..643452c 100644 --- a/lunaix-os/kernel/demos/iotest.c +++ b/lunaix-os/kernel/demos/iotest.c @@ -2,12 +2,7 @@ #include #include #include -#include - -LOG_MODULE("IOTEST") - -#define STDIN 1 -#define STDOUT 0 +#include 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 diff --git a/lunaix-os/kernel/demos/signal_demo.c b/lunaix-os/kernel/demos/signal_demo.c index 616a38e..27d7814 100644 --- a/lunaix-os/kernel/demos/signal_demo.c +++ b/lunaix-os/kernel/demos/signal_demo.c @@ -2,22 +2,20 @@ #include #include #include -#include #include - -LOG_MODULE("SIGDEMO") +#include 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 diff --git a/lunaix-os/kernel/device/device.c b/lunaix-os/kernel/device/device.c index 0990c56..1512e6f 100644 --- a/lunaix-os/kernel/device/device.c +++ b/lunaix-os/kernel/device/device.c @@ -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); diff --git a/lunaix-os/kernel/kprintf.c b/lunaix-os/kernel/kprintf.c index d18691a..fd005c0 100644 --- a/lunaix-os/kernel/kprintf.c +++ b/lunaix-os/kernel/kprintf.c @@ -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'; diff --git a/lunaix-os/kernel/lxconsole.c b/lunaix-os/kernel/lxconsole.c index c718b71..77949c8 100644 --- a/lunaix-os/kernel/lxconsole.c +++ b/lunaix-os/kernel/lxconsole.c @@ -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"); diff --git a/lunaix-os/kernel/proc0.c b/lunaix-os/kernel/proc0.c index c02cbe1..f926ffd 100644 --- a/lunaix-os/kernel/proc0.c +++ b/lunaix-os/kernel/proc0.c @@ -27,6 +27,8 @@ #include +#include + 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(); diff --git a/lunaix-os/kernel/spike.c b/lunaix-os/kernel/spike.c index 87726b0..860b201 100644 --- a/lunaix-os/kernel/spike.c +++ b/lunaix-os/kernel/spike.c @@ -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)); diff --git a/lunaix-os/libs/klibc/stdio/sprintf.c b/lunaix-os/libs/klibc/stdio/ksprintf.c similarity index 95% rename from lunaix-os/libs/klibc/stdio/sprintf.c rename to lunaix-os/libs/klibc/stdio/ksprintf.c index ade37bb..38b9031 100644 --- a/lunaix-os/libs/klibc/stdio/sprintf.c +++ b/lunaix-os/libs/klibc/stdio/ksprintf.c @@ -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 index 0000000..18e2387 --- /dev/null +++ b/lunaix-os/libs/ulibc/printf.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +// 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