From: Minep Date: Fri, 3 Nov 2023 19:33:53 +0000 (+0000) Subject: feat: kprintf now goes into dedicated pseudo-dev rather than flooding the framebuffer X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/commitdiff_plain/8c06c883e7b13c115d5ff207f79d4b68fccd5ad6?hp=0f73e6cc9945f9b4a074bb62b9708d1751fa3723 feat: kprintf now goes into dedicated pseudo-dev rather than flooding the framebuffer refactor: a better kprintf buffer design that can carry more information and simplify interface signature chores: some housekeeping stuff --- diff --git a/lunaix-os/arch/i386/exceptions/interrupts.c b/lunaix-os/arch/i386/exceptions/interrupts.c index 5d4787a..84a29c1 100644 --- a/lunaix-os/arch/i386/exceptions/interrupts.c +++ b/lunaix-os/arch/i386/exceptions/interrupts.c @@ -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: diff --git a/lunaix-os/arch/i386/exceptions/intr_routines.c b/lunaix-os/arch/i386/exceptions/intr_routines.c index d0b0d49..33b33c8 100644 --- a/lunaix-os/arch/i386/exceptions/intr_routines.c +++ b/lunaix-os/arch/i386/exceptions/intr_routines.c @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include @@ -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); } diff --git a/lunaix-os/arch/i386/hal/apic.c b/lunaix-os/arch/i386/hal/apic.c index 4323ac8..458bf03 100644 --- a/lunaix-os/arch/i386/hal/apic.c +++ b/lunaix-os/arch/i386/hal/apic.c @@ -9,8 +9,8 @@ * */ -#include #include +#include #include #include @@ -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); diff --git a/lunaix-os/arch/i386/mm/pfault.c b/lunaix-os/arch/i386/mm/pfault.c index 2a2c6f3..2533d0b 100644 --- a/lunaix-os/arch/i386/mm/pfault.c +++ b/lunaix-os/arch/i386/mm/pfault.c @@ -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, diff --git a/lunaix-os/hal/acpi/acpi.c b/lunaix-os/hal/acpi/acpi.c index 7ad3f06..67915fa 100644 --- a/lunaix-os/hal/acpi/acpi.c +++ b/lunaix-os/hal/acpi/acpi.c @@ -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; } diff --git a/lunaix-os/hal/ahci/ahci.c b/lunaix-os/hal/ahci/ahci.c index 61ea95b..33dc87e 100644 --- a/lunaix-os/hal/ahci/ahci.c +++ b/lunaix-os/hal/ahci/ahci.c @@ -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, diff --git a/lunaix-os/hal/char/ps2kbd.c b/lunaix-os/hal/char/ps2kbd.c index c86350b..5cbbae8 100644 --- a/lunaix-os/hal/char/ps2kbd.c +++ b/lunaix-os/hal/char/ps2kbd.c @@ -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); diff --git a/lunaix-os/hal/pci.c b/lunaix-os/hal/pci.c index 1f6cc3d..3e4a3fe 100644 --- a/lunaix-os/hal/pci.c +++ b/lunaix-os/hal/pci.c @@ -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, diff --git a/lunaix-os/hal/timer/apic_timer.c b/lunaix-os/hal/timer/apic_timer.c index e195f34..db3e700 100644 --- a/lunaix-os/hal/timer/apic_timer.c +++ b/lunaix-os/hal/timer/apic_timer.c @@ -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 index f2c58db..0000000 --- a/lunaix-os/includes/klibc/stdio.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef __LUNAIX_STDIO_H -#define __LUNAIX_STDIO_H -#include -#include - -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 index 0000000..92fd3ac --- /dev/null +++ b/lunaix-os/includes/klibc/strfmt.h @@ -0,0 +1,13 @@ +#ifndef __LUNAIX_STRFMT_H +#define __LUNAIX_STRFMT_H +#include +#include + +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 */ diff --git a/lunaix-os/includes/lunaix/clock.h b/lunaix-os/includes/lunaix/clock.h index 6a3d26e..4eea312 100644 --- a/lunaix-os/includes/lunaix/clock.h +++ b/lunaix-os/includes/lunaix/clock.h @@ -2,10 +2,6 @@ #define __LUNAIX_CLOCK_H #include - -void -clock_init(); - void clock_walltime(datetime_t* datetime); diff --git a/lunaix-os/includes/lunaix/compiler.h b/lunaix-os/includes/lunaix/compiler.h index 72bffb7..314afd9 100644 --- a/lunaix-os/includes/lunaix/compiler.h +++ b/lunaix-os/includes/lunaix/compiler.h @@ -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 */ diff --git a/lunaix-os/includes/lunaix/fs/twifs.h b/lunaix-os/includes/lunaix/fs/twifs.h index d77619e..50579ae 100644 --- a/lunaix-os/includes/lunaix/fs/twifs.h +++ b/lunaix-os/includes/lunaix/fs/twifs.h @@ -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 */ diff --git a/lunaix-os/includes/lunaix/syslog.h b/lunaix-os/includes/lunaix/syslog.h index eed1da2..2423315 100644 --- a/lunaix-os/includes/lunaix/syslog.h +++ b/lunaix-os/includes/lunaix/syslog.h @@ -1,17 +1,28 @@ #ifndef __LUNAIX_SYSLOG_H #define __LUNAIX_SYSLOG_H +#include #include +#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, ...) \ @@ -25,16 +36,4 @@ 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 */ diff --git a/lunaix-os/kernel/block/blkpart_gpt.c b/lunaix-os/kernel/block/blkpart_gpt.c index 3eee277..9d49bfc 100644 --- a/lunaix-os/kernel/block/blkpart_gpt.c +++ b/lunaix-os/kernel/block/blkpart_gpt.c @@ -8,7 +8,7 @@ #include #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; } diff --git a/lunaix-os/kernel/block/block.c b/lunaix-os/kernel/block/block.c index c78cd26..3f3c1e8 100644 --- a/lunaix-os/kernel/block/block.c +++ b/lunaix-os/kernel/block/block.c @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -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; } diff --git a/lunaix-os/kernel/debug/sdbg.c b/lunaix-os/kernel/debug/sdbg.c index af33a45..6a7d811 100644 --- a/lunaix-os/kernel/debug/sdbg.c +++ b/lunaix-os/kernel/debug/sdbg.c @@ -1,7 +1,7 @@ // FIXME Re-design needed!! #include -#include +#include #include #include #include diff --git a/lunaix-os/kernel/debug/trace.c b/lunaix-os/kernel/debug/trace.c index 0cee498..62ceb0e 100644 --- a/lunaix-os/kernel/debug/trace.c +++ b/lunaix-os/kernel/debug/trace.c @@ -113,11 +113,11 @@ trace_printstack_of(ptr_t fp) int n = trace_walkback(tbs, fp, NB_TRACEBACK, &fp); if (fp) { - kprintf(KDEBUG "...\n"); + kprintf(KDEBUG "..."); } 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 diff --git a/lunaix-os/kernel/device/devdb.c b/lunaix-os/kernel/device/devdb.c index 1c9ee4c..4123862 100644 --- a/lunaix-os/kernel/device/devdb.c +++ b/lunaix-os/kernel/device/devdb.c @@ -4,7 +4,7 @@ #include -#include +#include static DECLARE_HASHTABLE(dev_registry, 32); static DECLARE_HASHTABLE(dev_byif, 8); diff --git a/lunaix-os/kernel/device/device.c b/lunaix-os/kernel/device/device.c index b25a9c2..8765755 100644 --- a/lunaix-os/kernel/device/device.c +++ b/lunaix-os/kernel/device/device.c @@ -8,7 +8,7 @@ #include #include -#include +#include #include 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); diff --git a/lunaix-os/kernel/fs/mount.c b/lunaix-os/kernel/fs/mount.c index 629a480..97ef359 100644 --- a/lunaix-os/kernel/fs/mount.c +++ b/lunaix-os/kernel/fs/mount.c @@ -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, diff --git a/lunaix-os/kernel/fs/probe_boot.c b/lunaix-os/kernel/fs/probe_boot.c index 2ddc9b1..77b4dff 100644 --- a/lunaix-os/kernel/fs/probe_boot.c +++ b/lunaix-os/kernel/fs/probe_boot.c @@ -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, diff --git a/lunaix-os/kernel/fs/twifs/twifs.c b/lunaix-os/kernel/fs/twifs/twifs.c index c243fcc..704a103 100644 --- a/lunaix-os/kernel/fs/twifs/twifs.c +++ b/lunaix-os/kernel/fs/twifs/twifs.c @@ -8,7 +8,7 @@ * @copyright Copyright (c) 2022 * */ -#include +#include #include #include #include @@ -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); diff --git a/lunaix-os/kernel/fs/twimap.c b/lunaix-os/kernel/fs/twimap.c index 2371eb8..1021db0 100644 --- a/lunaix-os/kernel/fs/twimap.c +++ b/lunaix-os/kernel/fs/twimap.c @@ -3,7 +3,7 @@ #include #include -#include +#include #include #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); } diff --git a/lunaix-os/kernel/kinit.c b/lunaix-os/kernel/kinit.c index cb31ca1..0510c0c 100644 --- a/lunaix-os/kernel/kinit.c +++ b/lunaix-os/kernel/kinit.c @@ -25,7 +25,7 @@ #include #include -#include +#include #include 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 index 0000000..d323bc1 --- /dev/null +++ b/lunaix-os/kernel/kprint/kp_records.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include + +#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 index 0000000..bf300f9 --- /dev/null +++ b/lunaix-os/kernel/kprint/kp_records.h @@ -0,0 +1,34 @@ +#ifndef __LUNAIX_KP_RECORDS_H +#define __LUNAIX_KP_RECORDS_H + +#include +#include + +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 index 0000000..c3bd04c --- /dev/null +++ b/lunaix-os/kernel/kprint/kprintf.c @@ -0,0 +1,81 @@ +#include +#include +#include +#include + +#include + +#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 index 451abe0..0000000 --- a/lunaix-os/kernel/kprintf.c +++ /dev/null @@ -1,152 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#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 diff --git a/lunaix-os/kernel/proc0.c b/lunaix-os/kernel/proc0.c index 93cc020..ca1b4ca 100644 --- a/lunaix-os/kernel/proc0.c +++ b/lunaix-os/kernel/proc0.c @@ -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(); diff --git a/lunaix-os/kernel/process/process.c b/lunaix-os/kernel/process/process.c index 370a542..2a4bdb4 100644 --- a/lunaix-os/kernel/process/process.c +++ b/lunaix-os/kernel/process/process.c @@ -160,7 +160,7 @@ init_proc_user_space(struct proc_info* pcb) int status = 0; if ((status = mem_map(NULL, &mapped, USR_STACK, NULL, ¶m))) { - kprint_panic("fail to alloc user stack: %d", status); + kprintf(KFATAL "fail to alloc user stack: %d", status); } mapped->region_copied = __stack_copied; diff --git a/lunaix-os/kernel/process/taskfs.c b/lunaix-os/kernel/process/taskfs.c index e11068f..70afe13 100644 --- a/lunaix-os/kernel/process/taskfs.c +++ b/lunaix-os/kernel/process/taskfs.c @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include diff --git a/lunaix-os/kernel/spike.c b/lunaix-os/kernel/spike.c index cb1bc1f..63414c2 100644 --- a/lunaix-os/kernel/spike.c +++ b/lunaix-os/kernel/spike.c @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -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)); diff --git a/lunaix-os/kernel/time/clock.c b/lunaix-os/kernel/time/clock.c index ef557c7..3770dd0 100644 --- a/lunaix-os/kernel/time/clock.c +++ b/lunaix-os/kernel/time/clock.c @@ -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; } diff --git a/lunaix-os/libs/klibc/stdio/ksprintf.c b/lunaix-os/libs/klibc/ksprintf.c similarity index 95% rename from lunaix-os/libs/klibc/stdio/ksprintf.c rename to lunaix-os/libs/klibc/ksprintf.c index 24591dc..69c9253 100644 --- a/lunaix-os/libs/klibc/stdio/ksprintf.c +++ b/lunaix-os/libs/klibc/ksprintf.c @@ -1,6 +1,6 @@ #define __LUNAIX_LIBC -#include #include +#include #include #include @@ -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 diff --git a/lunaix-os/usr/link-usr.ld b/lunaix-os/usr/link-usr.ld index c069939..27a5020 100644 --- a/lunaix-os/usr/link-usr.ld +++ b/lunaix-os/usr/link-usr.ld @@ -2,4 +2,20 @@ ENTRY(_start) SECTIONS { . = 0x400000; -} \ No newline at end of file + + .text : { + *(.text) + } + + .data : { + *(.data) + } + + .ro.data : { + *(.ro.data) + } + + .bss : { + *(.bss) + } +}