From f2b4acb2fe6a366288b19843e0d2678b8590bdf4 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sun, 25 Apr 2021 13:43:14 +0200 Subject: Chu chuu, using the bus for everything now! Well, I know: bus != train. But I like trains. So I don't care. Go away! --- libs/libc/inc/assert.h | 17 +++++++---- libs/libc/inc/print.h | 5 +++- libs/libc/inc/sys.h | 15 ++++++++-- libs/libc/print.c | 59 +++++++++++++++++++++---------------- libs/libc/rand.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ libs/libc/random.c | 80 -------------------------------------------------- libs/libc/sys.c | 2 +- 7 files changed, 141 insertions(+), 117 deletions(-) create mode 100644 libs/libc/rand.c delete mode 100644 libs/libc/random.c (limited to 'libs/libc') diff --git a/libs/libc/inc/assert.h b/libs/libc/inc/assert.h index 9621e36..fb5a857 100644 --- a/libs/libc/inc/assert.h +++ b/libs/libc/inc/assert.h @@ -8,15 +8,20 @@ #ifdef KERNEL #include #define assert(exp) \ - if (!(exp)) { \ - printf("%s:%d: %s: Kernel assertion '%s' failed\n", __FILE__, __LINE__, __func__, \ - #exp); \ - __asm__ volatile("cli\nhlt"); \ + { \ + if (!(exp)) { \ + printf("%s:%d: %s: Kernel assertion '%s' failed\n", __FILE__, __LINE__, \ + __func__, #exp); \ + __asm__ volatile("cli\nhlt"); \ + } \ } #elif defined(USER) #define assert(exp) \ - if (!(exp)) \ - err(1, "%s:%d: %s: Assertion '%s' failed\n", __FILE__, __LINE__, __func__, #exp); + { \ + if (!(exp)) \ + err(1, "%s:%d: %s: Assertion '%s' failed\n", __FILE__, __LINE__, __func__, \ + #exp); \ + } #endif #endif diff --git a/libs/libc/inc/print.h b/libs/libc/inc/print.h index 1d85c33..6d959c3 100644 --- a/libs/libc/inc/print.h +++ b/libs/libc/inc/print.h @@ -14,13 +14,16 @@ int print(const char *str) NONNULL; NORETURN void panic(const char *format, ...) NONNULL; #ifdef USER +#include int vfprintf(const char *path, const char *format, va_list ap) NONNULL; +int viprintf(enum io_type io, const char *format, va_list ap) NONNULL; int fprintf(const char *path, const char *format, ...) NONNULL; +int iprintf(enum io_type io, const char *format, ...) NONNULL; int log(const char *format, ...) NONNULL; void err(int code, const char *format, ...) NONNULL; #else #include -int print_app(enum stream_defaults id, const char *proc_name, const char *str) NONNULL; +int print_prefix(void); void print_trace(u32 count); #endif diff --git a/libs/libc/inc/sys.h b/libs/libc/inc/sys.h index 72a8029..8c8e217 100644 --- a/libs/libc/inc/sys.h +++ b/libs/libc/inc/sys.h @@ -15,6 +15,7 @@ #define SYS_BOOT_SHUTDOWN 0xdead enum sys { + SYS_MIN, SYS_ALLOC, // Allocate memory SYS_SHACCESS, // Access shared memory SYS_FREE, // Free memory @@ -29,10 +30,12 @@ enum sys { SYS_EXIT, // Exit current process SYS_BOOT, // Boot functions (e.g. reboot/shutdown) SYS_YIELD, // Switch to next process + SYS_MAX, }; enum io_type { IO_MIN, + IO_LOGGER, IO_FRAMEBUFFER, IO_NETWORK, IO_KEYBOARD, @@ -44,8 +47,14 @@ enum io_type { // I/O control declarations #define IOCTL_FB_GET 0 -#define IOCTL_BUS_CONNECT 0 -#define IOCTL_BUS_REGISTER 1 +#define IOCTL_BUS_CONNECT_BUS 0 +#define IOCTL_BUS_CONNECT_CONN 1 +#define IOCTL_BUS_REGISTER 2 + +struct bus_header { + u32 conn; + // Data starts here +}; struct event_keyboard { u32 magic; @@ -84,7 +93,7 @@ res exec(const char *path, ...) ATTR((nonnull(1))) SENTINEL; res io_poll(enum io_type *devs) NONNULL; res io_read(enum io_type io, void *buf, u32 offset, u32 count) NONNULL; -res io_write(enum io_type io, void *buf, u32 offset, u32 count) NONNULL; +res io_write(enum io_type io, const void *buf, u32 offset, u32 count) NONNULL; res io_control(enum io_type io, ...); res yield(void); diff --git a/libs/libc/print.c b/libs/libc/print.c index 27048bd..2d08fdb 100644 --- a/libs/libc/print.c +++ b/libs/libc/print.c @@ -7,6 +7,15 @@ #include #include +#define RED "\x1B[1;31m" +#define GRN "\x1B[1;32m" +#define YEL "\x1B[1;33m" +#define BLU "\x1B[1;34m" +#define MAG "\x1B[1;35m" +#define CYN "\x1B[1;36m" +#define WHT "\x1B[1;37m" +#define RES "\x1B[0m" + static void append(char *dest, char *src, int index) { for (u32 i = index; i < strlen(src) + index; i++) @@ -98,13 +107,10 @@ int snprintf(char *str, u32 size, const char *format, ...) #ifdef USER #include -#define PATH_OUT "/proc/self/io/out" -#define PATH_LOG "/proc/self/io/log" -#define PATH_ERR "/proc/self/io/err" int vprintf(const char *format, va_list ap) { - return vfprintf(PATH_OUT, format, ap); + return viprintf(IO_LOGGER, format, ap); } int vfprintf(const char *path, const char *format, va_list ap) @@ -114,6 +120,13 @@ int vfprintf(const char *path, const char *format, va_list ap) return write(path, buf, 0, len); } +int viprintf(enum io_type io, const char *format, va_list ap) +{ + char buf[1024] = { 0 }; + int len = vsnprintf(buf, sizeof(buf), format, ap); + return io_write(io, buf, 0, len); +} + int fprintf(const char *path, const char *format, ...) { va_list ap; @@ -124,6 +137,16 @@ int fprintf(const char *path, const char *format, ...) return len; } +int iprintf(enum io_type io, const char *format, ...) +{ + va_list ap; + va_start(ap, format); + int len = viprintf(io, format, ap); + va_end(ap); + + return len; +} + int printf(const char *format, ...) { va_list ap; @@ -138,7 +161,7 @@ int log(const char *format, ...) { va_list ap; va_start(ap, format); - int len = vfprintf(PATH_LOG, format, ap); + int len = viprintf(IO_LOGGER, format, ap); va_end(ap); return len; @@ -150,14 +173,14 @@ NORETURN void err(int code, const char *format, ...) log("ERRNO: %d (%s)\n", errno, strerror(errno)); va_list ap; va_start(ap, format); - vfprintf(PATH_ERR, format, ap); + viprintf(IO_LOGGER, format, ap); va_end(ap); exit(code); } int print(const char *str) { - return write(PATH_OUT, str, 0, strlen(str)); + return io_write(IO_LOGGER, str, 0, strlen(str)); } #else @@ -169,15 +192,6 @@ int print(const char *str) #include #include -#define RED "\x1B[1;31m" -#define GRN "\x1B[1;32m" -#define YEL "\x1B[1;33m" -#define BLU "\x1B[1;34m" -#define MAG "\x1B[1;35m" -#define CYN "\x1B[1;36m" -#define WHT "\x1B[1;37m" -#define RES "\x1B[0m" - static void print_kernel(const char *str) { serial_print(RED); @@ -204,18 +218,11 @@ int printf(const char *format, ...) return len; } -int print_app(enum stream_defaults id, const char *proc_name, const char *str) +int print_prefix(void) { - if (id == STREAM_LOG) - serial_print(CYN "[LOG] to "); - else if (id == STREAM_ERR) - serial_print(YEL "[ERR] to "); - serial_print(proc_name); + serial_print(CYN "[LOG] to "); + serial_print(proc_current()->name); serial_print(": "); - stac(); - serial_print(str); - clac(); - serial_print(RES); return 1; } diff --git a/libs/libc/rand.c b/libs/libc/rand.c new file mode 100644 index 0000000..268a21f --- /dev/null +++ b/libs/libc/rand.c @@ -0,0 +1,80 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#include +#include +#include + +#ifdef KERNEL +#include +#endif + +static u32 g_seed = 1; + +void srand(u32 seed) +{ + g_seed = seed; +} + +static u32 rand_default(void) +{ + g_seed = g_seed * 1103515245 + 12345; + return (g_seed >> 16) & 0x7FFF; +} + +static u32 rand_once(void) +{ +#ifdef KERNEL + u32 val = 0; + if (cpu_extended_features.ebx & CPUID_EXT_FEAT_EBX_RDSEED) { + __asm__ volatile("1:\n" + "rdseed %0\n" + "jnc 1b\n" + : "=r"(val)); + } else if (cpu_features.ecx & CPUID_FEAT_ECX_RDRND) { + __asm__ volatile("1:\n" + "rdrand %0\n" + "jnc 1b\n" + : "=r"(val)); + } else { + val = rand_default(); + } + + return val; +#else + return rand_default(); +#endif +} + +u32 rand(void) +{ + u32 ret = 0; + ret |= (rand_once() & (0xffu << 0)); + ret |= (rand_once() & (0xffu << 8)); + ret |= (rand_once() & (0xffu << 16)); + ret |= (rand_once() & (0xffu << 24)); + return ret; +} + +void rand_fill(void *buf, u32 size) +{ + for (u32 i = 0; i < size; i++) + ((u8 *)buf)[i] = rand_once() & 0xff; +} + +char *randstr(u32 size) +{ + if (!size) + return NULL; + + char *buf = malloc(size + 1); + const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + + size--; + for (u32 i = 0; i < size; i++) { + int key = rand() % (sizeof(charset) - 1); + buf[i] = charset[key]; + } + buf[size] = '\0'; + + return buf; +} diff --git a/libs/libc/random.c b/libs/libc/random.c deleted file mode 100644 index 268a21f..0000000 --- a/libs/libc/random.c +++ /dev/null @@ -1,80 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#include -#include -#include - -#ifdef KERNEL -#include -#endif - -static u32 g_seed = 1; - -void srand(u32 seed) -{ - g_seed = seed; -} - -static u32 rand_default(void) -{ - g_seed = g_seed * 1103515245 + 12345; - return (g_seed >> 16) & 0x7FFF; -} - -static u32 rand_once(void) -{ -#ifdef KERNEL - u32 val = 0; - if (cpu_extended_features.ebx & CPUID_EXT_FEAT_EBX_RDSEED) { - __asm__ volatile("1:\n" - "rdseed %0\n" - "jnc 1b\n" - : "=r"(val)); - } else if (cpu_features.ecx & CPUID_FEAT_ECX_RDRND) { - __asm__ volatile("1:\n" - "rdrand %0\n" - "jnc 1b\n" - : "=r"(val)); - } else { - val = rand_default(); - } - - return val; -#else - return rand_default(); -#endif -} - -u32 rand(void) -{ - u32 ret = 0; - ret |= (rand_once() & (0xffu << 0)); - ret |= (rand_once() & (0xffu << 8)); - ret |= (rand_once() & (0xffu << 16)); - ret |= (rand_once() & (0xffu << 24)); - return ret; -} - -void rand_fill(void *buf, u32 size) -{ - for (u32 i = 0; i < size; i++) - ((u8 *)buf)[i] = rand_once() & 0xff; -} - -char *randstr(u32 size) -{ - if (!size) - return NULL; - - char *buf = malloc(size + 1); - const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - - size--; - for (u32 i = 0; i < size; i++) { - int key = rand() % (sizeof(charset) - 1); - buf[i] = charset[key]; - } - buf[size] = '\0'; - - return buf; -} diff --git a/libs/libc/sys.c b/libs/libc/sys.c index b0e6e93..cf2165a 100644 --- a/libs/libc/sys.c +++ b/libs/libc/sys.c @@ -138,7 +138,7 @@ res io_read(enum io_type io, void *buf, u32 offset, u32 count) return sys4(SYS_IOREAD, (int)io, (int)buf, (int)offset, (int)count); } -res io_write(enum io_type io, void *buf, u32 offset, u32 count) +res io_write(enum io_type io, const void *buf, u32 offset, u32 count) { return sys4(SYS_IOWRITE, (int)io, (int)buf, (int)offset, (int)count); } -- cgit v1.2.3