feat: kprintf now goes into dedicated pseudo-dev rather than flooding the framebuffer
authorMinep <lunaixsky@qq.com>
Fri, 3 Nov 2023 19:33:53 +0000 (19:33 +0000)
committerMinep <lunaixsky@qq.com>
Fri, 3 Nov 2023 19:33:53 +0000 (19:33 +0000)
refactor: a better kprintf buffer design that can carry more information and simplify interface signature
chores: some housekeeping stuff

37 files changed:
lunaix-os/arch/i386/exceptions/interrupts.c
lunaix-os/arch/i386/exceptions/intr_routines.c
lunaix-os/arch/i386/hal/apic.c
lunaix-os/arch/i386/mm/pfault.c
lunaix-os/hal/acpi/acpi.c
lunaix-os/hal/ahci/ahci.c
lunaix-os/hal/char/ps2kbd.c
lunaix-os/hal/pci.c
lunaix-os/hal/timer/apic_timer.c
lunaix-os/includes/klibc/stdio.h [deleted file]
lunaix-os/includes/klibc/strfmt.h [new file with mode: 0644]
lunaix-os/includes/lunaix/clock.h
lunaix-os/includes/lunaix/compiler.h
lunaix-os/includes/lunaix/fs/twifs.h
lunaix-os/includes/lunaix/syslog.h
lunaix-os/kernel/block/blkpart_gpt.c
lunaix-os/kernel/block/block.c
lunaix-os/kernel/debug/sdbg.c
lunaix-os/kernel/debug/trace.c
lunaix-os/kernel/device/devdb.c
lunaix-os/kernel/device/device.c
lunaix-os/kernel/fs/mount.c
lunaix-os/kernel/fs/probe_boot.c
lunaix-os/kernel/fs/twifs/twifs.c
lunaix-os/kernel/fs/twimap.c
lunaix-os/kernel/kinit.c
lunaix-os/kernel/kprint/kp_records.c [new file with mode: 0644]
lunaix-os/kernel/kprint/kp_records.h [new file with mode: 0644]
lunaix-os/kernel/kprint/kprintf.c [new file with mode: 0644]
lunaix-os/kernel/kprintf.c [deleted file]
lunaix-os/kernel/proc0.c
lunaix-os/kernel/process/process.c
lunaix-os/kernel/process/taskfs.c
lunaix-os/kernel/spike.c
lunaix-os/kernel/time/clock.c
lunaix-os/libs/klibc/ksprintf.c [moved from lunaix-os/libs/klibc/stdio/ksprintf.c with 95% similarity]
lunaix-os/usr/link-usr.ld

index 5d4787ab94f33f637fca23ddd9ef93b2a423bcb5..84a29c1ea4f98384ad250233cc6188357dc1ae5c 100644 (file)
@@ -28,11 +28,11 @@ intr_handler(isr_param* param)
         goto done;
     }
 
-    kprint_panic("INT %u: (%x) [%p: %p] Unknown",
-                 execp->vector,
-                 execp->err_code,
-                 execp->cs,
-                 execp->eip);
+    kprintf(KERROR "INT %u: (%x) [%p: %p] Unknown",
+            execp->vector,
+            execp->err_code,
+            execp->cs,
+            execp->eip);
 
 done:
 
index d0b0d492915043264a0e54827a3eb012c47cc588..33b33c8e92ef091abb3c29ae59d79a33f602b38f 100644 (file)
@@ -8,7 +8,7 @@
 #include <lunaix/syslog.h>
 #include <lunaix/trace.h>
 
-#include <klibc/stdio.h>
+#include <klibc/strfmt.h>
 
 #include <sys/apic.h>
 #include <sys/i386_intr.h>
@@ -23,7 +23,7 @@ extern u32_t debug_resv;
 void
 __print_panic_msg(const char* msg, const isr_param* param)
 {
-    kprintf(KERROR "panic: %s\n", msg);
+    kprintf(KERROR "panic: %s", msg);
     trace_printstack_isr(param);
 }
 
index 4323ac8051ab8b10852b9163306b346d64d5fb9f..458bf03baf08eed03683c2f833983a25c5d38fd6 100644 (file)
@@ -9,8 +9,8 @@
  *
  */
 
-#include <sys/cpu.h>
 #include <hal/intc.h>
+#include <sys/cpu.h>
 
 #include <sys/apic.h>
 #include <sys/interrupts.h>
@@ -60,7 +60,7 @@ apic_init()
     u32_t apic_id = apic_read_reg(APIC_IDR) >> 24;
     u32_t apic_ver = apic_read_reg(APIC_VER);
 
-    kprintf(KINFO "ID: %x, Version: %x, Max LVT: %u\n",
+    kprintf(KINFO "ID: %x, Version: %x, Max LVT: %u",
             apic_id,
             apic_ver & 0xff,
             (apic_ver >> 16) & 0xff);
index 2a2c6f3ad9ed8ada9ed9034df2fc4d5b9aa64d12..2533d0bac8dc1829f6361ae7caa5428e9bd1e495 100644 (file)
@@ -67,7 +67,7 @@ intr_routine_page_fault(const isr_param* param)
     if (PG_IS_PRESENT(*pte)) {
         if (((errcode ^ mapping.flags) & PG_ALLOW_USER)) {
             // invalid access
-            kprintf(KDEBUG "invalid user access. (%p->%p, attr:0x%x)\n",
+            kprintf(KDEBUG "invalid user access. (%p->%p, attr:0x%x)",
                     mapping.va,
                     mapping.pa,
                     mapping.flags);
@@ -133,7 +133,7 @@ intr_routine_page_fault(const isr_param* param)
         }
 
         if (errno < 0) {
-            kprintf(KERROR "fail to populate page (%d)\n", errno);
+            kprintf(KERROR "fail to populate page (%d)", errno);
             goto segv_term;
         }
 
@@ -148,10 +148,10 @@ intr_routine_page_fault(const isr_param* param)
         ;
 
 oom:
-    kprintf(KERROR "out of memory\n");
+    kprintf(KERROR "out of memory");
 
 segv_term:
-    kprintf(KERROR "(pid: %d) Segmentation fault on %p (%p:%p,e=0x%x)\n",
+    kprintf(KERROR "(pid: %d) Segmentation fault on %p (%p:%p,e=0x%x)",
             __current->pid,
             ptr,
             param->execp->cs,
index 7ad3f06e1c00177947783d6b50a7ca6e91c618cf..67915fa3092b86c0ddf34d04fac1ad46d32fbf45 100644 (file)
@@ -40,16 +40,13 @@ acpi_init()
           (acpi_sdthdr_t*)((acpi_apic_t**)&(rsdt->entry))[i];
         switch (sdthdr->signature) {
             case ACPI_MADT_SIG:
-                kprintf(KINFO "MADT: %p\n", sdthdr);
                 madt_parse((acpi_madt_t*)sdthdr, ctx);
                 break;
             case ACPI_FADT_SIG:
                 // FADT just a plain structure, no need to parse.
-                kprintf(KINFO "FADT: %p\n", sdthdr);
                 ctx->fadt = *(acpi_fadt_t*)sdthdr;
                 break;
             case ACPI_MCFG_SIG:
-                kprintf(KINFO "MCFG: %p\n", sdthdr);
                 mcfg_parse(sdthdr, ctx);
                 break;
             default:
@@ -57,8 +54,6 @@ acpi_init()
         }
     }
 
-    kprintf(KINFO "ACPI: %s\n", ctx->oem_id);
-
     return 0;
 }
 
index 61ea95b511dc28a5d658bd9d19d951aae94e153b..33dc87e3a05287e859baebcdd06c1f6b4e337aeb 100644 (file)
@@ -186,12 +186,12 @@ ahci_driver_init(struct device_def* def, struct device* dev)
         port_regs[HBA_RPxCMD] |= HBA_PxCMD_ST;
 
         if (!ahci_init_device(port)) {
-            kprintf(KERROR "init fail: 0x%x@p%d\n", port->regs[HBA_RPxSIG], i);
+            kprintf(KERROR "init fail: 0x%x@p%d", port->regs[HBA_RPxSIG], i);
             continue;
         }
 
         struct hba_device* hbadev = port->device;
-        kprintf(KINFO "sata%d: %s, blk_size=%d, blk=0..%d\n",
+        kprintf(KINFO "sata%d: %s, blk_size=%d, blk=0..%d",
                 i,
                 hbadev->model,
                 hbadev->block_size,
index c86350bc190576158d3b615b63f948838e8dda25..5cbbae8c5015ee38612b0d8ecbcb9a5904c9cef4 100644 (file)
@@ -277,14 +277,14 @@ ps2_kbd_init(struct device_def* devdef)
     // 4、控制器自检
     result = ps2_issue_cmd_wretry(PS2_CMD_SELFTEST, PS2_NO_ARG);
     if (result != PS2_RESULT_TEST_OK) {
-        kprintf(KWARN "controller self-test failed. (%x)\n", result);
+        kprintf(KWARN "controller self-test failed. (%x)", result);
         goto done;
     }
 
     // 5、设备自检(端口1自检,通常是我们的键盘)
     result = ps2_issue_cmd_wretry(PS2_CMD_SELFTEST_PORT1, PS2_NO_ARG);
     if (result != 0) {
-        kprintf(KERROR "interface test on port 1 failed. (%x)\n", result);
+        kprintf(KERROR "interface test on port 1 failed. (%x)", result);
         goto done;
     }
 
@@ -299,12 +299,6 @@ ps2_kbd_init(struct device_def* devdef)
 
     // 至此,PS/2控制器和设备已完成初始化,可以正常使用。
 
-    // 搞一个计时器,将我们的 ps2_process_cmd
-    // 挂上去。每隔5毫秒执行排在队头的命令。
-    //  为什么只执行队头的命令,而不是全部的命令?
-    //      因为我们需要保证isr尽量的简短,运行起来快速。而发送这些命令非常的耗时。
-    timer_run_ms(5, ps2_process_cmd, NULL, TIMER_MODE_PERIODIC);
-
     /*
      *   一切准备就绪后,我们才教ioapic去启用IRQ#1。
      *   至于为什么要在这里,原因是:初始化所使用的一些指令可能会导致IRQ#1的触发(因为返回码),或者是一些什么
@@ -456,6 +450,7 @@ intr_ps2_kbd_handler(const isr_param* param)
 
 #ifdef KBD_ENABLE_SPIRQ_FIX2
     if (scancode == PS2_RESULT_ACK || scancode == PS2_RESULT_NAK) {
+        ps2_process_cmd(NULL);
         return;
     }
 #endif
@@ -537,7 +532,7 @@ ps2_issue_cmd_wretry(char cmd, u16_t arg)
         c++;
     }
     if (c >= 5) {
-        kprintf(KWARN "max attempt reached.\n");
+        kprintf(KWARN "max attempt reached.");
     }
     return r;
 }
@@ -579,4 +574,4 @@ static struct device_def devrtc_i8042kbd = {
     .class = DEVCLASS(DEVIF_SOC, DEVFN_INPUT, DEV_KBD),
     .init = ps2_kbd_init
 };
-EXPORT_DEVICE(i8042_kbd, &devrtc_i8042kbd, load_timerstage);
+EXPORT_DEVICE(i8042_kbd, &devrtc_i8042kbd, load_earlystage);
index 1f6cc3d2b4b0687b2d9dc7dabad18bd135591d0b..3e4a3fefdee13be652758b2d26c966aa68467bdf 100644 (file)
@@ -35,14 +35,14 @@ pci_log_device(struct pci_device* pcidev)
     struct device_def* binddef = pcidev->binding.def;
 
     if (!binddef) {
-        kprintf("pci.%d:%d:%d, no binding\n",
+        kprintf("pci.%d:%d:%d, no binding",
                 PCILOC_BUS(loc),
                 PCILOC_DEV(loc),
                 PCILOC_FN(loc));
         return;
     }
 
-    kprintf("pci.%d:%d:%d, dev.%xh:%xh.%d, %s\n",
+    kprintf("pci.%d:%d:%d, dev.%xh:%xh.%d, %s",
             PCILOC_BUS(loc),
             PCILOC_DEV(loc),
             PCILOC_FN(loc),
@@ -98,7 +98,7 @@ pci_create_device(pciaddr_t loc, ptr_t pci_base, int devinfo)
 
 found:
     if (!pos->devdef.bind) {
-        kprintf(KERROR "pci_loc:%x, (%xh:%xh.%d) unbindable\n",
+        kprintf(KERROR "pci_loc:%x, (%xh:%xh.%d) unbindable",
                 loc,
                 pos->devdef.class.fn_grp,
                 pos->devdef.class.device,
@@ -108,7 +108,7 @@ found:
 
     int errno = pos->devdef.bind(&pos->devdef, &device->dev);
     if (errno) {
-        kprintf(KERROR "pci_loc:%x, (%xh:%xh.%d) failed, e=%d\n",
+        kprintf(KERROR "pci_loc:%x, (%xh:%xh.%d) failed, e=%d",
                 loc,
                 pos->devdef.class.fn_grp,
                 pos->devdef.class.device,
index e195f34e8099aac43a0ac3ef200fa2a3d0ef5a76..db3e700d004960281d8b000a6240e1e5f9b2ef2a 100644 (file)
@@ -132,7 +132,7 @@ apic_timer_init(struct hwtimer* timer, u32_t hertz, timer_tick_cb timer_cb)
 
     assert_msg(base_freq, "Fail to initialize timer (NOFREQ)");
 
-    kprintf(KINFO "hw: %u Hz; os: %u Hz\n", base_freq, frequency);
+    kprintf(KINFO "hw: %u Hz; os: %u Hz", base_freq, frequency);
 
     // cleanup
     isrm_ivfree(iv_timer);
diff --git a/lunaix-os/includes/klibc/stdio.h b/lunaix-os/includes/klibc/stdio.h
deleted file mode 100644 (file)
index f2c58db..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-#ifndef __LUNAIX_STDIO_H
-#define __LUNAIX_STDIO_H
-#include <stdarg.h>
-#include <stddef.h>
-
-size_t
-__ksprintf_internal(char* buffer,
-                    const char* fmt,
-                    size_t max_len,
-                    va_list vargs);
-
-size_t
-ksprintf(char* buffer, char* fmt, ...);
-size_t
-ksnprintf(char* buffer, size_t n, char* fmt, ...);
-#endif /* __LUNAIX_STDIO_H */
diff --git a/lunaix-os/includes/klibc/strfmt.h b/lunaix-os/includes/klibc/strfmt.h
new file mode 100644 (file)
index 0000000..92fd3ac
--- /dev/null
@@ -0,0 +1,13 @@
+#ifndef __LUNAIX_STRFMT_H
+#define __LUNAIX_STRFMT_H
+#include <stdarg.h>
+#include <stddef.h>
+
+size_t
+ksnprintfv(char* buffer, const char* fmt, size_t max_len, va_list vargs);
+
+size_t
+ksprintf(char* buffer, char* fmt, ...);
+size_t
+ksnprintf(char* buffer, size_t n, char* fmt, ...);
+#endif /* __LUNAIX_STRFMT_H */
index 6a3d26eacf91a0c8b54bed2ef6949e032b069652..4eea312fedc7206bbee01af4c1f8e138a0ead913 100644 (file)
@@ -2,10 +2,6 @@
 #define __LUNAIX_CLOCK_H
 
 #include <lunaix/time.h>
-
-void
-clock_init();
-
 void
 clock_walltime(datetime_t* datetime);
 
index 72bffb713b5c4ec136323ac3d4c4d7d4a6c7437e..314afd95e297948b6d3596f341ed6f770c32c819 100644 (file)
@@ -7,4 +7,8 @@
 #define weak_alias(name) __attribute__((weak, alias(name)))
 #define weak __attribute__((weak))
 #define noret __attribute__((noreturn))
+
+#define stringify(v) #v
+#define stringify__(v) stringify(v)
+
 #endif /* __LUNAIX_COMPILER_H */
index d77619e952291213245990a46617c9a7482d650f..50579aeed5d6eb580a3af755cfd98116eb785e62 100644 (file)
@@ -59,4 +59,11 @@ twifs_rm_node(struct twifs_node* node);
 struct twimap*
 twifs_mapping(struct twifs_node* parent, void* data, const char* fmt, ...);
 
+#define twimap_entry_simple(parent, name, data, read_cb)                       \
+    ({                                                                         \
+        struct twimap* map = twifs_mapping((parent), (data), (name));          \
+        map->read = (read_cb);                                                 \
+        map;                                                                   \
+    })
+
 #endif /* __LUNAIX_TWIFS_H */
index eed1da22f065dda25c0963b6c0ce6ce52dfec8a5..24233159bc608de5a5ebc57db45e18e2e893d146 100644 (file)
@@ -1,17 +1,28 @@
 #ifndef __LUNAIX_SYSLOG_H
 #define __LUNAIX_SYSLOG_H
 
+#include <lunaix/compiler.h>
 #include <stdarg.h>
 
+#define KLOG_DEBUG 0
+#define KLOG_INFO 1
+#define KLOG_WARN 2
+#define KLOG_ERROR 3
+#define KLOG_FATAL 4
+
 #define _LEVEL_INFO "0"
 #define _LEVEL_WARN "1"
 #define _LEVEL_ERROR "2"
 #define _LEVEL_DEBUG "3"
 
-#define KINFO "\x1b" _LEVEL_INFO
-#define KWARN "\x1b" _LEVEL_WARN
-#define KERROR "\x1b" _LEVEL_ERROR
-#define KDEBUG "\x1b" _LEVEL_DEBUG
+#define KMSG_LVLSTART '\x1b'
+#define KMSG_LOGLEVEL(c) ((c) - '0')
+
+#define KDEBUG "\x1b" stringify__(KLOG_DEBUG)
+#define KINFO "\x1b" stringify__(KLOG_INFO)
+#define KWARN "\x1b" stringify__(KLOG_WARN)
+#define KERROR "\x1b" stringify__(KLOG_ERROR)
+#define KFATAL "\x1b" stringify__(KLOG_FATAL)
 
 #define LOG_MODULE(module)                                                     \
     static void kprintf(const char* fmt, ...)                                  \
 void
 __kprintf(const char* component, const char* fmt, va_list args);
 
-void
-kprint_hex(const void* buffer, unsigned int size);
-
-void
-kprint_panic(const char* fmt, ...);
-
-void
-kprint_dbg(const char* fmt, ...);
-
-void
-kappendf(const char* fmt, ...);
-
 #endif /* __LUNAIX_SYSLOG_H */
index 3eee27727008917155ab72366fa70dd1e1ac2115..9d49bfcb98d95845449c4e69a8251570f0264cb8 100644 (file)
@@ -8,7 +8,7 @@
 #include <lib/crc.h>
 
 #define GPT_BLKSIZE 512
-#define LBA2OFF(lba) ((lba)*GPT_BLKSIZE)
+#define LBA2OFF(lba) ((lba) * GPT_BLKSIZE)
 #define ENT_PER_BLK (GPT_BLKSIZE / sizeof(struct gpt_entry))
 
 #define GPTSIG_LO 0x20494645UL
@@ -50,7 +50,7 @@ blkpart_parse(struct device* master, struct gpt_header* header)
           (ent->start_lba * GPT_BLKSIZE) / (u64_t)bdev->blk_size;
         u64_t elba_local = (ent->end_lba * GPT_BLKSIZE) / (u64_t)bdev->blk_size;
 
-        kprintf("%s: guid part#%d: %d..%d\n",
+        kprintf("%s: guid part#%d: %d..%d",
                 bdev->bdev_id,
                 i,
                 (u32_t)slba_local,
@@ -83,7 +83,7 @@ blkpart_probegpt(struct device* master)
     u32_t crc = gpt_hdr->hdr_cksum;
     gpt_hdr->hdr_cksum = 0;
     if (crc32b((void*)gpt_hdr, sizeof(*gpt_hdr)) != crc) {
-        kprintf(KWARN "checksum failed\n");
+        kprintf(KWARN "checksum failed");
         // FUTURE check the backup header
         return EINVAL;
     }
index c78cd26a1e522b1679c760c0c69caa2eb0d204f9..3f3c1e8ab647905375a0efb1acec730a8fbdbc98 100644 (file)
@@ -1,4 +1,4 @@
-#include <klibc/stdio.h>
+#include <klibc/strfmt.h>
 #include <klibc/string.h>
 
 #include <hal/ahci/hba.h>
@@ -295,7 +295,7 @@ block_mount(struct block_dev* bdev, devfs_exporter fs_export)
 
     errno = blkpart_probegpt(bdev->dev);
     if (errno < 0) {
-        kprintf(KERROR "Fail to parse partition table (%d)\n", errno);
+        kprintf(KERROR "Fail to parse partition table (%d)", errno);
     } else if (!errno) {
         // TODO try other PT parser...
     }
@@ -307,7 +307,7 @@ block_mount(struct block_dev* bdev, devfs_exporter fs_export)
     return errno;
 
 error:
-    kprintf(KERROR "Fail to mount block device: %s (%x)\n", bdev->name, -errno);
+    kprintf(KERROR "Fail to mount block device: %s (%x)", bdev->name, -errno);
     return errno;
 }
 
index af33a452634ed9aa39f6255a21b01cf686b49372..6a7d81136785f44d5e20bafd81aea3fec1576ae4 100644 (file)
@@ -1,7 +1,7 @@
 // FIXME Re-design needed!!
 
 #include <hal/serial.h>
-#include <klibc/stdio.h>
+#include <klibc/strfmt.h>
 #include <lunaix/syslog.h>
 #include <sdbg/gdbstub.h>
 #include <sdbg/lsdbg.h>
index 0cee498f3cb6d4f26fe1bcd0ceb1aa907e894ac7..62ceb0ef723409d46c56c62ae88a5d65e761f48e 100644 (file)
@@ -113,11 +113,11 @@ trace_printstack_of(ptr_t fp)
     int n = trace_walkback(tbs, fp, NB_TRACEBACK, &fp);
 
     if (fp) {
-        kprintf(KDEBUG "...<truncated>\n");
+        kprintf(KDEBUG "...<truncated>");
     }
 
     for (int i = 0; i < n; i++) {
-        kprintf(KDEBUG "%p: %s\n", tbs[i].pc, tbs[i].symbol);
+        kprintf(KDEBUG "%p: %s", tbs[i].pc, tbs[i].symbol);
     }
 }
 
@@ -133,11 +133,11 @@ trace_printswctx(const isr_param* p, char* direction)
 
     struct ksym_entry* sym = trace_sym_lookup(p->execp->eip);
 
-    kprintf(KDEBUG ">> (sw:%s) iv:%d, errno:%p <<\n",
+    kprintf(KDEBUG ">> (sw:%s) iv:%d, errno:%p <<",
             direction,
             p->execp->vector,
             p->execp->err_code);
-    kprintf(KDEBUG "%p:%s\n", p->execp->eip, ksym_getstr(sym));
+    kprintf(KDEBUG "%p:%s", p->execp->eip, ksym_getstr(sym));
 }
 
 void
@@ -147,7 +147,6 @@ trace_printstack_isr(const isr_param* isrm)
     ptr_t fp = cpu_get_fp();
     int prev_fromusr = 0;
 
-    kprintf(KDEBUG "\n");
     kprintf(KDEBUG "stack trace (pid=%d)\n", __current->pid);
 
     trace_printstack_of(fp);
@@ -170,5 +169,4 @@ trace_printstack_isr(const isr_param* isrm)
 
         p = p->execp->saved_prev_ctx;
     }
-    kprintf(KDEBUG "\n");
 }
\ No newline at end of file
index 1c9ee4c7135cca4e0e5e9deed8005ed928884536..41238620b64caab78863470672d8e8b6c7fb8d73 100644 (file)
@@ -4,7 +4,7 @@
 
 #include <lib/hash.h>
 
-#include <klibc/stdio.h>
+#include <klibc/strfmt.h>
 
 static DECLARE_HASHTABLE(dev_registry, 32);
 static DECLARE_HASHTABLE(dev_byif, 8);
index b25a9c203594bab14d2d2892ca04bbe39d5e5796..87657551eb50a0e5a3ac42a257d712ad8e011344 100644 (file)
@@ -8,7 +8,7 @@
 #include <lunaix/syscall.h>
 #include <lunaix/syscall_utils.h>
 
-#include <klibc/stdio.h>
+#include <klibc/strfmt.h>
 #include <klibc/string.h>
 
 static DEFINE_LLIST(root_list);
@@ -21,7 +21,7 @@ void
 device_setname_vargs(struct device* dev, char* fmt, va_list args)
 {
     size_t strlen =
-      __ksprintf_internal(dev->name_val, fmt, DEVICE_NAME_SIZE, args);
+      ksnprintfv(dev->name_val, fmt, DEVICE_NAME_SIZE, args);
 
     dev->name = HSTR(dev->name_val, strlen);
 
index 629a480f615d2d034c1a1aae2e0fe3c3abe5292e..97ef359f40351c22acb2f8937bc27f82cd3c37b0 100644 (file)
@@ -170,7 +170,7 @@ vfs_mount_at(const char* fs_name,
             goto cleanup;
         }
 
-        kprintf("mount: dev=%s, fs=%s, mode=%d\n", dev_name, fs_name, options);
+        kprintf("mount: dev=%s, fs=%s, mode=%d", dev_name, fs_name, options);
 
         mnt_point->mnt->flags = options;
     } else {
@@ -180,7 +180,7 @@ vfs_mount_at(const char* fs_name,
     return errno;
 
 cleanup:
-    kprintf(KERROR "mount: dev=%s, fs=%s, mode=%d, err=%d\n",
+    kprintf(KERROR "mount: dev=%s, fs=%s, mode=%d, err=%d",
             dev_name,
             fs_name,
             options,
index 2ddc9b1317176f1cfbeda18ae8ac26411fb2934a..77b4dff6eca146790e333886a4cadcf5a21586a5 100644 (file)
@@ -23,7 +23,7 @@ probe_boot_medium()
         int errno =
           pos->ops.read(pos, (void*)volp, ISO9660_READ_OFF, ISO9660_BLKSZ);
         if (errno < 0) {
-            kprintf(KINFO "failed %xh:%xh, /dev/%s\n",
+            kprintf(KINFO "failed %xh:%xh, /dev/%s",
                     pos->ident.fn_grp,
                     pos->ident.unique,
                     pos->name.value);
@@ -36,7 +36,7 @@ probe_boot_medium()
         }
 
         if (*(u32_t*)volp->sys_id == LUNAIX_ID) {
-            kprintf(KINFO "%xh:%xh, /dev/%s, %s\n",
+            kprintf(KINFO "%xh:%xh, /dev/%s, %s",
                     pos->ident.fn_grp,
                     pos->ident.unique,
                     pos->name.value,
index c243fcc073ed6607bd0488c0451551ade9430832..704a103f270b750f130430b4035d27960b8ff3d1 100644 (file)
@@ -8,7 +8,7 @@
  * @copyright Copyright (c) 2022
  *
  */
-#include <klibc/stdio.h>
+#include <klibc/strfmt.h>
 #include <klibc/string.h>
 #include <lunaix/clock.h>
 #include <lunaix/fs.h>
@@ -192,7 +192,7 @@ struct twifs_node*
 twifs_file_node_vargs(struct twifs_node* parent, const char* fmt, va_list args)
 {
     char buf[VFS_NAME_MAXLEN];
-    size_t len = __ksprintf_internal(buf, fmt, VFS_NAME_MAXLEN, args);
+    size_t len = ksnprintfv(buf, fmt, VFS_NAME_MAXLEN, args);
 
     return __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFSEQDEV);
 }
@@ -217,7 +217,7 @@ twifs_dir_node(struct twifs_node* parent, const char* fmt, ...)
     va_start(args, fmt);
 
     char buf[VFS_NAME_MAXLEN];
-    size_t len = __ksprintf_internal(buf, fmt, VFS_NAME_MAXLEN, args);
+    size_t len = ksnprintfv(buf, fmt, VFS_NAME_MAXLEN, args);
     struct twifs_node* twi_node =
       __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFDIR);
 
index 2371eb8b257b63b1991986e5f04a954ee324798f..1021db077e924f047cc0a203f2f48eac3bd53cb5 100644 (file)
@@ -3,7 +3,7 @@
 #include <lunaix/mm/valloc.h>
 #include <lunaix/spike.h>
 
-#include <klibc/stdio.h>
+#include <klibc/strfmt.h>
 #include <klibc/string.h>
 
 #define TWIMAP_BUFFER_SIZE 4096
@@ -80,7 +80,7 @@ twimap_printf(struct twimap* mapping, const char* fmt, ...)
     char* buf = mapping->buffer + mapping->size_acc;
 
     mapping->size_acc +=
-      __ksprintf_internal(buf, fmt, TWIMAP_BUFFER_SIZE, args) - 1;
+      ksnprintfv(buf, fmt, TWIMAP_BUFFER_SIZE, args) - 1;
 
     va_end(args);
 }
index cb31ca1af1ea60ef4324b1c6f0d06ebfd70d8095..0510c0c53de0d7be781c04374eb4b0baa3ba3159 100644 (file)
@@ -25,7 +25,7 @@
 #include <sys/interrupts.h>
 #include <sys/mm/mempart.h>
 
-#include <klibc/stdio.h>
+#include <klibc/strfmt.h>
 #include <klibc/string.h>
 
 extern void
@@ -62,26 +62,35 @@ kernel_bootstrap(struct boot_handoff* bhctx)
     /* Get platform configuration */
     acpi_init();
 
-    /* Let's get fs online as soon as possible, as things rely on them */
-    vfs_init();
-    fsm_init();
-
     /* Get intc online, this is the cornerstone when initing devices */
     intc_init();
 
+    /*
+        TODO autoload these init function that do not have dependency between
+       them
+    */
+
+    /* Let's get fs online as soon as possible, as things rely on them */
+    vfs_init();
+    fsm_init();
     input_init();
+    block_init();
+    sched_init();
+
     device_earlystage();
 
     /* System timing and clock support */
-    clock_init();
-    timer_init();
+    /*
+        FIXME we must get timer as earlier as possible
 
-    device_timerstage();
+        A decoupling between rtc and general device sub-sys is needed.
+        Otherwise we timer can only be loaded after device_earlystage.
 
-    block_init();
+        We need a dedicated timer&clock subsystem
+    */
+    timer_init();
 
     /* the bare metal are now happy, let's get software over with */
-    sched_init();
 
     int errno = 0;
     if ((errno = vfs_mount_root("ramfs", NULL))) {
diff --git a/lunaix-os/kernel/kprint/kp_records.c b/lunaix-os/kernel/kprint/kp_records.c
new file mode 100644 (file)
index 0000000..d323bc1
--- /dev/null
@@ -0,0 +1,54 @@
+#include <klibc/string.h>
+#include <lunaix/clock.h>
+#include <lunaix/mm/valloc.h>
+#include <lunaix/spike.h>
+#include <lunaix/syslog.h>
+
+#include "kp_records.h"
+
+struct kp_records*
+kp_rec_create(int max_recs)
+{
+    struct kp_records* recs = (struct kp_records*)valloc(KP_RECS_SIZE);
+
+    *recs = (struct kp_records){ .max_recs = max_recs,
+                                 .log_lvl = KLOG_DEBUG,
+                                 .kp_ent_wp = &recs->kp_ents.ents };
+
+    llist_init_head(&recs->kp_ents.ents);
+
+    return recs;
+}
+
+static inline int
+kp_recs_full(struct kp_records* recs)
+{
+    return recs->cur_recs >= recs->max_recs;
+}
+
+void
+kp_rec_put(struct kp_records* recs, int lvl, char* content, size_t len)
+{
+    assert(len < 256);
+
+    struct kp_entry* ent;
+    if (!kp_recs_full(recs)) {
+        assert(recs->kp_ent_wp == &recs->kp_ents.ents);
+
+        ent = (struct kp_entry*)vzalloc(KP_ENT_SIZE);
+        llist_append(&recs->kp_ents.ents, &ent->ents);
+        recs->cur_recs++;
+    } else {
+        recs->kp_ent_wp = recs->kp_ent_wp->next;
+        ent = container_of(recs->kp_ent_wp, struct kp_entry, ents);
+    }
+
+    vfree_safe(ent->content);
+
+    char* _content = valloc(len);
+    memcpy(_content, content, len);
+
+    ent->lvl = lvl;
+    ent->content = _content;
+    ent->time = clock_systime();
+}
\ No newline at end of file
diff --git a/lunaix-os/kernel/kprint/kp_records.h b/lunaix-os/kernel/kprint/kp_records.h
new file mode 100644 (file)
index 0000000..bf300f9
--- /dev/null
@@ -0,0 +1,34 @@
+#ifndef __LUNAIX_KP_RECORDS_H
+#define __LUNAIX_KP_RECORDS_H
+
+#include <lunaix/ds/llist.h>
+#include <lunaix/time.h>
+
+struct kp_entry
+{
+    struct llist_header ents;
+    int lvl;
+    time_t time;
+    char* content;
+};
+#define KP_ENT_SIZE sizeof(struct kp_entry)
+
+struct kp_records
+{
+    struct kp_entry kp_ents;
+
+    struct llist_header* kp_ent_wp;
+
+    int max_recs;
+    int cur_recs;
+    int log_lvl;
+};
+#define KP_RECS_SIZE sizeof(struct kp_records)
+
+struct kp_records*
+kp_rec_create(int max_recs);
+
+void
+kp_rec_put(struct kp_records*, int lvl, char* content, size_t len);
+
+#endif /* __LUNAIX_KP_RECORDS_H */
diff --git a/lunaix-os/kernel/kprint/kprintf.c b/lunaix-os/kernel/kprint/kprintf.c
new file mode 100644 (file)
index 0000000..c3bd04c
--- /dev/null
@@ -0,0 +1,81 @@
+#include <lunaix/fs/twifs.h>
+#include <lunaix/fs/twimap.h>
+#include <lunaix/syscall.h>
+#include <lunaix/syslog.h>
+
+#include <klibc/strfmt.h>
+
+#include "kp_records.h"
+
+#define MAX_BUFSZ 512
+#define MAX_BUFSZ_HLF 256
+#define MAX_KPENT_NUM 1024
+
+static char tmp_buf[MAX_BUFSZ];
+
+static struct kp_records kprecs = {
+    .kp_ents = { .ents = { .next = &kprecs.kp_ents.ents,
+                           .prev = &kprecs.kp_ents.ents } },
+    .max_recs = MAX_KPENT_NUM,
+    .kp_ent_wp = &kprecs.kp_ents.ents
+};
+
+static char*
+shift_level(const char* str, int* level)
+{
+    if (str[0] == KMSG_LVLSTART) {
+        *level = KMSG_LOGLEVEL(str[1]);
+
+        return str += 2;
+    }
+
+    *level = KLOG_INFO;
+    return str;
+}
+
+static void
+__kprintf_level(const char* component, int level, const char* fmt, va_list args)
+{
+    char* buf = &tmp_buf[MAX_BUFSZ_HLF];
+    ksnprintf(buf, MAX_BUFSZ_HLF, "%s: %s", component, fmt);
+
+    size_t sz = ksnprintfv(tmp_buf, buf, MAX_BUFSZ_HLF, args);
+    kp_rec_put(&kprecs, level, tmp_buf, sz);
+}
+
+void
+__kprintf(const char* component, const char* fmt, va_list args)
+{
+    int level;
+    fmt = shift_level(fmt, &level);
+
+    __kprintf_level(component, level, fmt, args);
+}
+
+static void
+__twimap_kprintf_read(struct twimap* map)
+{
+    struct kp_records* kprecs = twimap_data(map, struct kp_records*);
+
+    /*
+        XXX we can foreach all records in a single twimap read call,
+            as records is monotonic increasing by design.
+    */
+    struct kp_entry *pos, *n;
+    llist_for_each(pos, n, kprecs->kp_ent_wp, ents)
+    {
+        twimap_printf(map, "[%05d] %s\n", pos->time, pos->content);
+    }
+}
+
+static void
+kprintf_mapping_init()
+{
+    twimap_entry_simple(NULL, "kmsg", &kprecs, __twimap_kprintf_read);
+}
+EXPORT_TWIFS_PLUGIN(kprintf, kprintf_mapping_init);
+
+__DEFINE_LXSYSCALL3(void, syslog, int, level, const char*, fmt, va_list, args)
+{
+    __kprintf_level("syslog", level, fmt, args);
+}
\ No newline at end of file
diff --git a/lunaix-os/kernel/kprintf.c b/lunaix-os/kernel/kprintf.c
deleted file mode 100644 (file)
index 451abe0..0000000
+++ /dev/null
@@ -1,152 +0,0 @@
-#include <klibc/stdio.h>
-#include <lunaix/lxconsole.h>
-#include <lunaix/spike.h>
-#include <lunaix/syscall.h>
-#include <lunaix/syslog.h>
-#include <lunaix/tty/tty.h>
-
-#define MAX_KPRINTF_BUF_SIZE 512
-#define MAX_XFMT_SIZE 512
-
-static char* log_prefix[] = { "- ", "W ", "E ", "D " };
-static char* color_code[] = { "", "\033[6;0m", "\033[12;0m", "\033[9;0m" };
-
-void 
-__kprintf_internal(const char* component,
-                   int log_level,
-                   const char* prefix,
-                   const char* fmt,
-                   va_list args)
-{
-    char buf[MAX_KPRINTF_BUF_SIZE];
-    char expanded_fmt[MAX_XFMT_SIZE];
-    char* color = color_code[log_level];
-
-    if (component) {
-        ksnprintf(expanded_fmt,
-                  MAX_XFMT_SIZE,
-                  "%s%s%s: %s\033[39;49m",
-                  color,
-                  prefix,
-                  component,
-                  fmt);
-    } else {
-        ksnprintf(
-          expanded_fmt, MAX_XFMT_SIZE, "%s%s%s\033[39;49m", color, prefix, fmt);
-    }
-
-    __ksprintf_internal(buf, expanded_fmt, MAX_KPRINTF_BUF_SIZE, args);
-    console_write_str(buf);
-}
-
-char*
-__get_loglevel(char* fmt, int* log_level_out)
-{
-    char l = '0';
-    if (*fmt == '\x1b') {
-        l = *(++fmt);
-        fmt++;
-    }
-    l -= '0';
-
-    if (l > 3) {
-        l = 0;
-    }
-
-    *log_level_out = (int)l;
-    return fmt;
-}
-
-void
-kappendf(const char* fmt, ...)
-{
-    char buf[MAX_KPRINTF_BUF_SIZE];
-
-    va_list args;
-    va_start(args, fmt);
-
-    int log_level;
-    fmt = __get_loglevel(fmt, &log_level);
-
-    __kprintf_internal(NULL, log_level, "", fmt, args);
-
-    va_end(args);
-}
-
-void
-__kprintf(const char* component, const char* fmt, va_list args)
-{
-    if (!fmt)
-        return;
-
-    int log_level;
-    fmt = __get_loglevel(fmt, &log_level);
-    __kprintf_internal(component, log_level, log_prefix[log_level], fmt, args);
-}
-
-void
-kprint_panic(const char* fmt, ...)
-{
-    char buf[MAX_KPRINTF_BUF_SIZE];
-    va_list args;
-    va_start(args, fmt);
-
-    tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_RED);
-    tty_clear_line(24);
-
-    __ksprintf_internal(buf, fmt, MAX_KPRINTF_BUF_SIZE, args);
-    tty_put_str_at(buf, 0, 24);
-
-    va_end(args);
-
-    spin();
-}
-
-void
-kprint_dbg(const char* fmt, ...)
-{
-    char buf[MAX_KPRINTF_BUF_SIZE];
-    va_list args;
-    va_start(args, fmt);
-
-    tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_MAGENTA);
-    tty_clear_line(24);
-
-    __ksprintf_internal(buf, fmt, MAX_KPRINTF_BUF_SIZE, args);
-    tty_put_str_at(buf, 0, 24);
-
-    va_end(args);
-
-    tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
-}
-
-void
-kprint_hex(const void* buffer, unsigned int size)
-{
-    unsigned char* data = (unsigned char*)buffer;
-    char buf[16];
-    char ch_cache[16];
-    unsigned int ptr = 0;
-    int i;
-
-    ch_cache[0] = '|';
-    ch_cache[1] = ' ';
-    while (size) {
-        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 : '.';
-            ksnprintf(buf, 64, "%.2x  ", c);
-            console_write_str(buf);
-        }
-        ch_cache[2 + i] = '\0';
-        console_write_str(ch_cache);
-        console_write_char('\n');
-    }
-}
-
-__DEFINE_LXSYSCALL3(void, syslog, int, level, const char*, fmt, va_list, args)
-{
-    __kprintf_internal("syslog", level, "", fmt, args);
-}
\ No newline at end of file
index 93cc0207ef4819c648908eb2337e7d746b0ccee3..ca1b4cadf39f03b8d90b28f4c3318dca587956bb 100644 (file)
@@ -92,10 +92,6 @@ __proc0()
 void
 init_platform()
 {
-    kprintf(KINFO "\033[11;0mLunaixOS (gcc v%s, %s)\033[39;49m\n",
-            __VERSION__,
-            __TIME__);
-
     device_poststage();
 
     twifs_register_plugins();
index 370a5425c8636f21f3a6d05ce8491dc840433b29..2a4bdb452c8314fbda85776c56fdaaf6fe2bea58 100644 (file)
@@ -160,7 +160,7 @@ init_proc_user_space(struct proc_info* pcb)
 
     int status = 0;
     if ((status = mem_map(NULL, &mapped, USR_STACK, NULL, &param))) {
-        kprint_panic("fail to alloc user stack: %d", status);
+        kprintf(KFATAL "fail to alloc user stack: %d", status);
     }
 
     mapped->region_copied = __stack_copied;
index e11068f1a28511f0f23129bcf365713ff3fefb58..70afe1349fd5cf808e5e650260ac690c91415e86 100644 (file)
@@ -4,7 +4,7 @@
 #include <lunaix/process.h>
 #include <lunaix/sched.h>
 
-#include <klibc/stdio.h>
+#include <klibc/strfmt.h>
 #include <klibc/string.h>
 
 #include <usr/lunaix/dirent_defs.h>
index cb1bc1f2777dc485bf4f46ee680c5dc45d2e4061..63414c25e4bac73508e6a16c484477572464cfdd 100644 (file)
@@ -1,4 +1,4 @@
-#include <klibc/stdio.h>
+#include <klibc/strfmt.h>
 #include <lunaix/spike.h>
 #include <sys/interrupts.h>
 
@@ -30,7 +30,7 @@ panickf(const char* fmt, ...)
 {
     va_list args;
     va_start(args, fmt);
-    __ksprintf_internal(buffer, fmt, 1024, args);
+    ksnprintfv(buffer, fmt, 1024, args);
     va_end(args);
 
     asm("int %0" ::"i"(LUNAIX_SYS_PANIC), "D"(buffer));
index ef557c784e8dd47895a4f1148874d9f1cd1e02aa..3770dd00ce3088aa4dfff566768aa2615ff69a04 100644 (file)
@@ -51,12 +51,6 @@ clock_build_mapping()
 }
 EXPORT_TWIFS_PLUGIN(sys_clock, clock_build_mapping);
 
-void
-clock_init()
-{
-    // hwrtc_init();
-}
-
 time_t
 clock_unixtime()
 {
@@ -68,6 +62,10 @@ clock_unixtime()
 time_t
 clock_systime()
 {
+    if (!current_timer) {
+        return 0;
+    }
+
     ticks_t t = hwtimer_current_systicks();
     return t / current_timer->running_freq;
 }
similarity index 95%
rename from lunaix-os/libs/klibc/stdio/ksprintf.c
rename to lunaix-os/libs/klibc/ksprintf.c
index 24591dc43552dcd17b4853d24f6ced402901ff0f..69c9253540d289f1b3850483f657dd871a0614f6 100644 (file)
@@ -1,6 +1,6 @@
 #define __LUNAIX_LIBC
-#include <klibc/stdio.h>
 #include <klibc/stdlib.h>
+#include <klibc/strfmt.h>
 #include <klibc/string.h>
 #include <lunaix/types.h>
 
@@ -20,10 +20,7 @@ static const char flag_chars[] = "#0- +";
 #define FLAG_CAPS (1 << 9)
 
 size_t
-__ksprintf_internal(char* buffer,
-                    const char* fmt,
-                    size_t max_len,
-                    va_list vargs)
+ksnprintfv(char* buffer, const 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 :)
@@ -206,7 +203,7 @@ ksprintf(char* buffer, char* fmt, ...)
 {
     va_list args;
     va_start(args, fmt);
-    size_t len = __ksprintf_internal(buffer, fmt, 0, args);
+    size_t len = ksnprintfv(buffer, fmt, 0, args);
     va_end(args);
     return len;
 }
@@ -216,7 +213,7 @@ ksnprintf(char* buffer, size_t n, char* fmt, ...)
 {
     va_list args;
     va_start(args, fmt);
-    size_t len = __ksprintf_internal(buffer, fmt, n, args);
+    size_t len = ksnprintfv(buffer, fmt, n, args);
     va_end(args);
     return len;
 }
\ No newline at end of file
index c0699391d79cb6558e47b64e1e76f7d016c30c84..27a50201ac6f63948944c7082a9c73f00f8d1c42 100644 (file)
@@ -2,4 +2,20 @@ ENTRY(_start)
 
 SECTIONS {
     . = 0x400000;
-}
\ No newline at end of file
+
+    .text : {
+        *(.text)
+    }
+
+    .data : {
+        *(.data)
+    }
+
+    .ro.data : {
+        *(.ro.data)
+    }
+
+    .bss : {
+        *(.bss)
+    }
+}