Browse Source

Automatically generate syscalls.h and syscalls.c as well

coderain 1 year ago
parent
commit
c2c026e397
10 changed files with 89 additions and 205 deletions
  1. 4 0
      .gitignore
  2. 17 7
      Makefile
  3. 3 0
      kernel/.gitignore
  4. 1 1
      kernel/Makefile
  5. 2 71
      kernel/src/syscalls.c
  6. 2 1
      library/.gitignore
  7. 0 7
      library/Makefile
  8. 0 46
      library/gen_wrappers.awk
  9. 1 72
      sdk/syscalls.h
  10. 59 0
      syscalls.awk

+ 4 - 0
.gitignore

@@ -18,3 +18,7 @@
 !manager/*
 !sdk
 !sdk/*
+!syscalls.awk
+
+# Exclude generate files:
+sdk/syscall_enum.h

+ 17 - 7
Makefile

@@ -1,14 +1,16 @@
-.PHONY: all clean crt kernel library manager tests
+.PHONY: all clean crt kernel library manager tests generate
+
+GENERATED_SOURCES = sdk/syscall_enum.h kernel/src/syscall_table.inl library/src/wrappers.c
 
 all: livecd.iso tests
 
 crt:
 	$(MAKE) -C crt all
 
-kernel: crt
+kernel: generate crt
 	$(MAKE) -C kernel all
 
-library: kernel
+library: generate crt
 	$(MAKE) -C library all
 
 manager: library
@@ -17,12 +19,20 @@ manager: library
 tests:
 	$(MAKE) -C tests all
 
+generate: $(GENERATED_SOURCES)
+
+sdk/syscall_enum.h: $(wildcard sdk/*.h) syscalls.awk
+	@echo 'Generating system call sources/headers...'
+	sort sdk/* | awk -v enum_file=sdk/syscall_enum.h -v table_file=kernel/src/syscall_table.inl -v wrappers_file=library/src/wrappers.c -f syscalls.awk
+
+kernel/src/syscall_table.inl: sdk/syscall_enum.h
+
+library/src/wrappers.c: sdk/syscall_enum.h
+
 livecd.iso: kernel library manager
-	mkdir -p livecd
-	mkdir -p livecd/boot
+	mkdir -p livecd/boot/grub
 	cp kernel/monolithium livecd/boot/monolithium
 	cp manager/manager livecd/manager
-	mkdir -p livecd/boot/grub
 	cp grub.cfg livecd/boot/grub/grub.cfg
 	grub-mkrescue -o $@ livecd
 
@@ -32,4 +42,4 @@ clean:
 	$(MAKE) -C library clean
 	$(MAKE) -C manager clean
 	$(MAKE) -C tests clean
-	rm livecd.iso
+	rm -f $(GENERATED_SOURCES) livecd.iso

+ 3 - 0
kernel/.gitignore

@@ -8,3 +8,6 @@
 !include/**
 !src
 !src/**
+
+# Exclude generated files:
+src/syscall_table.inl

+ 1 - 1
kernel/Makefile

@@ -66,7 +66,7 @@ all: $(OBJDIR) $(DEPDIR) monolithium
 clean:
 	find $(OBJDIR) -name \*.o -delete
 	find $(DEPDIR) -name \*.d -delete
-	rm monolithium
+	rm -f monolithium
 
 -include $(DEPENDS)
 

+ 2 - 71
kernel/src/syscalls.c

@@ -29,78 +29,9 @@
 #include <exception.h>
 #include <pipe.h>
 
-extern qword_t syscall_function(const void*, dword_t*, dword_t);
+extern sysret_t syscall_function(const void*, dword_t*, dword_t);
 
-const void *service_table[] =
-{
-    &syscall_alloc_memory,
-    &syscall_clock_get_time,
-    &syscall_clock_set_time,
-    &syscall_close_object,
-    &syscall_commit_memory,
-    &syscall_connect_pipeline,
-    &syscall_create_memory_section,
-    &syscall_create_pipeline,
-    &syscall_create_process,
-    &syscall_create_semaphore,
-    &syscall_create_thread,
-    &syscall_create_user,
-    &syscall_delete_file,
-    &syscall_delete_user,
-    &syscall_device_ioctl,
-    &syscall_duplicate_handle,
-    &syscall_enum_processes,
-    &syscall_flush_memory_section,
-    &syscall_free_memory,
-    &syscall_freeze_thread,
-    &syscall_get_exception_info,
-    &syscall_get_milliseconds,
-    &syscall_get_nanoseconds,
-    &syscall_list_directory,
-    &syscall_listen_pipeline,
-    &syscall_logon_user,
-    &syscall_map_memory_section,
-    &syscall_mount,
-    &syscall_open_file,
-    &syscall_open_memory_section,
-    &syscall_open_pipeline,
-    &syscall_open_process,
-    &syscall_open_semaphore,
-    &syscall_open_thread,
-    &syscall_power_control,
-    &syscall_query_file,
-    &syscall_query_handle,
-    &syscall_query_memory,
-    &syscall_query_process,
-    &syscall_query_thread,
-    &syscall_query_user,
-    &syscall_raise_exception,
-    &syscall_read_file,
-    &syscall_read_memory,
-    &syscall_read_pipe,
-    &syscall_release_semaphore,
-    &syscall_restore_exception_handler,
-    &syscall_revert_user,
-    &syscall_save_exception_handler,
-    &syscall_set_file,
-    &syscall_set_memory_flags,
-    &syscall_set_thread,
-    &syscall_set_user_id,
-    &syscall_sleep,
-    &syscall_terminate,
-    &syscall_terminate_thread,
-    &syscall_thaw_thread,
-    &syscall_uncommit_memory,
-    &syscall_unmount,
-    &syscall_wait_directory_event,
-    &syscall_wait_process,
-    &syscall_wait_semaphore,
-    &syscall_wait_thread,
-    &syscall_write_file,
-    &syscall_write_memory,
-    &syscall_write_pipe,
-    &syscall_yield_quantum,
-};
+#include "syscall_table.inl"
 
 static void system_service_handler(registers_t *regs, byte_t int_num)
 {

+ 2 - 1
library/.gitignore

@@ -7,4 +7,5 @@
 !src
 !src/*
 
-src/wrappers.c
+# Exclude generated files:
+src/wrappers.c

+ 0 - 7
library/Makefile

@@ -40,10 +40,7 @@ else
 endif
 
 # Input and output files
-WRAPPERS = $(SRCDIR)/wrappers.c
-
 SOURCES =  $(wildcard $(SRCDIR)/*.c)
-SOURCES += $(WRAPPERS)
 SOURCES += $(wildcard $(SRCDIR)/*.asm)
 
 DEPENDS = $(shell find $(DEPDIR) -type f -name \*.d)
@@ -61,9 +58,6 @@ $(OBJDIR):
 $(DEPDIR):
 	mkdir -p $(DEPDIR)
 
-$(WRAPPERS): $(wildcard ../sdk/*.h)
-	sort $^ | awk -f gen_wrappers.awk > $@
-
 $(OBJDIR)/%.o: $(SRCDIR)/%.c Makefile
 	mkdir -p $(dir $@)
 	mkdir -p $(dir $(@:$(OBJDIR)/%.o=$(DEPDIR)/%.d))
@@ -77,6 +71,5 @@ libmlsys.a: $(OBJECTS)
 
 clean:
 	rm -f libmlsys.a
-	rm -f $(SRCDIR)/wrappers.c
 	find $(OBJDIR) -name \*.o -delete
 	find $(DEPDIR) -name \*.d -delete

+ 0 - 46
library/gen_wrappers.awk

@@ -1,46 +0,0 @@
-BEGIN {
-    FS = ",[ ]*";
-
-    print "/*";
-    print " * wrappers.c (Automatically generated)";
-    print " *";
-    print " * Copyright (C) 2017 Aleksandar Andrejevic <theflash@sdf.lonestar.org>";
-    print " *";
-    print " * This program is free software: you can redistribute it and/or modify";
-    print " * it under the terms of the GNU Affero General Public License as";
-    print " * published by the Free Software Foundation, either version 3 of the";
-    print " * License, or (at your option) any later version.";
-    print " *";
-    print " * This program is distributed in the hope that it will be useful,";
-    print " * but WITHOUT ANY WARRANTY; without even the implied warranty of";
-    print " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the";
-    print " * GNU Affero General Public License for more details.";
-    print " *";
-    print " * You should have received a copy of the GNU Affero General Public License";
-    print " * along with this program.  If not, see <http://www.gnu.org/licenses/>.";
-    print " */";
-    print "";
-    print "#include <sdk/monolithium.h>";
-    print "";
-}
-
-/sysret_t syscall_[a-zA-Z0-9_]+\(.*\);/ {
-    print substr($0, 1, length - 1);
-    print "{";
-
-    match($0, /\(.*\)/);
-    printf "    return syscall(" toupper(substr($0, 10, RSTART - 10));
-
-    $0 = substr($0, RSTART + 1, RLENGTH - 2);
-
-    if ($0 != "void") {
-        for (i = 1; i <= NF; i++) {
-            match($i, /[a-zA-Z0-9_]*$/);
-            printf ", " substr($i, RSTART);
-        }
-    }
-
-    print ");";
-    print "}";
-    print "";
-}

+ 1 - 72
sdk/syscalls.h

@@ -25,78 +25,7 @@
 #define MAX_PARAMETERS 16
 #define SYSCALL_INTERRUPT 0xE5
 
-typedef enum
-{
-    SYSCALL_ALLOC_MEMORY,
-    SYSCALL_CLOCK_GET_TIME,
-    SYSCALL_CLOCK_SET_TIME,
-    SYSCALL_CLOSE_OBJECT,
-    SYSCALL_COMMIT_MEMORY,
-    SYSCALL_CONNECT_PIPELINE,
-    SYSCALL_CREATE_MEMORY_SECTION,
-    SYSCALL_CREATE_PIPELINE,
-    SYSCALL_CREATE_PROCESS,
-    SYSCALL_CREATE_SEMAPHORE,
-    SYSCALL_CREATE_THREAD,
-    SYSCALL_CREATE_USER,
-    SYSCALL_DELETE_FILE,
-    SYSCALL_DELETE_USER,
-    SYSCALL_DEVICE_IOCTL,
-    SYSCALL_DUPLICATE_HANDLE,
-    SYSCALL_ENUM_PROCESSES,
-    SYSCALL_FLUSH_MEMORY_SECTION,
-    SYSCALL_FREE_MEMORY,
-    SYSCALL_FREEZE_THREAD,
-    SYSCALL_GET_EXCEPTION_INFO,
-    SYSCALL_GET_MILLISECONDS,
-    SYSCALL_GET_NANOSECONDS,
-    SYSCALL_LIST_DIRECTORY,
-    SYSCALL_LISTEN_PIPELINE,
-    SYSCALL_LOGON_USER,
-    SYSCALL_MAP_MEMORY_SECTION,
-    SYSCALL_MOUNT,
-    SYSCALL_OPEN_FILE,
-    SYSCALL_OPEN_MEMORY_SECTION,
-    SYSCALL_OPEN_PIPELINE,
-    SYSCALL_OPEN_PROCESS,
-    SYSCALL_OPEN_SEMAPHORE,
-    SYSCALL_OPEN_THREAD,
-    SYSCALL_POWER_CONTROL,
-    SYSCALL_QUERY_FILE,
-    SYSCALL_QUERY_HANDLE,
-    SYSCALL_QUERY_MEMORY,
-    SYSCALL_QUERY_PROCESS,
-    SYSCALL_QUERY_THREAD,
-    SYSCALL_QUERY_USER,
-    SYSCALL_RAISE_EXCEPTION,
-    SYSCALL_READ_FILE,
-    SYSCALL_READ_MEMORY,
-    SYSCALL_READ_PIPE,
-    SYSCALL_RELEASE_SEMAPHORE,
-    SYSCALL_RESTORE_EXCEPTION_HANDLER,
-    SYSCALL_REVERT_USER,
-    SYSCALL_SAVE_EXCEPTION_HANDLER,
-    SYSCALL_SET_FILE,
-    SYSCALL_SET_MEMORY_FLAGS,
-    SYSCALL_SET_THREAD,
-    SYSCALL_SET_USER_ID,
-    SYSCALL_SLEEP,
-    SYSCALL_TERMINATE,
-    SYSCALL_TERMINATE_THREAD,
-    SYSCALL_THAW_THREAD,
-    SYSCALL_UNCOMMIT_MEMORY,
-    SYSCALL_UNMOUNT,
-    SYSCALL_WAIT_DIRECTORY_EVENT,
-    SYSCALL_WAIT_PROCESS,
-    SYSCALL_WAIT_SEMAPHORE,
-    SYSCALL_WAIT_THREAD,
-    SYSCALL_WRITE_FILE,
-    SYSCALL_WRITE_MEMORY,
-    SYSCALL_WRITE_PIPE,
-    SYSCALL_YIELD_QUANTUM,
-
-    SERVICE_COUNT
-} syscall_number_t;
+#include "syscall_enum.h"
 
 sysret_t syscall(syscall_number_t num, ...);
 

+ 59 - 0
syscalls.awk

@@ -0,0 +1,59 @@
+BEGIN {
+    FS = ",[ ]*";
+
+    if (!wrappers_file || !enum_file || !table_file)
+    {
+        print "Error: One or more filenames have not been specified.";
+        exit 1;
+    }
+
+    print "/* This file has been automatically generated */" > enum_file;
+    print "" >> enum_file;
+    print "typedef enum" >> enum_file;
+    print "{" >> enum_file;
+
+    print "/* This file has been automatically generated */" > table_file;
+    print "" >> table_file;
+    print "const void *service_table[] =" >> table_file;
+    print "{" >> table_file
+
+    print "/* This file has been automatically generated */" > wrappers_file;
+    print "" >> wrappers_file;
+    print "#include <sdk/monolithium.h>" >> wrappers_file;
+    print "" >> wrappers_file;
+}
+
+END {
+    if (enum_file && table_file && wrappers_file) {
+        print "" >> enum_file;
+        print "    SERVICE_COUNT" >> enum_file;
+        print "} syscall_number_t;" >> enum_file;
+
+        print "};" >> table_file;
+    }
+}
+
+/sysret_t syscall_[a-zA-Z0-9_]+\(.*\);/ {
+    print substr($0, 1, length - 1) >> wrappers_file;
+    print "{" >> wrappers_file;
+
+    match($0, /\(.*\)/);
+    name = substr($0, 10, RSTART - 10);
+
+    print "    &" name "," >> table_file;
+    print "    " toupper(name) "," >> enum_file;
+    printf "    return syscall(" toupper(name) >> wrappers_file;
+
+    $0 = substr($0, RSTART + 1, RLENGTH - 2);
+
+    if ($0 != "void") {
+        for (i = 1; i <= NF; i++) {
+            match($i, /[a-zA-Z0-9_]*$/);
+            printf ", " substr($i, RSTART) >> wrappers_file;
+        }
+    }
+
+    print ");" >> wrappers_file;
+    print "}" >> wrappers_file;
+    print "" >> wrappers_file;
+}