From a9cfeffd21239290ab1da46a952fe5789ae3f6de Mon Sep 17 00:00:00 2001 From: Minep Date: Sun, 20 Nov 2022 15:04:28 +0000 Subject: [PATCH 1/1] feat: (device) dev_null and dev_rand support feat: versioning stuff --- lunaix-os/.vscode/c_cpp_properties.json | 3 +- lunaix-os/config/make-cc | 2 +- lunaix-os/flags.h | 8 ++++ lunaix-os/hal/cpu.c | 9 +++++ lunaix-os/includes/hal/rnd.h | 22 +++++++++++ lunaix-os/includes/lunaix/device.h | 3 ++ lunaix-os/includes/lunaix/fs.h | 1 - lunaix-os/kernel/device/builtin/devbuiltin.c | 14 +++++++ lunaix-os/kernel/device/builtin/devnull.c | 39 ++++++++++++++++++++ lunaix-os/kernel/device/builtin/devrand.c | 31 ++++++++++++++++ lunaix-os/kernel/fs/fs_export.c | 15 ++++++++ lunaix-os/kernel/k_init.c | 1 + 12 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 lunaix-os/includes/hal/rnd.h create mode 100644 lunaix-os/kernel/device/builtin/devbuiltin.c create mode 100644 lunaix-os/kernel/device/builtin/devnull.c create mode 100644 lunaix-os/kernel/device/builtin/devrand.c diff --git a/lunaix-os/.vscode/c_cpp_properties.json b/lunaix-os/.vscode/c_cpp_properties.json index 574a1bd..c68b4c7 100644 --- a/lunaix-os/.vscode/c_cpp_properties.json +++ b/lunaix-os/.vscode/c_cpp_properties.json @@ -8,7 +8,8 @@ "compilerArgs": [ "-ffreestanding", "-D__ARCH_IA32", - "-D__LUNAIXOS_DEBUG__" + "-D__LUNAIXOS_DEBUG__", + "-include flags.h" ], "defines": [], "compilerPath": "${HOME}/opt/cross-compiler/bin/i686-elf-gcc", diff --git a/lunaix-os/config/make-cc b/lunaix-os/config/make-cc index ed1f678..4491370 100644 --- a/lunaix-os/config/make-cc +++ b/lunaix-os/config/make-cc @@ -2,7 +2,7 @@ CC := i686-elf-gcc AS := i686-elf-as -ARCH_OPT := -D__ARCH_IA32 +ARCH_OPT := -D__ARCH_IA32 -include flags.h O := -O2 W := -Wall -Wextra -Wno-unknown-pragmas \ -Wno-unused-function \ diff --git a/lunaix-os/flags.h b/lunaix-os/flags.h index d4262df..61d12c9 100644 --- a/lunaix-os/flags.h +++ b/lunaix-os/flags.h @@ -1,6 +1,14 @@ #ifndef __LUNAIX_FLAGS_H #define __LUNAIX_FLAGS_H +#ifdef __ARCH_IA32 +#define PLATFORM_TARGET "x86_32" +#else +#define PLATFORM_TARGET "unknown" +#endif + +#define LUNAIX_VER "0.0.1-dev" + /* Uncomment below to force LunaixOS use kernel page table when context switch to kernel space NOTE: This will make the kernel global. diff --git a/lunaix-os/hal/cpu.c b/lunaix-os/hal/cpu.c index 9517445..b8a5e9b 100644 --- a/lunaix-os/hal/cpu.c +++ b/lunaix-os/hal/cpu.c @@ -1,5 +1,6 @@ #include #include +#include #include void @@ -69,4 +70,12 @@ void cpu_wrmsr(u32_t msr_idx, u32_t reg_high, u32_t reg_low) { asm volatile("wrmsr" : : "d"(reg_high), "a"(reg_low), "c"(msr_idx)); +} + +int +rnd_is_supported() +{ + reg32 eax, ebx, ecx, edx; + __get_cpuid(0x01, &eax, &ebx, &ecx, &edx); + return (ecx & (1 << 30)); } \ No newline at end of file diff --git a/lunaix-os/includes/hal/rnd.h b/lunaix-os/includes/hal/rnd.h new file mode 100644 index 0000000..8ab544a --- /dev/null +++ b/lunaix-os/includes/hal/rnd.h @@ -0,0 +1,22 @@ +#ifndef __LUNAIX_RND_H +#define __LUNAIX_RND_H + +#include + +static inline void +rnd_fill(void* data, size_t len) +{ + asm volatile("1:\n" + "rdrand %%eax\n" + "movl %%eax, (%0)\n" + "addl $4, %%eax\n" + "subl $4, %1\n" + "jnz 1b" ::"r"((ptr_t)data), + "r"((len & ~0x3)) + : "%al"); +} + +int +rnd_is_supported(); + +#endif /* __LUNAIX_RND_H */ diff --git a/lunaix-os/includes/lunaix/device.h b/lunaix-os/includes/lunaix/device.h index 1c2445b..ee56b1d 100644 --- a/lunaix-os/includes/lunaix/device.h +++ b/lunaix-os/includes/lunaix/device.h @@ -66,4 +66,7 @@ device_getbyname(struct device* root_dev, const char* name, size_t len); struct device* device_getbyoffset(struct device* root_dev, int pos); +void +device_init_builtin(); + #endif /* __LUNAIX_DEVICE_H */ diff --git a/lunaix-os/includes/lunaix/fs.h b/lunaix-os/includes/lunaix/fs.h index 5a5cb79..3ec85f5 100644 --- a/lunaix-os/includes/lunaix/fs.h +++ b/lunaix-os/includes/lunaix/fs.h @@ -95,7 +95,6 @@ struct v_superblock struct device* dev; struct v_dnode* root; struct filesystem* fs; - u32_t iobuf_size; struct hbucket* i_cache; void* data; struct diff --git a/lunaix-os/kernel/device/builtin/devbuiltin.c b/lunaix-os/kernel/device/builtin/devbuiltin.c new file mode 100644 index 0000000..5c7b9b1 --- /dev/null +++ b/lunaix-os/kernel/device/builtin/devbuiltin.c @@ -0,0 +1,14 @@ +#include + +extern void +devbuiltin_init_rand(); + +extern void +devbuiltin_init_null(); + +void +device_init_builtin() +{ + devbuiltin_init_null(); + devbuiltin_init_rand(); +} \ No newline at end of file diff --git a/lunaix-os/kernel/device/builtin/devnull.c b/lunaix-os/kernel/device/builtin/devnull.c new file mode 100644 index 0000000..977c385 --- /dev/null +++ b/lunaix-os/kernel/device/builtin/devnull.c @@ -0,0 +1,39 @@ +#include + +int +__null_wr_pg(struct device* dev, void* buf, size_t offset) +{ + // do nothing + return PG_SIZE; +} + +int +__null_wr(struct device* dev, void* buf, size_t offset, size_t len) +{ + // do nothing + return len; +} + +int +__null_rd_pg(struct device* dev, void* buf, size_t offset) +{ + // do nothing + return 0; +} + +int +__null_rd(struct device* dev, void* buf, size_t offset, size_t len) +{ + // do nothing + return 0; +} + +void +devbuiltin_init_null() +{ + struct device* devnull = device_addseq(NULL, NULL, "null"); + devnull->write_page = __null_wr_pg; + devnull->write = __null_wr; + devnull->read_page = __null_rd_pg; + devnull->read = __null_rd; +} \ No newline at end of file diff --git a/lunaix-os/kernel/device/builtin/devrand.c b/lunaix-os/kernel/device/builtin/devrand.c new file mode 100644 index 0000000..7526601 --- /dev/null +++ b/lunaix-os/kernel/device/builtin/devrand.c @@ -0,0 +1,31 @@ +#include +#include +#include + +LOG_MODULE("rand") + +int +__rand_rd_pg(struct device* dev, void* buf, size_t offset) +{ + rnd_fill(buf, PG_SIZE); + return PG_SIZE; +} + +int +__rand_rd(struct device* dev, void* buf, size_t offset, size_t len) +{ + rnd_fill(buf, len); + return len; +} + +void +devbuiltin_init_rand() +{ + if (!rnd_is_supported()) { + kprintf(KWARN "not supported.\n"); + return; + } + struct device* devrand = device_addseq(NULL, NULL, "rand"); + devrand->read = __rand_rd; + devrand->read_page = __rand_rd_pg; +} \ No newline at end of file diff --git a/lunaix-os/kernel/fs/fs_export.c b/lunaix-os/kernel/fs/fs_export.c index 13d6557..046204f 100644 --- a/lunaix-os/kernel/fs/fs_export.c +++ b/lunaix-os/kernel/fs/fs_export.c @@ -37,6 +37,18 @@ __mount_reset(struct twimap* map) map->index = container_of(all_mnts.next, struct v_mount, list); } +void +__version_rd(struct twimap* map) +{ + twimap_printf(map, + "LunaixOS version %s (%s-gnu-gcc %s) %s %s", + LUNAIX_VER, + PLATFORM_TARGET, + __VERSION__, + __DATE__, + __TIME__); +} + void vfs_export_attributes() { @@ -44,4 +56,7 @@ vfs_export_attributes() map->read = __mount_read; map->go_next = __mount_next; map->reset = __mount_reset; + + map = twifs_mapping(NULL, NULL, "version"); + map->read = __version_rd; } \ No newline at end of file diff --git a/lunaix-os/kernel/k_init.c b/lunaix-os/kernel/k_init.c index 6274dd8..e0730e3 100644 --- a/lunaix-os/kernel/k_init.c +++ b/lunaix-os/kernel/k_init.c @@ -93,6 +93,7 @@ _kernel_init() vfs_mount("/task", "taskfs", NULL, MNT_RO); lxconsole_spawn_ttydev(); + device_init_builtin(); syscall_install(); -- 2.27.0