From 1eeed1150149b63d6e49e033697454bc12b533b9 Mon Sep 17 00:00:00 2001 From: Minep Date: Mon, 11 Dec 2023 19:43:48 +0000 Subject: [PATCH] fix: corner cases when printing large content through term interface refactor: make usr/ compilation more clean refactor: clean up unused header files refactor: renamed things --- .../arch/i386/exceptions/intr_routines.c | 6 -- .../{kernel/tty => hal/char}/lxconsole.c | 76 +++++++------------ lunaix-os/hal/char/serial.c | 18 ++++- lunaix-os/hal/term/console.c | 5 +- lunaix-os/hal/term/lcntls/ansi_cntl.c | 4 +- lunaix-os/hal/term/lcntls/lcntl.c | 13 +++- lunaix-os/hal/term/term.c | 13 +++- lunaix-os/hal/term/term_io.c | 43 ++++++----- lunaix-os/includes/hal/term.h | 1 + lunaix-os/includes/lunaix/compiler.h | 3 +- lunaix-os/includes/lunaix/ds/ldga.h | 5 +- lunaix-os/includes/lunaix/ds/rbuffer.h | 37 +++++---- lunaix-os/includes/lunaix/kprintf.h | 6 ++ lunaix-os/includes/lunaix/lxconsole.h | 31 -------- lunaix-os/includes/lunaix/owloysius.h | 8 +- lunaix-os/includes/lunaix/syslog.h | 19 ----- lunaix-os/includes/lunaix/tty/console.h | 16 ---- lunaix-os/kernel/ds/rbuffer.c | 12 +-- lunaix-os/kernel/fs/twifs/twifs.c | 2 +- lunaix-os/kernel/fs/vfs.c | 2 +- lunaix-os/kernel/kinit.c | 8 +- lunaix-os/kernel/kprint/kprintf.c | 4 +- lunaix-os/kernel/proc0.c | 2 +- lunaix-os/kernel/tty/tty.c | 75 ++++++------------ lunaix-os/link/linker.ld | 18 ++++- lunaix-os/makeinc/toolchain.mkinc | 2 +- lunaix-os/usr/apps.list | 2 + lunaix-os/usr/{cat/main.c => cat.c} | 0 lunaix-os/usr/cat/makefile | 21 ----- lunaix-os/usr/execs.list | 5 ++ lunaix-os/usr/init/init.c | 34 ++++----- lunaix-os/usr/{ls => }/ls.c | 0 lunaix-os/usr/ls/makefile | 21 ----- lunaix-os/usr/makefile | 30 +++++--- lunaix-os/usr/{signal_demo => }/signal_demo.c | 0 lunaix-os/usr/signal_demo/makefile | 21 ----- lunaix-os/usr/{stat/main.c => stat.c} | 0 lunaix-os/usr/stat/makefile | 21 ----- lunaix-os/usr/{testp/main.c => testp.c} | 0 lunaix-os/usr/testp/makefile | 21 ----- 40 files changed, 226 insertions(+), 379 deletions(-) rename lunaix-os/{kernel/tty => hal/char}/lxconsole.c (85%) create mode 100644 lunaix-os/includes/lunaix/kprintf.h delete mode 100644 lunaix-os/includes/lunaix/lxconsole.h delete mode 100644 lunaix-os/includes/lunaix/tty/console.h create mode 100644 lunaix-os/usr/apps.list rename lunaix-os/usr/{cat/main.c => cat.c} (100%) delete mode 100644 lunaix-os/usr/cat/makefile create mode 100644 lunaix-os/usr/execs.list rename lunaix-os/usr/{ls => }/ls.c (100%) delete mode 100644 lunaix-os/usr/ls/makefile rename lunaix-os/usr/{signal_demo => }/signal_demo.c (100%) delete mode 100644 lunaix-os/usr/signal_demo/makefile rename lunaix-os/usr/{stat/main.c => stat.c} (100%) delete mode 100644 lunaix-os/usr/stat/makefile rename lunaix-os/usr/{testp/main.c => testp.c} (100%) delete mode 100644 lunaix-os/usr/testp/makefile diff --git a/lunaix-os/arch/i386/exceptions/intr_routines.c b/lunaix-os/arch/i386/exceptions/intr_routines.c index 77a1372..91366bf 100644 --- a/lunaix-os/arch/i386/exceptions/intr_routines.c +++ b/lunaix-os/arch/i386/exceptions/intr_routines.c @@ -1,7 +1,6 @@ #include #include -#include #include #include #include @@ -32,7 +31,6 @@ intr_routine_divide_zero(const isr_param* param) { __print_panic_msg("div zero", param); - console_flush(); spin(); } @@ -41,7 +39,6 @@ intr_routine_general_protection(const isr_param* param) { __print_panic_msg("general protection", param); - console_flush(); spin(); } @@ -50,7 +47,6 @@ intr_routine_sys_panic(const isr_param* param) { __print_panic_msg((char*)(param->registers.edi), param); - console_flush(); spin(); } @@ -59,7 +55,6 @@ intr_routine_fallback(const isr_param* param) { __print_panic_msg("unknown interrupt", param); - console_flush(); spin(); } @@ -83,7 +78,6 @@ intr_routine_apic_error(const isr_param* param) __print_panic_msg(buf, param); - console_flush(); spin(); } diff --git a/lunaix-os/kernel/tty/lxconsole.c b/lunaix-os/hal/char/lxconsole.c similarity index 85% rename from lunaix-os/kernel/tty/lxconsole.c rename to lunaix-os/hal/char/lxconsole.c index 2540077..53d01bb 100644 --- a/lunaix-os/kernel/tty/lxconsole.c +++ b/lunaix-os/hal/char/lxconsole.c @@ -14,17 +14,26 @@ #include #include #include -#include #include #include #include #include #include -#include #include +#include +#include #include +struct console +{ + struct lx_timer* flush_timer; + struct fifo_buf output; + struct fifo_buf input; + size_t wnd_start; + size_t lines; +}; + static struct console lx_console; int @@ -39,6 +48,12 @@ console_write(struct console* console, u8_t* data, size_t size); void console_flush(); +void +console_view_up(); + +void +console_view_down(); + static waitq_t lx_reader; static volatile pid_t fg_pgid = 0; @@ -81,42 +96,6 @@ done: return INPUT_EVT_NEXT; } -int -__tty_exec_cmd(struct device* dev, u32_t req, va_list args) -{ - switch (req) { - case TIOCGPGRP: - return fg_pgid; - case TIOCSPGRP: - fg_pgid = va_arg(args, pid_t); - break; - case TIOCCLSBUF: - fifo_clear(&lx_console.output); - fifo_clear(&lx_console.input); - lx_console.wnd_start = 0; - lx_console.lines = 0; - lx_console.output.flags |= FIFO_DIRTY; - break; - case TIOCFLUSH: - lx_console.output.flags |= FIFO_DIRTY; - console_flush(); - break; - default: - return EINVAL; - } - return 0; -} - -void -lxconsole_init() -{ - memset(&lx_console, 0, sizeof(lx_console)); - fifo_init(&lx_console.output, valloc(8192), 8192, 0); - fifo_init(&lx_console.input, valloc(4096), 4096, 0); - - lx_console.flush_timer = NULL; -} - int __tty_write_pg(struct device* dev, void* buf, size_t offset) { @@ -153,12 +132,6 @@ __tty_read(struct device* dev, void* buf, size_t offset, size_t len) return count + fifo_read(&console->input, buf + count, len - count); } -void -console_schedule_flush() -{ - // TODO make the flush on-demand rather than periodic -} - size_t __find_next_line(size_t start) { @@ -286,13 +259,17 @@ console_write_char(char str) console_write(&lx_console, (u8_t*)&str, 1); } -void -console_start_flushing() + +static void +lxconsole_init() { - struct lx_timer* timer = - timer_run_ms(20, console_flush, NULL, TIMER_MODE_PERIODIC); - lx_console.flush_timer = timer; + memset(&lx_console, 0, sizeof(lx_console)); + fifo_init(&lx_console.output, valloc(8192), 8192, 0); + fifo_init(&lx_console.input, valloc(4096), 4096, 0); + + lx_console.flush_timer = NULL; } +owloysius_fetch_init(lxconsole_init, on_earlyboot) static int lxconsole_spawn_ttydev(struct device_def* devdef) @@ -302,7 +279,6 @@ lxconsole_spawn_ttydev(struct device_def* devdef) tty_dev->ops.write_page = __tty_write_pg; tty_dev->ops.read = __tty_read; tty_dev->ops.read_page = __tty_read_pg; - tty_dev->ops.exec_cmd = __tty_exec_cmd; waitq_init(&lx_reader); input_add_listener(__lxconsole_listener); diff --git a/lunaix-os/hal/char/serial.c b/lunaix-os/hal/char/serial.c index d93ff39..826b3a6 100644 --- a/lunaix-os/hal/char/serial.c +++ b/lunaix-os/hal/char/serial.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -20,6 +21,8 @@ static DEFINE_LLIST(serial_devs); static int serial_idx = 0; +static struct device_cat* serial_cat; + #define serial_device(dev) ((struct serial_dev*)(dev)->underlay) int @@ -239,7 +242,7 @@ struct serial_dev* serial_create(struct devclass* class, char* if_ident) { struct serial_dev* sdev = valloc(sizeof(struct serial_dev)); - struct device* dev = device_allocseq(NULL, sdev); + struct device* dev = device_allocseq(dev_meta(serial_cat), sdev); dev->ops.read = __serial_read; dev->ops.read_page = __serial_read_page; dev->ops.read_async = __serial_read_async; @@ -264,7 +267,7 @@ serial_create(struct devclass* class, char* if_ident) device_grant_capability(dev, cap_meta(tp_cap)); - register_device(dev, class, "serial%d", class->variant); + register_device(dev, class, "s%d", class->variant); term_create(dev, if_ident); @@ -284,4 +287,13 @@ serial_get_avilable() } return NULL; -} \ No newline at end of file +} + +static void +init_serial_dev() +{ + serial_cat = device_addcat(NULL, "serial"); + + assert(serial_cat); +} +owloysius_fetch_init(init_serial_dev, on_earlyboot) \ No newline at end of file diff --git a/lunaix-os/hal/term/console.c b/lunaix-os/hal/term/console.c index 7d9b57b..1c2a2cf 100644 --- a/lunaix-os/hal/term/console.c +++ b/lunaix-os/hal/term/console.c @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -41,5 +42,7 @@ setup_default_tty() INFO("system console: %s", console_dev); sysconsole = dev; + + kprintf_dump_logs(); } -lunaix_initfn(setup_default_tty, call_on_boot); \ No newline at end of file +owloysius_fetch_init(setup_default_tty, on_boot); \ No newline at end of file diff --git a/lunaix-os/hal/term/lcntls/ansi_cntl.c b/lunaix-os/hal/term/lcntls/ansi_cntl.c index 112f719..778eb4b 100644 --- a/lunaix-os/hal/term/lcntls/ansi_cntl.c +++ b/lunaix-os/hal/term/lcntls/ansi_cntl.c @@ -22,12 +22,12 @@ __ansi_actcontrol(struct term* termdev, struct linebuffer* lbuf, char chr) default: if ((int)chr < 32) { rbuffer_put(cooked, '^'); - chr += 64; + return rbuffer_put(cooked, chr += 64); } break; } - return rbuffer_put(cooked, chr); + return rbuffer_put_nof(cooked, chr); } struct term_lcntl ansi_line_controller = {.process_and_put = __ansi_actcontrol}; \ No newline at end of file diff --git a/lunaix-os/hal/term/lcntls/lcntl.c b/lunaix-os/hal/term/lcntls/lcntl.c index 0802318..de3334c 100644 --- a/lunaix-os/hal/term/lcntls/lcntl.c +++ b/lunaix-os/hal/term/lcntls/lcntl.c @@ -44,6 +44,13 @@ lcntl_transform_seq(struct term* tdev, struct linebuffer* lbuf, bool out) #define QUIT tdev->cc[_VQUIT] #define SUSP tdev->cc[_VSUSP] +#define putc_safe(rb, chr) \ + ({ \ + if (!rbuffer_put_nof(rb, chr)) { \ + break; \ + } \ + }) + if (!out) { // Keep all cc's (except VMIN & VTIME) up to L2 cache. for (size_t i = 0; i < _VMIN; i++) { @@ -51,7 +58,7 @@ lcntl_transform_seq(struct term* tdev, struct linebuffer* lbuf, bool out) } } - while (rbuffer_get(raw, &c) && allow_more) { + while (allow_more && rbuffer_get(raw, &c)) { if (c == '\r' && ((_if & _ICRNL) || (_of & _OCRNL))) { c = '\n'; @@ -136,14 +143,14 @@ lcntl_transform_seq(struct term* tdev, struct linebuffer* lbuf, bool out) do_out: if (c == '\n' && (_of & _ONLCR)) { - rbuffer_put(cooked, '\r'); + putc_safe(cooked, '\r'); } put_char: if (!out && (_lf & _IEXTEN) && lcntl_slave_put) { allow_more = lcntl_slave_put(tdev, lbuf, c); } else { - allow_more = rbuffer_put(cooked, c); + allow_more = rbuffer_put_nof(cooked, c); } } diff --git a/lunaix-os/hal/term/term.c b/lunaix-os/hal/term/term.c index 3cc9741..3db8759 100644 --- a/lunaix-os/hal/term/term.c +++ b/lunaix-os/hal/term/term.c @@ -136,7 +136,7 @@ tdev_do_write(struct device* dev, void* buf, off_t fpos, size_t len) if (!rbuffer_empty(deref(current))) { term_flush(tdev); } - return 0; + return wrsz; } static int @@ -170,6 +170,14 @@ load_default_setting(struct term* tdev) memcpy(tdev->cc, default_cc, _NCCS * sizeof(cc_t)); } +static void +alloc_term_buffer(struct term* terminal, size_t sz_hlf) +{ + line_alloc(&terminal->line_in, sz_hlf); + line_alloc(&terminal->line_out, sz_hlf); + terminal->scratch_pad = valloc(sz_hlf); +} + struct term* term_create(struct device* chardev, char* suffix) { @@ -189,8 +197,7 @@ term_create(struct device* chardev, char* suffix) // TODO choice of lcntl can be flexible terminal->lcntl = line_controls[ANSI_LCNTL]; - line_alloc(&terminal->line_in, 1024); - line_alloc(&terminal->line_out, 1024); + alloc_term_buffer(terminal, 1024); if (chardev) { int cdev_var = DEV_VAR_FROM(chardev->ident.unique); diff --git a/lunaix-os/hal/term/term_io.c b/lunaix-os/hal/term/term_io.c index 835659d..57602c8 100644 --- a/lunaix-os/hal/term/term_io.c +++ b/lunaix-os/hal/term/term_io.c @@ -93,26 +93,35 @@ term_read(struct term* tdev) int term_flush(struct term* tdev) { - if ((tdev->oflags & _OPOST)) { - lcntl_transform_outseq(tdev); - } - struct linebuffer* line_out = &tdev->line_out; - size_t xmit_len = line_out->current->len; - char* xmit_buf = line_out->next->buffer; + char* xmit_buf = tdev->scratch_pad; + lbuf_ref_t current_ref = ref_current(line_out); - rbuffer_gets(line_out->current, xmit_buf, xmit_len); + int count = 0; - off_t off = 0; - int ret = 0; - while (xmit_len && ret >= 0) { - ret = tdev->chdev->ops.write(tdev->chdev, &xmit_buf[off], 0, xmit_len); - xmit_len -= ret; - off += ret; - } + while (!rbuffer_empty(deref(current_ref))) { + if ((tdev->oflags & _OPOST)) { + lcntl_transform_outseq(tdev); + } + + size_t xmit_len = line_out->current->len; - // put back the left over if transmittion went south - rbuffer_puts(line_out->current, xmit_buf, xmit_len); + rbuffer_gets(line_out->current, xmit_buf, xmit_len); + + off_t off = 0; + int ret = 0; + while (xmit_len && ret >= 0) { + ret = tdev->chdev->ops.write(tdev->chdev, &xmit_buf[off], 0, xmit_len); + xmit_len -= ret; + off += ret; + count += ret; + } + + // put back the left over if transmittion went south + rbuffer_puts(line_out->current, xmit_buf, xmit_len); + + line_flip(line_out); + } - return off; + return count; } \ No newline at end of file diff --git a/lunaix-os/includes/hal/term.h b/lunaix-os/includes/hal/term.h index 0f11e52..4ddf74d 100644 --- a/lunaix-os/includes/hal/term.h +++ b/lunaix-os/includes/hal/term.h @@ -61,6 +61,7 @@ struct term struct term_lcntl* lcntl; struct linebuffer line_out; struct linebuffer line_in; + char* scratch_pad; pid_t fggrp; struct termport_capability* tp_cap; diff --git a/lunaix-os/includes/lunaix/compiler.h b/lunaix-os/includes/lunaix/compiler.h index 7581978..6c33779 100644 --- a/lunaix-os/includes/lunaix/compiler.h +++ b/lunaix-os/includes/lunaix/compiler.h @@ -10,6 +10,7 @@ #define optimize(opt) __attribute__((optimize(opt))) #define must_inline __attribute__((always_inline)) #define must_emit __attribute__((used)) +#define unreachable __builtin_unreachable() #define clz(bits) __builtin_clz(bits) #define sadd_overflow(a, b, of) __builtin_sadd_overflow(a, b, of) @@ -34,7 +35,7 @@ spin() volatile int __infloop = 1; while (__infloop) ; - __builtin_unreachable(); + unreachable; } #endif /* __LUNAIX_COMPILER_H */ diff --git a/lunaix-os/includes/lunaix/ds/ldga.h b/lunaix-os/includes/lunaix/ds/ldga.h index 8c00bbe..a9b1430 100644 --- a/lunaix-os/includes/lunaix/ds/ldga.h +++ b/lunaix-os/includes/lunaix/ds/ldga.h @@ -12,15 +12,16 @@ #define __LUNAIX_LDGA_H #include +#include #define ldga_el_id(ga_name, el_name) __lga_##ga_name##_##el_name #define ldga_section(ga_name) __attribute__((section(".lga." ga_name))) #define export_ldga_el(ga_name, el_name, type, val) \ - type ldga_section(#ga_name) ldga_el_id(ga_name, el_name) = (type)(val) + type ldga_section(#ga_name) must_emit ldga_el_id(ga_name, el_name) = (type)(val) #define export_ldga_el_sfx(ga_name, el_name, type, val, suffix) \ - type ldga_section(#ga_name "." #suffix) ldga_el_id(ga_name, el_name) = \ + type ldga_section(#ga_name "." #suffix) must_emit ldga_el_id(ga_name, el_name) = \ (type)(val) #define export_ldga_el_idx(ga_name, i, type, val) \ diff --git a/lunaix-os/includes/lunaix/ds/rbuffer.h b/lunaix-os/includes/lunaix/ds/rbuffer.h index f3777b6..d45c9fd 100644 --- a/lunaix-os/includes/lunaix/ds/rbuffer.h +++ b/lunaix-os/includes/lunaix/ds/rbuffer.h @@ -20,6 +20,22 @@ rbuffer_init(struct rbuffer* rb, char* buf, size_t maxsz) struct rbuffer* rbuffer_create(char* buf, size_t maxsz); +int +rbuffer_erase(struct rbuffer* rb); + +int +rbuffer_put(struct rbuffer* rb, char c); + +int +rbuffer_puts(struct rbuffer* rb, char* c, size_t len); + +int +rbuffer_gets(struct rbuffer* rb, char* buf, size_t len); + +int +rbuffer_get(struct rbuffer* rb, char* c); + + static inline void rbuffer_clear(struct rbuffer* rb) { @@ -38,19 +54,14 @@ rbuffer_full(struct rbuffer* rb) return rb->len == rb->maxsz; } -int -rbuffer_erase(struct rbuffer* rb); - -int -rbuffer_put(struct rbuffer* rb, char c); - -int -rbuffer_puts(struct rbuffer* rb, char* c, size_t len); - -int -rbuffer_gets(struct rbuffer* rb, char* buf, size_t len); +static inline int +rbuffer_put_nof(struct rbuffer* rb, char c) +{ + if (rbuffer_full(rb)) { + return 0; + } -int -rbuffer_get(struct rbuffer* rb, char* c); + return rbuffer_put(rb, c); +} #endif /* __LUNAIX_RBUFFER_H */ diff --git a/lunaix-os/includes/lunaix/kprintf.h b/lunaix-os/includes/lunaix/kprintf.h new file mode 100644 index 0000000..84fef64 --- /dev/null +++ b/lunaix-os/includes/lunaix/kprintf.h @@ -0,0 +1,6 @@ +#ifndef __LUNAIX_KPRINTF_H +#define __LUNAIX_KPRINTF_H + +void kprintf_dump_logs(); + +#endif /* __LUNAIX_KPRINTF_H */ diff --git a/lunaix-os/includes/lunaix/lxconsole.h b/lunaix-os/includes/lunaix/lxconsole.h deleted file mode 100644 index fbf59a4..0000000 --- a/lunaix-os/includes/lunaix/lxconsole.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef __LUNAIX_LXCONSOLE_H -#define __LUNAIX_LXCONSOLE_H - -#define TCINTR 0x03 // ETX -#define TCEOF 0x04 // EOT -#define TCBS 0x08 // BS -#define TCLF 0x0A // LF -#define TCCR 0x0D // CR -#define TCSTOP 0x1A // SUB - -void -lxconsole_init(); - -void -console_write_str(char* str); - -void -console_write_char(char chr); - -void -console_view_up(); - -void -console_view_down(); - -void -console_flush(); - -void -console_start_flushing(); -#endif /* __LUNAIX_LXCONSOLE_H */ diff --git a/lunaix-os/includes/lunaix/owloysius.h b/lunaix-os/includes/lunaix/owloysius.h index 7948066..c1f8584 100644 --- a/lunaix-os/includes/lunaix/owloysius.h +++ b/lunaix-os/includes/lunaix/owloysius.h @@ -3,11 +3,11 @@ #include -#define call_on_earlyboot c_earlyboot -#define call_on_boot c_boot -#define call_on_postboot c_postboot +#define on_earlyboot c_earlyboot +#define on_boot c_boot +#define on_postboot c_postboot -#define lunaix_initfn(func, call_stage) \ +#define owloysius_fetch_init(func, call_stage) \ export_ldga_el(lunainit, func, ptr_t, func); \ export_ldga_el_sfx(lunainit, func##_##call_stage, ptr_t, func, call_stage); diff --git a/lunaix-os/includes/lunaix/syslog.h b/lunaix-os/includes/lunaix/syslog.h index 4877857..423823c 100644 --- a/lunaix-os/includes/lunaix/syslog.h +++ b/lunaix-os/includes/lunaix/syslog.h @@ -40,23 +40,4 @@ void kprintf_m(const char* component, const char* fmt, va_list args); - -// TODO need more thought on it - -// struct klog_chunk -// { -// void* log_entry; -// size_t max_len; -// size_t len; -// }; - -// struct klog_chunk* -// kprintf_lcstart_m(const char* component, size_t size); - -// void -// kprintf_lcappend_m(struct klog_chunk*, const char* fmt, va_list args); - -// void -// kprintf_lcdone_m(struct klog_chunk*); - #endif /* __LUNAIX_SYSLOG_H */ diff --git a/lunaix-os/includes/lunaix/tty/console.h b/lunaix-os/includes/lunaix/tty/console.h deleted file mode 100644 index 24d088f..0000000 --- a/lunaix-os/includes/lunaix/tty/console.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef __LUNAIX_CONSOLE_H -#define __LUNAIX_CONSOLE_H - -#include -#include - -struct console -{ - struct lx_timer* flush_timer; - struct fifo_buf output; - struct fifo_buf input; - size_t wnd_start; - size_t lines; -}; - -#endif /* __LUNAIX_CONSOLE_H */ diff --git a/lunaix-os/kernel/ds/rbuffer.c b/lunaix-os/kernel/ds/rbuffer.c index ab4dea5..eea75d6 100644 --- a/lunaix-os/kernel/ds/rbuffer.c +++ b/lunaix-os/kernel/ds/rbuffer.c @@ -32,7 +32,7 @@ rbuffer_put(struct rbuffer* rb, char c) rb->ptr = (rb->ptr + 1) % rb->maxsz; rb->len = MIN(rb->len + 1, rb->maxsz); - return 1; + return rb->len < rb->maxsz; } int @@ -41,21 +41,21 @@ rbuffer_puts(struct rbuffer* rb, char* buf, size_t len) if (!len) return 0; - size_t ptr = (rb->ptr + len) % rb->maxsz; size_t nlen = MIN(len, rb->maxsz); - size_t ptr_start = (ptr - nlen) % rb->maxsz; + size_t ptr = (rb->ptr + nlen) % rb->maxsz; + size_t ptr_start = rb->ptr; buf = &buf[nlen]; if (ptr_start >= ptr) { size_t llen = rb->maxsz - ptr_start; memcpy(&rb->buffer[ptr_start], &buf[-nlen], llen); - memcpy(&rb->buffer[0], &buf[-nlen + llen], ptr + 1); + memcpy(&rb->buffer[0], &buf[-nlen + llen], ptr); } else { memcpy(&rb->buffer[ptr_start], &buf[-nlen], nlen); } rb->ptr = ptr; - rb->len = nlen; + rb->len = MIN(rb->len + nlen, rb->maxsz); return nlen; } @@ -74,7 +74,7 @@ rbuffer_gets(struct rbuffer* rb, char* buf, size_t len) if (ptr_start >= ptr_end) { size_t llen = rb->maxsz - ptr_start; memcpy(&buf[-nlen], &rb->buffer[ptr_start], llen); - memcpy(&buf[-nlen + llen], &rb->buffer[0], ptr_end + 1); + memcpy(&buf[-nlen + llen], &rb->buffer[0], ptr_end); } else { memcpy(&buf[-nlen], &rb->buffer[ptr_start], nlen); } diff --git a/lunaix-os/kernel/fs/twifs/twifs.c b/lunaix-os/kernel/fs/twifs/twifs.c index 9d6d9f7..6aa5f6a 100644 --- a/lunaix-os/kernel/fs/twifs/twifs.c +++ b/lunaix-os/kernel/fs/twifs/twifs.c @@ -208,7 +208,7 @@ twifs_file_node_vargs(struct twifs_node* parent, const char* fmt, va_list args) char buf[VFS_NAME_MAXLEN]; size_t len = ksnprintfv(buf, fmt, VFS_NAME_MAXLEN, args); - return __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFSEQDEV); + return __twifs_new_node(parent ? parent : fs_root, buf, len, F_FILE); } struct twifs_node* diff --git a/lunaix-os/kernel/fs/vfs.c b/lunaix-os/kernel/fs/vfs.c index 28cede5..4ec74b4 100644 --- a/lunaix-os/kernel/fs/vfs.c +++ b/lunaix-os/kernel/fs/vfs.c @@ -708,7 +708,7 @@ __DEFINE_LXSYSCALL3(int, read, int, fd, void*, buf, size_t, count) file->inode->atime = clock_unixtime(); if ((file->inode->itype & VFS_IFSEQDEV) || (fd_s->flags & FO_DIRECT)) { - errno = file->ops->read(file->inode, buf, count, file->f_pos); + errno = file->ops->read(file->inode, buf, count, file->f_pos); } else { errno = pcache_read(file->inode, buf, count, file->f_pos); } diff --git a/lunaix-os/kernel/kinit.c b/lunaix-os/kernel/kinit.c index 5884e5b..a54b68a 100644 --- a/lunaix-os/kernel/kinit.c +++ b/lunaix-os/kernel/kinit.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -57,10 +56,11 @@ kernel_bootstrap(struct boot_handoff* bhctx) device_scan_drivers(); - // crt + invoke_init_function(on_earlyboot); + + // FIXME this goes to hal/gfxa tty_init(ioremap(0xB8000, PG_SIZE)); tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK); - lxconsole_init(); device_sysconf_load(); @@ -89,7 +89,7 @@ kernel_bootstrap(struct boot_handoff* bhctx) must_success(vfs_mount_root("ramfs", NULL)); must_success(vfs_mount("/dev", "devfs", NULL, 0)); - invoke_init_function(call_on_boot); + invoke_init_function(on_boot); must_success(vfs_unmount("/dev")); diff --git a/lunaix-os/kernel/kprint/kprintf.c b/lunaix-os/kernel/kprint/kprintf.c index 310b1f6..158a799 100644 --- a/lunaix-os/kernel/kprint/kprintf.c +++ b/lunaix-os/kernel/kprint/kprintf.c @@ -87,7 +87,8 @@ kprintf_mapping_init() EXPORT_TWIFS_PLUGIN(kprintf, kprintf_mapping_init); -static void kprintf_init() { +void +kprintf_dump_logs() { if (unlikely(!sysconsole)) { return; } @@ -98,7 +99,6 @@ static void kprintf_init() { sysconsole->ops.write(sysconsole, pos->content, 0, pos->len); } } -lunaix_initfn(kprintf_init, call_on_postboot); __DEFINE_LXSYSCALL3(void, syslog, int, level, const char*, fmt, va_list, args) { diff --git a/lunaix-os/kernel/proc0.c b/lunaix-os/kernel/proc0.c index a9d25a7..a935a3b 100644 --- a/lunaix-os/kernel/proc0.c +++ b/lunaix-os/kernel/proc0.c @@ -82,7 +82,7 @@ void init_platform() { device_postboot_load(); - invoke_init_function(call_on_postboot); + invoke_init_function(on_postboot); twifs_register_plugins(); diff --git a/lunaix-os/kernel/tty/tty.c b/lunaix-os/kernel/tty/tty.c index 6f98855..e5a913f 100644 --- a/lunaix-os/kernel/tty/tty.c +++ b/lunaix-os/kernel/tty/tty.c @@ -1,6 +1,5 @@ #include #include -#include #include #include @@ -61,62 +60,34 @@ tty_flush_buffer(struct fifo_buf* buf) tty_clear(); char chr; - int state = 0; - int g[2] = { 0, 0 }; - vga_attribute current_theme = tty_theme_color; while (fifo_readone_async(buf, (u8_t*)&chr)) { - if (state == 0 && chr == '\033') { - state = 1; - } else if (state == 1 && chr == '[') { - state = 2; - } else if (state > 1) { - if ('0' <= chr && chr <= '9') { - g[state - 2] = (chr - '0') + g[state - 2] * 10; - } else if (chr == ';' && state == 2) { - state = 3; - } else { - if (g[0] == 39 && g[1] == 49) { - current_theme = tty_theme_color; - } else { - current_theme = (g[1] << 4 | g[0]) << 8; - } - g[0] = 0; - g[1] = 0; - state = 0; - } - } else { - state = 0; - switch (chr) { - case '\t': - x += 4; - break; - case '\n': - y++; - // fall through - case '\r': - x = 0; - break; - // case '\x08': - // *(tty_vga_buffer + x + y * TTY_WIDTH) = - // (current_theme | 0x20); - // break; - default: - *(tty_vga_buffer + x + y * TTY_WIDTH) = - (current_theme | chr); - (x)++; - break; - } - - if (x >= TTY_WIDTH) { - x = 0; + switch (chr) { + case '\t': + x += 4; + break; + case '\n': y++; - } - if (y >= TTY_HEIGHT) { - y--; + // fall through + case '\r': + x = 0; + break; + default: + *(tty_vga_buffer + x + y * TTY_WIDTH) = + (tty_theme_color | chr); + (x)++; break; - } + } + + if (x >= TTY_WIDTH) { + x = 0; + y++; + } + if (y >= TTY_HEIGHT) { + y--; + break; } } + tty_set_cursor(x, y); } diff --git a/lunaix-os/link/linker.ld b/lunaix-os/link/linker.ld index c91bcb2..3941b36 100644 --- a/lunaix-os/link/linker.ld +++ b/lunaix-os/link/linker.ld @@ -130,21 +130,31 @@ SECTIONS { . = ALIGN(8); - PROVIDE(__lga_lunainit_call_on_boot_start = .); + PROVIDE(__lga_lunainit_on_earlyboot_start = .); + + KEEP(*(.lga.lunainit.c_earlyboot)); + + PROVIDE(__lga_lunainit_on_earlyboot_end = .); + + /* ---- */ + + . = ALIGN(8); + + PROVIDE(__lga_lunainit_on_boot_start = .); KEEP(*(.lga.lunainit.c_boot)); - PROVIDE(__lga_lunainit_call_on_boot_end = .); + PROVIDE(__lga_lunainit_on_boot_end = .); /* ---- */ . = ALIGN(8); - PROVIDE(__lga_lunainit_call_on_postboot_start = .); + PROVIDE(__lga_lunainit_on_postboot_start = .); KEEP(*(.lga.lunainit.c_postboot)); - PROVIDE(__lga_lunainit_call_on_postboot_end = .); + PROVIDE(__lga_lunainit_on_postboot_end = .); } diff --git a/lunaix-os/makeinc/toolchain.mkinc b/lunaix-os/makeinc/toolchain.mkinc index 6882e8d..c63f846 100644 --- a/lunaix-os/makeinc/toolchain.mkinc +++ b/lunaix-os/makeinc/toolchain.mkinc @@ -27,7 +27,7 @@ OFLAGS := -fno-gcse\ -fno-indirect-inlining\ -fno-omit-frame-pointer -CFLAGS := $(ARCH_OPT) -std=gnu99 -MMD $(OFLAGS) $(W) +CFLAGS := $(ARCH_OPT) -std=gnu99 $(OFLAGS) $(W) ifeq ($(BUILD_MODE),debug) O = -Og diff --git a/lunaix-os/usr/apps.list b/lunaix-os/usr/apps.list new file mode 100644 index 0000000..cf7abdd --- /dev/null +++ b/lunaix-os/usr/apps.list @@ -0,0 +1,2 @@ +init +sh \ No newline at end of file diff --git a/lunaix-os/usr/cat/main.c b/lunaix-os/usr/cat.c similarity index 100% rename from lunaix-os/usr/cat/main.c rename to lunaix-os/usr/cat.c diff --git a/lunaix-os/usr/cat/makefile b/lunaix-os/usr/cat/makefile deleted file mode 100644 index 33802bd..0000000 --- a/lunaix-os/usr/cat/makefile +++ /dev/null @@ -1,21 +0,0 @@ -define src_files - main.c -endef - -obj_files := $(addsuffix .o,$(src_files)) -include_opt := $(addprefix -I,$(INCLUDES)) - -out := $(BUILD_DIR)/bin - -%.c.o: %.c - $(call status_,CC,$<) - @$(CC) $(CFLAGS) $(include_opt) -c $< -o $@ - -$(out)/$(BUILD_NAME): $(obj_files) - $(call status_,LD,$(@F)) - @$(CC) -T $(LD_SCRIPT) -o $@ $< $(LIBC) $(LDFLAGS) - -all: $(out)/$(BUILD_NAME) - -clean: - @rm -f $(obj_files) \ No newline at end of file diff --git a/lunaix-os/usr/execs.list b/lunaix-os/usr/execs.list new file mode 100644 index 0000000..c661258 --- /dev/null +++ b/lunaix-os/usr/execs.list @@ -0,0 +1,5 @@ +testp +ls +signal_demo +cat +stat \ No newline at end of file diff --git a/lunaix-os/usr/init/init.c b/lunaix-os/usr/init/init.c index 0a2f541..5aebfcb 100644 --- a/lunaix-os/usr/init/init.c +++ b/lunaix-os/usr/init/init.c @@ -15,14 +15,15 @@ } \ } while (0) -#define check(statement) \ - do { \ +#define check(statement) \ + ({ \ int err = 0; \ - if ((err = (statement))) { \ - syslog(2, #statement " failed: %d", err); \ - return err; \ + if ((err = (statement)) < 0) { \ + syslog(2, #statement " failed: %d", err); \ + _exit(1); \ } \ - } while (0) + err; \ + }) int init_termios(int fd) { @@ -34,7 +35,7 @@ init_termios(int fd) { term.c_iflag = ICRNL | IGNBRK; term.c_oflag = ONLCR | OPOST; term.c_cflag = CREAD | CLOCAL | CS8 | CPARENB; - term.c_cc[VERASE] = 0x08; + term.c_cc[VERASE] = 0x7f; check(tcsetattr(fd, 0, &term)); @@ -44,7 +45,6 @@ init_termios(int fd) { int main(int argc, const char** argv) { - int err = 0; mkdir("/dev"); mkdir("/sys"); @@ -54,24 +54,16 @@ main(int argc, const char** argv) must_mount(NULL, "/sys", "twifs", MNT_RO); must_mount(NULL, "/task", "taskfs", MNT_RO); - if ((err = open("/dev/tty", 0)) < 0) { - syslog(2, "fail to open tty (%d)\n", errno); - return err; - } + int fd = check(open("/dev/tty", 0)); - check(init_termios(err)); + check(init_termios(fd)); - if ((err = dup(err)) < 0) { - syslog(2, "fail to setup tty i/o (%d)\n", errno); - return err; - } + check(dup(fd)); - if ((err = symlink("/usr", "/mnt/lunaix-os/usr"))) { - syslog(2, "symlink /usr:/mnt/lunaix-os/usr (%d)\n", errno); - return err; - } + check(symlink("/usr", "/mnt/lunaix-os/usr")); pid_t pid; + int err = 0; if (!(pid = fork())) { err = execve("/usr/bin/sh", NULL, NULL); printf("fail to execute (%d)\n", errno); diff --git a/lunaix-os/usr/ls/ls.c b/lunaix-os/usr/ls.c similarity index 100% rename from lunaix-os/usr/ls/ls.c rename to lunaix-os/usr/ls.c diff --git a/lunaix-os/usr/ls/makefile b/lunaix-os/usr/ls/makefile deleted file mode 100644 index 5351d43..0000000 --- a/lunaix-os/usr/ls/makefile +++ /dev/null @@ -1,21 +0,0 @@ -define src_files - ls.c -endef - -obj_files := $(addsuffix .o,$(src_files)) -include_opt := $(addprefix -I,$(INCLUDES)) - -out := $(BUILD_DIR)/bin - -%.c.o: %.c - $(call status_,CC,$<) - @$(CC) $(CFLAGS) $(include_opt) -c $< -o $@ - -$(out)/$(BUILD_NAME): $(obj_files) - $(call status_,LD,$(@F)) - @$(CC) -T $(LD_SCRIPT) -o $@ $< $(LIBC) $(LDFLAGS) - -all: $(out)/$(BUILD_NAME) - -clean: - @rm -f $(obj_files) \ No newline at end of file diff --git a/lunaix-os/usr/makefile b/lunaix-os/usr/makefile index e2a98d0..e49d7d0 100644 --- a/lunaix-os/usr/makefile +++ b/lunaix-os/usr/makefile @@ -7,6 +7,8 @@ sys_include := $(CURDIR)/includes build_dir := $(CURDIR)/build libc_name := liblunac libc_files := $(libc_name).a +libc := $(addprefix $(build_dir)/lib/,$(libc_files)) +ldscript := $(CURDIR)/link-usr.ld common_param := CC AR INCLUDES BUILD_DIR BUILD_NAME CFLAGS LDFLAGS @@ -28,30 +30,38 @@ $(build_dir)/$(libc_name).a: $(build_dir)/bin $(build_dir)/lib $(build_dir)/incl $(call status,TASK,$(BUILD_NAME)) @$(MAKE) $(MKFLAGS) -C libc/ $(task) -app-list := ls -app-list += init -app-list += signal_demo -app-list += sh -app-list += cat -app-list += testp -app-list += stat +app-list = $(shell cat apps.list) +exec-list = $(shell cat execs.list) mkapp-list := $(addprefix app-, $(app-list)) +mkexec-list := $(addprefix $(build_dir)/bin/, $(exec-list)) -export LD_SCRIPT := $(CURDIR)/link-usr.ld -export LIBC := $(addprefix $(build_dir)/lib/,$(libc_files)) +export LD_SCRIPT := $(ldscript) +export LIBC := $(libc) app-%: $(call status,TASK,$*) @$(MAKE) $(MKFLAGS) -C $* $(task) BUILD_NAME="$*" +exec_%.o: %.c + $(call status,CC,$<) + @$(CC) $(CFLAGS) $(addprefix -I,$(INCLUDES)) -c $< -o $@ + +$(build_dir)/bin/%: exec_%.o + $(call status,LD,$(@F)) + @$(CC) -T $(ldscript) -o $@ $< $(libc) $(LDFLAGS) + app: task := all app: INCLUDES += $(build_dir)/includes app: $(mkapp-list) +exec: task := all +exec: INCLUDES += $(build_dir)/includes +exec: $(mkexec-list) + clean: task := clean clean: $(mkapp-list) @rm -rf $(build_dir) @$(MAKE) $(MKFLAGS) -C libc/ $(task) all: task := all -all: $(build_dir)/$(libc_name).a app \ No newline at end of file +all: $(build_dir)/$(libc_name).a exec app \ No newline at end of file diff --git a/lunaix-os/usr/signal_demo/signal_demo.c b/lunaix-os/usr/signal_demo.c similarity index 100% rename from lunaix-os/usr/signal_demo/signal_demo.c rename to lunaix-os/usr/signal_demo.c diff --git a/lunaix-os/usr/signal_demo/makefile b/lunaix-os/usr/signal_demo/makefile deleted file mode 100644 index 1bb1248..0000000 --- a/lunaix-os/usr/signal_demo/makefile +++ /dev/null @@ -1,21 +0,0 @@ -define src_files - signal_demo.c -endef - -obj_files := $(addsuffix .o,$(src_files)) -include_opt := $(addprefix -I,$(INCLUDES)) - -out := $(BUILD_DIR)/bin - -%.c.o: %.c - $(call status_,CC,$<) - @$(CC) $(CFLAGS) $(include_opt) -c $< -o $@ - -$(out)/$(BUILD_NAME): $(obj_files) - $(call status_,LD,$(@F)) - @$(CC) -T $(LD_SCRIPT) -o $@ $< $(LIBC) $(LDFLAGS) - -all: $(out)/$(BUILD_NAME) - -clean: - @rm -f $(obj_files) \ No newline at end of file diff --git a/lunaix-os/usr/stat/main.c b/lunaix-os/usr/stat.c similarity index 100% rename from lunaix-os/usr/stat/main.c rename to lunaix-os/usr/stat.c diff --git a/lunaix-os/usr/stat/makefile b/lunaix-os/usr/stat/makefile deleted file mode 100644 index 33802bd..0000000 --- a/lunaix-os/usr/stat/makefile +++ /dev/null @@ -1,21 +0,0 @@ -define src_files - main.c -endef - -obj_files := $(addsuffix .o,$(src_files)) -include_opt := $(addprefix -I,$(INCLUDES)) - -out := $(BUILD_DIR)/bin - -%.c.o: %.c - $(call status_,CC,$<) - @$(CC) $(CFLAGS) $(include_opt) -c $< -o $@ - -$(out)/$(BUILD_NAME): $(obj_files) - $(call status_,LD,$(@F)) - @$(CC) -T $(LD_SCRIPT) -o $@ $< $(LIBC) $(LDFLAGS) - -all: $(out)/$(BUILD_NAME) - -clean: - @rm -f $(obj_files) \ No newline at end of file diff --git a/lunaix-os/usr/testp/main.c b/lunaix-os/usr/testp.c similarity index 100% rename from lunaix-os/usr/testp/main.c rename to lunaix-os/usr/testp.c diff --git a/lunaix-os/usr/testp/makefile b/lunaix-os/usr/testp/makefile deleted file mode 100644 index 33802bd..0000000 --- a/lunaix-os/usr/testp/makefile +++ /dev/null @@ -1,21 +0,0 @@ -define src_files - main.c -endef - -obj_files := $(addsuffix .o,$(src_files)) -include_opt := $(addprefix -I,$(INCLUDES)) - -out := $(BUILD_DIR)/bin - -%.c.o: %.c - $(call status_,CC,$<) - @$(CC) $(CFLAGS) $(include_opt) -c $< -o $@ - -$(out)/$(BUILD_NAME): $(obj_files) - $(call status_,LD,$(@F)) - @$(CC) -T $(LD_SCRIPT) -o $@ $< $(LIBC) $(LDFLAGS) - -all: $(out)/$(BUILD_NAME) - -clean: - @rm -f $(obj_files) \ No newline at end of file -- 2.27.0