diff --git a/Makefile b/Makefile index 8b1b2ff..ab42d73 100644 --- a/Makefile +++ b/Makefile @@ -15,3 +15,4 @@ include make/libarena.mk include make/libioutil.mk include make/libmath.mk include make/libfat.mk +include make/libdebugconsole.mk diff --git a/aux/compiledb.sh b/aux/compiledb.sh index f1ab5fb..3266ed6 100755 --- a/aux/compiledb.sh +++ b/aux/compiledb.sh @@ -14,3 +14,4 @@ make -B all_compiledb_libarena make -B all_compiledb_libioutil make -B all_compiledb_libmath make -B all_compiledb_libfat +make -B all_compiledb_libdebugconsole diff --git a/aux/devel.sh b/aux/devel.sh index f31b11d..d72fd7d 100755 --- a/aux/devel.sh +++ b/aux/devel.sh @@ -21,6 +21,7 @@ make -B all_libarena "$bt" make -B all_libioutil "$bt" make -B all_libmath "$bt" make -B all_libfat "$bt" +make -B all_libdebugconsole "$bt" make -B all_apps "$bt" make -B all_dist ./aux/limine_iso_amd64.sh diff --git a/aux/docs.sh b/aux/docs.sh index 350e814..714cf57 100755 --- a/aux/docs.sh +++ b/aux/docs.sh @@ -15,5 +15,6 @@ make -B docs_libarena make -B docs_libioutil make -B docs_libmath make -B docs_libfat +make -B docs_libdebugconsole mkdocs build diff --git a/aux/format.sh b/aux/format.sh index e214e92..e76847a 100755 --- a/aux/format.sh +++ b/aux/format.sh @@ -14,4 +14,5 @@ make -B format_libarena make -B format_libioutil make -B format_libmath make -B format_libfat +make -B format_libdebugconsole make -B format_apps diff --git a/ce/mprintf.c b/ce/mprintf.c index 6d87fc4..86649a4 100644 --- a/ce/mprintf.c +++ b/ce/mprintf.c @@ -6,8 +6,6 @@ #include #include -void putchar_ (char ch) { (void)ch; } - void mprintf (const char* fmt, ...) { va_list args; va_start (args, fmt); diff --git a/include/devices.h b/include/devices.h index 54ae3f5..3c74256 100644 --- a/include/devices.h +++ b/include/devices.h @@ -1,6 +1,9 @@ #ifndef _DEVICES_H #define _DEVICES_H +/* debugconsole device */ +#define DEBUGCONSOLE_PUTSTR 0 + /* terminal device */ #define TERMINAL_PUTSTR 0 #define TERMINAL_DIMENSIONS 1 diff --git a/init/Makefile b/init/Makefile index 08c01ec..f5f047b 100644 --- a/init/Makefile +++ b/init/Makefile @@ -4,5 +4,9 @@ $(eval $(call add_lib,libterminal)) $(eval $(call add_lib,libprocess)) $(eval $(call add_lib,libstring)) $(eval $(call add_lib,libkb)) +$(eval $(call add_lib,libdebugconsole)) +$(eval $(call add_lib,libaux)) + +cflags += -DPRINTF_INCLUDE_CONFIG_H=1 include ../make/user.mk diff --git a/init/init.c b/init/init.c index 206734b..a4bb216 100644 --- a/init/init.c +++ b/init/init.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -23,6 +24,8 @@ void receiver (void* arg) { } void app_main (void) { + debug_printf ("Init process is running. Starting user shell...\n"); + int ce_pid = exec ("RD", "ce"); ce_pgid = get_procgroup (ce_pid); diff --git a/kernel/amd64/bootmain.c b/kernel/amd64/bootmain.c index f1c1427..56c9bf6 100644 --- a/kernel/amd64/bootmain.c +++ b/kernel/amd64/bootmain.c @@ -64,7 +64,7 @@ void bootmain (void) { vfs_create_volume ("RD", FS_TARFS, rd0, false); struct device* temp0 = device_find ("TEMP0"); - int x = vfs_create_volume ("TEMP", FS_FAT16, temp0, true); + vfs_create_volume ("TEMP", FS_FAT16, temp0, true); proc_pid_alloc_init (); procgroup_pgid_alloc_init (); diff --git a/kernel/device/debugconsole.c b/kernel/device/debugconsole.c new file mode 100644 index 0000000..213766f --- /dev/null +++ b/kernel/device/debugconsole.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +#include +#include + +bool debugconsole_init (struct device* device, void* arg) { + (void)device, (void)arg; + return true; +} + +void debugconsole_fini (struct device* device) { (void)device; } + +int debugconsole_putstr (struct device* device, struct proc* proc, struct reschedule_ctx* rctx, + void* a1, void* a2, void* a3, void* a4) { + (void)a2, (void)a3, (void)a4, (void)device, (void)rctx; + + char* string = (char*)a1; + size_t* len = (size_t*)a2; + + if (string == NULL || len == NULL) + return -ST_BAD_ADDRESS_SPACE; + + spin_lock (&proc->lock); + int pid = proc->pid; + spin_unlock (&proc->lock); + + debugprintf ("(CPU %d; PID %d) %.*s\n", thiscpu->id, pid, (int)*len, string); + + return ST_OK; +} diff --git a/kernel/device/debugconsole.h b/kernel/device/debugconsole.h new file mode 100644 index 0000000..25f883f --- /dev/null +++ b/kernel/device/debugconsole.h @@ -0,0 +1,17 @@ +#ifndef _KERNEL_DEVICE_DEBUGCONSOLE_H +#define _KERNEL_DEVICE_DEBUGCONSOLE_H + +#include +#include +#include + +struct device; + +bool debugconsole_init (struct device* device, void* arg); + +void debugconsole_fini (struct device* device); + +int debugconsole_putstr (struct device* device, struct proc* proc, struct reschedule_ctx* rctx, + void* a1, void* a2, void* a3, void* a4); + +#endif // _KERNEL_DEVICE_DEBUGCONSOLE_H diff --git a/kernel/device/device.c b/kernel/device/device.c index 6068afb..6008e92 100644 --- a/kernel/device/device.c +++ b/kernel/device/device.c @@ -1,7 +1,7 @@ +#include #include #include #include -#include #include #include #include @@ -21,6 +21,7 @@ #include #if defined(__x86_64__) +#include #include #endif @@ -85,7 +86,14 @@ struct device* device_create (const char* key, device_op_func_t* ops, size_t ops return device; } -void terminal_device_init (void) { +static void debugconsole_device_init (void) { + device_op_func_t ops[] = { + [DEBUGCONSOLE_PUTSTR] = &debugconsole_putstr, + }; + device_create ("DEBUGCONSOLE", ops, lengthof (ops), &debugconsole_init, &debugconsole_fini, NULL); +} + +static void terminal_device_init (void) { device_op_func_t ops[] = { [TERMINAL_PUTSTR] = &terminal_putstr, [TERMINAL_DIMENSIONS] = &terminal_dimensions, @@ -93,7 +101,7 @@ void terminal_device_init (void) { device_create ("TERMINAL", ops, lengthof (ops), &terminal_init, &terminal_fini, NULL); } -void ramdisk_device_init (void) { +static void ramdisk_device_init (void) { device_op_func_t ops[] = { [XDRV_GET_SIZE] = &ramdrv_get_size, [XDRV_GET_SECTOR_SIZE] = &ramdrv_get_sector_size, @@ -136,7 +144,7 @@ void ramdisk_device_init (void) { LZ4F_freeDecompressionContext (dctx); } -void temp_device_init (void) { +static void temp_device_init (void) { device_op_func_t ops[] = { [XDRV_GET_SIZE] = &ramdrv_get_size, [XDRV_GET_SECTOR_SIZE] = &ramdrv_get_sector_size, @@ -153,7 +161,7 @@ void temp_device_init (void) { } #if defined(__x86_64__) -void ps2kb_device_init (void) { +static void ps2kb_device_init (void) { device_op_func_t ops[] = { [KB_READ_KEY] = &ps2kb_read_key, }; @@ -167,6 +175,7 @@ void devices_init (void) { terminal_device_init (); ramdisk_device_init (); temp_device_init (); + debugconsole_device_init (); #if defined(__x86_64__) ps2kb_device_init (); diff --git a/kernel/device/src.mk b/kernel/device/src.mk index 05aaeb9..662f8f0 100644 --- a/kernel/device/src.mk +++ b/kernel/device/src.mk @@ -2,13 +2,15 @@ c += device/device.c \ device/terminal.c \ device/ramdrv.c \ device/partdrv.c \ - device/partitions.c + device/partitions.c \ + device/debugconsole.c o += device/device.o \ device/terminal.o \ device/ramdrv.o \ device/partdrv.o \ - device/partitions.o + device/partitions.o \ + device/debugconsole.o ifeq ($(platform),amd64) c += device/ps2_kb.c \ diff --git a/kernel/device/terminal.h b/kernel/device/terminal.h index cbd21a1..1218a86 100644 --- a/kernel/device/terminal.h +++ b/kernel/device/terminal.h @@ -7,7 +7,6 @@ #include struct device; -struct device_op_ctx; bool terminal_init (struct device* device, void* arg); diff --git a/libaux/printf.c b/libaux/printf.c index 1435341..22f020f 100644 --- a/libaux/printf.c +++ b/libaux/printf.c @@ -1691,3 +1691,5 @@ int fctprintf (void (*out) (char c, void* extra_arg), void* extra_arg, const cha va_end (args); return ret; } + +void putchar_ (char ch) { (void)ch; } diff --git a/libdebugconsole/.gitignore b/libdebugconsole/.gitignore new file mode 100644 index 0000000..de276e3 --- /dev/null +++ b/libdebugconsole/.gitignore @@ -0,0 +1,4 @@ +*.o +*.json +docs/ +.cache/ diff --git a/libdebugconsole/Makefile b/libdebugconsole/Makefile new file mode 100644 index 0000000..06dbe6f --- /dev/null +++ b/libdebugconsole/Makefile @@ -0,0 +1,11 @@ +include ../make/ufuncs.mk + +$(eval $(call add_include,libsystem)) +$(eval $(call add_include,liballoc)) +$(eval $(call add_include,libaux)) + +cflags += -DPRINTF_INCLUDE_CONFIG_H=1 + +libname := libdebugconsole + +include ../make/lib.mk diff --git a/libdebugconsole/build/.gitignore b/libdebugconsole/build/.gitignore new file mode 100644 index 0000000..10301e2 --- /dev/null +++ b/libdebugconsole/build/.gitignore @@ -0,0 +1 @@ +*.a diff --git a/libdebugconsole/debugconsole.c b/libdebugconsole/debugconsole.c new file mode 100644 index 0000000..bb72417 --- /dev/null +++ b/libdebugconsole/debugconsole.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include +#include + +int debugconsole_print (const char* string, size_t len) { + return device_do ("DEBUGCONSOLE", DEBUGCONSOLE_PUTSTR, (void*)string, (void*)&len, NULL, NULL); +} + +void debug_printf (const char* fmt, ...) { + va_list args; + va_start (args, fmt); + + char* buf = malloc (DEBUG_PRINTF_MAX); + + if (buf == NULL) { + va_end (args); + return; + } + + buf[0] = '\0'; + int len = vsnprintf (buf, DEBUG_PRINTF_MAX, fmt, args); + + va_end (args); + + debugconsole_print (buf, len); + free (buf); +} diff --git a/libdebugconsole/debugconsole.h b/libdebugconsole/debugconsole.h new file mode 100644 index 0000000..9279baf --- /dev/null +++ b/libdebugconsole/debugconsole.h @@ -0,0 +1,10 @@ +#ifndef _LIBDEBUGCONSOLE_DEBUGCONSOLE_H +#define _LIBDEBUGCONSOLE_DEBUGCONSOLE_H + +#define DEBUG_PRINTF_MAX (16 * 1024) + +int debugconsole_print (const char* string, size_t len); + +void debug_printf (const char* fmt, ...); + +#endif // _LIBDEBUGCONSOLE_DEBUGCONSOLE_H diff --git a/libdebugconsole/src.mk b/libdebugconsole/src.mk new file mode 100644 index 0000000..b6abdc3 --- /dev/null +++ b/libdebugconsole/src.mk @@ -0,0 +1,3 @@ +c += debugconsole.c + +o += debugconsole.o diff --git a/make/libdebugconsole.mk b/make/libdebugconsole.mk new file mode 100644 index 0000000..b1643aa --- /dev/null +++ b/make/libdebugconsole.mk @@ -0,0 +1,16 @@ +all_libdebugconsole: + make -C libdebugconsole platform=$(platform) all + +all_compiledb_libdebugconsole: + bear --output libdebugconsole/compile_commands.json -- make -C libdebugconsole platform=$(platform) all + +clean_libdebugconsole: + make -C libdebugconsole platform=$(platform) clean + +format_libdebugconsole: + make -C libdebugconsole platform=$(platform) format + +docs_libdebugconsole: + make -C libdebugconsole platform=$(platform) docs + +.PHONY: all_libdebugconsole clean_libdebugconsole format_libdebugconsole docs_libdebugconsole all_compiledb_libdebugconsole