refactor: a better kprintf buffer design that can carry more information and simplify interface signature
chores: some housekeeping stuff
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:
#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>
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);
}
*
*/
-#include <sys/cpu.h>
#include <hal/intc.h>
+#include <sys/cpu.h>
#include <sys/apic.h>
#include <sys/interrupts.h>
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);
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);
}
if (errno < 0) {
- kprintf(KERROR "fail to populate page (%d)\n", errno);
+ kprintf(KERROR "fail to populate page (%d)", errno);
goto segv_term;
}
;
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,
(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:
}
}
- kprintf(KINFO "ACPI: %s\n", ctx->oem_id);
-
return 0;
}
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,
// 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;
}
// 至此,PS/2控制器和设备已完成初始化,可以正常使用。
- // 搞一个计时器,将我们的 ps2_process_cmd
- // 挂上去。每隔5毫秒执行排在队头的命令。
- // 为什么只执行队头的命令,而不是全部的命令?
- // 因为我们需要保证isr尽量的简短,运行起来快速。而发送这些命令非常的耗时。
- timer_run_ms(5, ps2_process_cmd, NULL, TIMER_MODE_PERIODIC);
-
/*
* 一切准备就绪后,我们才教ioapic去启用IRQ#1。
* 至于为什么要在这里,原因是:初始化所使用的一些指令可能会导致IRQ#1的触发(因为返回码),或者是一些什么
#ifdef KBD_ENABLE_SPIRQ_FIX2
if (scancode == PS2_RESULT_ACK || scancode == PS2_RESULT_NAK) {
+ ps2_process_cmd(NULL);
return;
}
#endif
c++;
}
if (c >= 5) {
- kprintf(KWARN "max attempt reached.\n");
+ kprintf(KWARN "max attempt reached.");
}
return r;
}
.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);
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),
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,
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,
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);
+++ /dev/null
-#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 */
--- /dev/null
+#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 */
#define __LUNAIX_CLOCK_H
#include <lunaix/time.h>
-
-void
-clock_init();
-
void
clock_walltime(datetime_t* datetime);
#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 */
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 */
#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 */
#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
(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,
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;
}
-#include <klibc/stdio.h>
+#include <klibc/strfmt.h>
#include <klibc/string.h>
#include <hal/ahci/hba.h>
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...
}
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;
}
// 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>
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);
}
}
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
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);
p = p->execp->saved_prev_ctx;
}
- kprintf(KDEBUG "\n");
}
\ No newline at end of file
#include <lib/hash.h>
-#include <klibc/stdio.h>
+#include <klibc/strfmt.h>
static DECLARE_HASHTABLE(dev_registry, 32);
static DECLARE_HASHTABLE(dev_byif, 8);
#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);
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);
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 {
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,
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);
}
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,
* @copyright Copyright (c) 2022
*
*/
-#include <klibc/stdio.h>
+#include <klibc/strfmt.h>
#include <klibc/string.h>
#include <lunaix/clock.h>
#include <lunaix/fs.h>
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);
}
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);
#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
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);
}
#include <sys/interrupts.h>
#include <sys/mm/mempart.h>
-#include <klibc/stdio.h>
+#include <klibc/strfmt.h>
#include <klibc/string.h>
extern void
/* 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))) {
--- /dev/null
+#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
--- /dev/null
+#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 */
--- /dev/null
+#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
+++ /dev/null
-#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
void
init_platform()
{
- kprintf(KINFO "\033[11;0mLunaixOS (gcc v%s, %s)\033[39;49m\n",
- __VERSION__,
- __TIME__);
-
device_poststage();
twifs_register_plugins();
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;
#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>
-#include <klibc/stdio.h>
+#include <klibc/strfmt.h>
#include <lunaix/spike.h>
#include <sys/interrupts.h>
{
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));
}
EXPORT_TWIFS_PLUGIN(sys_clock, clock_build_mapping);
-void
-clock_init()
-{
- // hwrtc_init();
-}
-
time_t
clock_unixtime()
{
time_t
clock_systime()
{
+ if (!current_timer) {
+ return 0;
+ }
+
ticks_t t = hwtimer_current_systicks();
return t / current_timer->running_freq;
}
#define __LUNAIX_LIBC
-#include <klibc/stdio.h>
#include <klibc/stdlib.h>
+#include <klibc/strfmt.h>
#include <klibc/string.h>
#include <lunaix/types.h>
#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 :)
{
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;
}
{
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
SECTIONS {
. = 0x400000;
-}
\ No newline at end of file
+
+ .text : {
+ *(.text)
+ }
+
+ .data : {
+ *(.data)
+ }
+
+ .ro.data : {
+ *(.ro.data)
+ }
+
+ .bss : {
+ *(.bss)
+ }
+}