From 6761cc071879adf32c880f2796a717a2d3aac265 Mon Sep 17 00:00:00 2001 From: able Date: Wed, 20 Dec 2023 21:48:55 -0600 Subject: [PATCH] init commit --- .gitignore | 1 + build.sh | 1 + run.sh | 1 + shell.nix | 7 ++ src/ableos_std/ableos_std.c | 15 ++++ src/ableos_std/arguments.c | 13 +++ src/ableos_std/ecalls.c | 12 +++ src/ableos_std/process.c | 36 ++++++++ src/ableos_std/services/discovery_service.c | 3 + src/ableos_std/services/doc.md | 1 + src/ableos_std/services/log.c | 41 +++++++++ src/ableos_std/types/bool.h | 7 ++ src/ableos_std/types/int.h | 9 ++ src/ableos_std/types/services.h | 10 +++ src/ableos_std/types/string/string.c | 30 +++++++ src/ableos_std/types/string/string.h | 4 + src/ableos_std/types/sys_id.h | 23 +++++ src/main.c | 94 +++++++++++++++++++++ 18 files changed, 308 insertions(+) create mode 100644 .gitignore create mode 100755 build.sh create mode 100755 run.sh create mode 100644 shell.nix create mode 100644 src/ableos_std/ableos_std.c create mode 100644 src/ableos_std/arguments.c create mode 100644 src/ableos_std/ecalls.c create mode 100644 src/ableos_std/process.c create mode 100644 src/ableos_std/services/discovery_service.c create mode 100644 src/ableos_std/services/doc.md create mode 100644 src/ableos_std/services/log.c create mode 100644 src/ableos_std/types/bool.h create mode 100644 src/ableos_std/types/int.h create mode 100644 src/ableos_std/types/services.h create mode 100644 src/ableos_std/types/string/string.c create mode 100644 src/ableos_std/types/string/string.h create mode 100644 src/ableos_std/types/sys_id.h create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..07ed706 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/* \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..486a622 --- /dev/null +++ b/build.sh @@ -0,0 +1 @@ +clang src/main.c -o build/main \ No newline at end of file diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..8a4d948 --- /dev/null +++ b/run.sh @@ -0,0 +1 @@ +./build/main \ No newline at end of file diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..56ac78e --- /dev/null +++ b/shell.nix @@ -0,0 +1,7 @@ +{ pkgs ? import { } }: +pkgs.mkShell { + nativeBuildInputs = with pkgs.buildPackages; [ + gnumake + libunwind + ]; +} diff --git a/src/ableos_std/ableos_std.c b/src/ableos_std/ableos_std.c new file mode 100644 index 0000000..28e9015 --- /dev/null +++ b/src/ableos_std/ableos_std.c @@ -0,0 +1,15 @@ +#include "types/bool.h" +#include "types/int.h" + +#include "types/string/string.h" +#include "types/string/string.c" + +#include "types/sys_id.h" +#include "types/services.h" + +#include "ecalls.c" + +#include "services/log.c" + +#include "arguments.c" +#include "process.c" \ No newline at end of file diff --git a/src/ableos_std/arguments.c b/src/ableos_std/arguments.c new file mode 100644 index 0000000..01031bf --- /dev/null +++ b/src/ableos_std/arguments.c @@ -0,0 +1,13 @@ +typedef struct { + String key; + String value; +} Argument; + +Argument get_arguments() { + Argument args[0]; + + return (Argument) { + .key = from_c_str("arch"), + .value = from_c_str("x86-64") + }; +} diff --git a/src/ableos_std/ecalls.c b/src/ableos_std/ecalls.c new file mode 100644 index 0000000..f918dfc --- /dev/null +++ b/src/ableos_std/ecalls.c @@ -0,0 +1,12 @@ +void ipc_send(BufferID buffer_id, const char* ptr, unsigned int length){ + // TODO: reach out to Erin and see about embedding HBASM +} + +void ipc_recv(BufferID buffer_id){ + // TODO: reach out to Erin and see about embedding HBASM +} + +// BufferID ipc_make_buffer(){ +// // TODO: Formalize the method of specifying a buffer protocol +// // TODO: reach out to Erin and see about embedding HBASM +// } \ No newline at end of file diff --git a/src/ableos_std/process.c b/src/ableos_std/process.c new file mode 100644 index 0000000..63beea2 --- /dev/null +++ b/src/ableos_std/process.c @@ -0,0 +1,36 @@ + + +typedef enum { + Success, + NoPermission, + NoProcess, + + AlreadyRunning, +} ProcessResponse; + +ProcessID new_id(u64 host, u64 local) { + + return (ProcessID) { + .host = host, + .local = local + }; +} +// Kill a process +ProcessResponse terminate_process(ProcessID pid) { + return NoPermission; +} + +// Request the kernel to create a new empty process +// A process is not started as soon as it is created +// This gives the starting process time to set it up +ProcessID create_process() { + trace("New process created"); + return new_id(0,0); +} + +// Start execution of a created process +ProcessResponse start_process(ProcessID pid) { + trace("process started"); + + return AlreadyRunning; +} diff --git a/src/ableos_std/services/discovery_service.c b/src/ableos_std/services/discovery_service.c new file mode 100644 index 0000000..912390a --- /dev/null +++ b/src/ableos_std/services/discovery_service.c @@ -0,0 +1,3 @@ +Services locate_services() { + +} \ No newline at end of file diff --git a/src/ableos_std/services/doc.md b/src/ableos_std/services/doc.md new file mode 100644 index 0000000..c5c36d5 --- /dev/null +++ b/src/ableos_std/services/doc.md @@ -0,0 +1 @@ +A folder for implementations of interacting with system or kernel provided services diff --git a/src/ableos_std/services/log.c b/src/ableos_std/services/log.c new file mode 100644 index 0000000..a0e9b6b --- /dev/null +++ b/src/ableos_std/services/log.c @@ -0,0 +1,41 @@ +// Log Levels +typedef enum { + ERROR = 0, + WARN = 1, + INFO = 2, + DEBUG = 3, + TRACE = 4, +} Level; + +// An inner log function +// Call into the IPC log service from here +void inner_log(Level level, const char* message, const char* file, int line) { + const char* lvl_str = ""; + switch (level) { + case ERROR: + lvl_str = "ERROR"; + break; + case WARN: + lvl_str = "WARN"; + break; + case INFO: + lvl_str = "INFO"; + break; + case DEBUG: + lvl_str = "DEBUG"; + break; + case TRACE: + lvl_str = "TRACE"; + break; + } + BufferID log_buffer = construct_buffer_id(0, 1); + ipc_send(log_buffer, message, c_str_len(message)); + + printf("%s [%s:%d]: %s\r\n", lvl_str, file, line, message); +} + +#define error(MSG) inner_log(0, (MSG), __FILE__, __LINE__) +#define warn(MSG) inner_log(1, (MSG), __FILE__, __LINE__) +#define info(MSG) inner_log(2, (MSG), __FILE__, __LINE__) +#define debug(MSG) inner_log(3, (MSG), __FILE__, __LINE__) +#define trace(MSG) inner_log(4, (MSG), __FILE__, __LINE__) \ No newline at end of file diff --git a/src/ableos_std/types/bool.h b/src/ableos_std/types/bool.h new file mode 100644 index 0000000..100ff97 --- /dev/null +++ b/src/ableos_std/types/bool.h @@ -0,0 +1,7 @@ +typedef int bool; + +#define True 1 +#define true True + +#define False 0 +#define false False \ No newline at end of file diff --git a/src/ableos_std/types/int.h b/src/ableos_std/types/int.h new file mode 100644 index 0000000..061f47c --- /dev/null +++ b/src/ableos_std/types/int.h @@ -0,0 +1,9 @@ +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +typedef unsigned long long u64; + +typedef char i8; +typedef short i16; +typedef int i32; +typedef long long i64; \ No newline at end of file diff --git a/src/ableos_std/types/services.h b/src/ableos_std/types/services.h new file mode 100644 index 0000000..57e7c84 --- /dev/null +++ b/src/ableos_std/types/services.h @@ -0,0 +1,10 @@ +typedef struct { + +} Protocol; + +typedef struct { + Protocol protocol; + BufferID buffer; +} Service; + +typedef Service Services[]; \ No newline at end of file diff --git a/src/ableos_std/types/string/string.c b/src/ableos_std/types/string/string.c new file mode 100644 index 0000000..e665485 --- /dev/null +++ b/src/ableos_std/types/string/string.c @@ -0,0 +1,30 @@ + + +inline static u32 c_str_len(char const* ptr) { + if (ptr == NULL) { + return 0; + } + u32 len = 0; + while (*ptr != '\0') { + ++ptr; + ++len; + } + return len; +} + +String from_c_str(char* ptr) { + return (String) { + .ptr = ptr, + .len = c_str_len(ptr) + }; +} + +bool string_eq(String lhs, String rhs) { + if (lhs.ptr == rhs.ptr) { + if (lhs.len == rhs.len) { + return true; + } + return false; + } + return false; +} \ No newline at end of file diff --git a/src/ableos_std/types/string/string.h b/src/ableos_std/types/string/string.h new file mode 100644 index 0000000..7b292fc --- /dev/null +++ b/src/ableos_std/types/string/string.h @@ -0,0 +1,4 @@ +typedef struct { + char* ptr; + u32 len; +} String; \ No newline at end of file diff --git a/src/ableos_std/types/sys_id.h b/src/ableos_std/types/sys_id.h new file mode 100644 index 0000000..7176500 --- /dev/null +++ b/src/ableos_std/types/sys_id.h @@ -0,0 +1,23 @@ +// All System Resource IDs are declared here +// as well as compound IDs + +#define HostID u64 +#define LocalID u64 + +typedef struct { + HostID host; + LocalID local; +} ProcessID; + +typedef struct { + HostID host_id; + LocalID local_id; +} BufferID; +BufferID construct_buffer_id(HostID host_id, LocalID local_id){ + return (BufferID) { + .host_id = host_id, + .local_id = local_id + }; +} + + diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..2a95091 --- /dev/null +++ b/src/main.c @@ -0,0 +1,94 @@ +#include +#include "ableos_std/ableos_std.c" + +typedef enum { + // KeyUp is the default state + KeyUp = 0, + KeyDown = 0x43, +} KeyState; + +typedef struct { + KeyState kstate; + // TODO: https://github.com/rust-windowing/winit/issues/2995 + char key; +} KeyboardEvent; + + +void send_keyb_event_ipc(KeyboardEvent kevent){ + +} + + + +KeyboardEvent get_keyboard_event(){ + /* + li64(r1, 5); + li64(r2, 0x64); + eca(); + */ + + return (KeyboardEvent) { + .kstate = KeyUp, + .key = 'c' + }; + +} + +typedef enum { + ScanCodeSet1 = 0x43, + ScanCodeSet2 = 0x41, + ScanCodeSet3 = 0x3f, + + ACK = 0xFA, + Resend = 0xFE, +} PS2Response; + +typedef enum { + GetCurrent = 0, + CommandScanCodeSet1 = 1, + CommandScanCodeSet2 = 2, + CommandScanCodeSet3 = 3, +} PS2ScancodeSet; + + +PS2Response set_led() { + return Resend; +} + +PS2Response scancode_command(PS2ScancodeSet scancode) { + return Resend; +} + + +// Replacement for the PS/2 Keyboard Driver on ableOS +int main() { + Argument args = get_arguments(); + + int b = string_eq(args.value, from_c_str("x86-64")); + if (b == 1) { + info("running on X86"); + } else { + error("This driver cannot run on non-x86_64 machines"); + // terminate_process(); + } + + info("abc"); + bool running = false; + while (running) { + // check incoming buffer for keyb settings changes + bool changes = false; + // if they exist apply changes + if (changes) { + set_led(); + } + // check keyb state + get_keyboard_event(); + // push keyb state to a buffer + } + + create_process(); + + + return 0; +}; +