feat: (device) dev_null and dev_rand support
authorMinep <zelong56@gmail.com>
Sun, 20 Nov 2022 15:04:28 +0000 (15:04 +0000)
committerMinep <zelong56@gmail.com>
Sun, 20 Nov 2022 15:04:28 +0000 (15:04 +0000)
feat: versioning stuff

12 files changed:
lunaix-os/.vscode/c_cpp_properties.json
lunaix-os/config/make-cc
lunaix-os/flags.h
lunaix-os/hal/cpu.c
lunaix-os/includes/hal/rnd.h [new file with mode: 0644]
lunaix-os/includes/lunaix/device.h
lunaix-os/includes/lunaix/fs.h
lunaix-os/kernel/device/builtin/devbuiltin.c [new file with mode: 0644]
lunaix-os/kernel/device/builtin/devnull.c [new file with mode: 0644]
lunaix-os/kernel/device/builtin/devrand.c [new file with mode: 0644]
lunaix-os/kernel/fs/fs_export.c
lunaix-os/kernel/k_init.c

index 574a1bd865aa9b215005dd473f638bc7222cbc55..c68b4c707b47f8732525c3186b1c8253c6b996fa 100644 (file)
@@ -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",
index ed1f6786021a640316dc2fba02a091fb78d12a61..4491370c42fd1fa3fa5570f97e98f6398784df31 100644 (file)
@@ -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 \
index d4262dfa4fbbe921fc33da1672b4025991d8ef0e..61d12c92e729edc1e20f28751ab667919ba5d765 100644 (file)
@@ -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.
index 9517445e1e2b31938391c86254f3ffffd9fb4452..b8a5e9b422f510dda9e226de403616606b6f7ccf 100644 (file)
@@ -1,5 +1,6 @@
 #include <cpuid.h>
 #include <hal/cpu.h>
+#include <hal/rnd.h>
 #include <stdint.h>
 
 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 (file)
index 0000000..8ab544a
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef __LUNAIX_RND_H
+#define __LUNAIX_RND_H
+
+#include <lunaix/types.h>
+
+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 */
index 1c2445b04f84930b86f699d34daab3d3964670d0..ee56b1da9d9a19592fa0274b5de5c733e87fea32 100644 (file)
@@ -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 */
index 5a5cb79514e51d66198a4f13e0ce040630b8f544..3ec85f5c2b711fce57f8b273af22694d0c1da571 100644 (file)
@@ -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 (file)
index 0000000..5c7b9b1
--- /dev/null
@@ -0,0 +1,14 @@
+#include <lunaix/device.h>
+
+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 (file)
index 0000000..977c385
--- /dev/null
@@ -0,0 +1,39 @@
+#include <lunaix/device.h>
+
+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 (file)
index 0000000..7526601
--- /dev/null
@@ -0,0 +1,31 @@
+#include <hal/rnd.h>
+#include <lunaix/device.h>
+#include <lunaix/syslog.h>
+
+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
index 13d65577fc62f9c9bf15af52d6db977f9f9e89e3..046204f026e556b5e4736433710df8362bc29e83 100644 (file)
@@ -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
index 6274dd8fa5e88e5a96efe87502a8cef5a5d1ffcc..e0730e32fef15ab7aeaadb12f159dcbbed86965a 100644 (file)
@@ -93,6 +93,7 @@ _kernel_init()
     vfs_mount("/task", "taskfs", NULL, MNT_RO);
 
     lxconsole_spawn_ttydev();
+    device_init_builtin();
 
     syscall_install();