From e92d2fb87b49fbdfcb9cd6fbf9d7da7c3a39b444 Mon Sep 17 00:00:00 2001 From: "Magomed Kostoev (mkostoevr)" Date: Sat, 27 Nov 2021 23:28:38 +0000 Subject: [PATCH] [KERNEL][UMKA] Make it compile and run on Windows git-svn-id: svn://kolibrios.org@9342 a494cfbc-eb01-0410-851d-a64ba20cac60 --- kernel/trunk/runtests.py | 42 +++++-- kernel/trunk/test/common/__init__.py | 3 +- kernel/trunk/umka/shell.c | 38 +------ kernel/trunk/umka/umka.asm | 164 +++++++++++++++++---------- kernel/trunk/umka/umka.h | 6 + kernel/trunk/umka/vdisk.c | 10 +- kernel/trunk/umka/win32/pci.c | 8 ++ kernel/trunk/umka/win32/pci.h | 8 ++ kernel/trunk/umka/win32/thread.c | 8 ++ 9 files changed, 179 insertions(+), 108 deletions(-) create mode 100644 kernel/trunk/umka/win32/pci.c create mode 100644 kernel/trunk/umka/win32/pci.h create mode 100644 kernel/trunk/umka/win32/thread.c diff --git a/kernel/trunk/runtests.py b/kernel/trunk/runtests.py index 86b4d1bd8e..b836e9b25e 100755 --- a/kernel/trunk/runtests.py +++ b/kernel/trunk/runtests.py @@ -172,7 +172,7 @@ def run_tests_serially(tests, root_dir): return thread def gcc(fin, fout): - flags = "-m32 -std=c11 -g -O0 -fno-pie -w" # -masm-intel + flags = "-m32 -std=c11 -g -O0 -fno-pie -w" defines = "-D_FILE_OFFSET_BITS=64 -DNDEBUG -D_POSIX_C_SOURCE=200809L" include = "-Iumka -Iumka/linux" command = f"clang {flags} {defines} {include} -c {fin} -o {fout}" @@ -185,8 +185,13 @@ def build_umka_asm(): files = "umka/umka.asm umka/build/umka.o -s umka/build/umka.fas" memory = "-m 2000000" command = f"{include} fasm {flags} {files} {memory}" - print(command) - os.system(command) + if sys.platform != "win32": + print(command) + os.system(command) + else: + my_env = os.environ.copy() + my_env["INCLUDE"] = "../../programs/develop/libraries/libcrash/hash" + print(subprocess.check_output(f"fasm {flags} {files} {memory} -dwin32=1", shell = True, env = my_env)) def build_umka(): if not enable_umka: @@ -194,12 +199,16 @@ def build_umka(): if os.path.exists("umka_shell.exe"): return os.makedirs("umka/build/linux", exist_ok = True) + os.makedirs("umka/build/win32", exist_ok = True) sources = [ "umka_shell.c", "shell.c", "vdisk.c", "lodepng.c", - "linux/pci.c", - "linux/thread.c" ] + "getopt.c" ] + if sys.platform == "win32": + sources += [ "win32/pci.c", "win32/thread.c" ] + else: + sources += [ "linux/pci.c", "linux/thread.c" ] sources = [f"umka/{f}" for f in sources] objects = [] for source in sources: @@ -210,9 +219,16 @@ def build_umka(): build_umka_asm() objects.append("umka/build/umka.o") objects = " ".join(objects) - command = f"gcc -m32 -no-pie -o umka_shell.exe -static -T umka/umka.ld {objects}" + if sys.platform != "win32": + ld_script = "-T umka/umka.ld" + else: + ld_script = "" + command = f"clang -m32 -fno-pie -o umka_shell.exe -static {ld_script} {objects}" print(command) os.system(command) + if not os.path.exists("umka_shell.exe"): + print("Could't compile umka_shell.exe") + exit() def create_relocated(root_dir, fname): with open(fname, "rb") as f: @@ -227,8 +243,18 @@ def create_relocated(root_dir, fname): def run_umka_test(root_dir, test_file_path): test = create_relocated(root_dir, test_file_path) ref_log = create_relocated(root_dir, f"{test_file_path[:-2]}.ref.log") - out_log = f"{test_file_path[:-2]}.out.log.o" - os.system(f"./umka_shell.exe < {test} > {out_log}") + out_log = f"{test_file_path[:-2]}.out.log" + if sys.platform != "win32": + prefix = "./" + else: + prefix = "" + os.system(f"{prefix}umka_shell.exe < {test} > {out_log}") + if sys.platform == "win32": + with open(out_log, "rb") as f: + contents = f.read() + contents_no_crlf = contents.replace(b"\r\n", b"\n") + with open(out_log, "wb") as f: + f.write(contents_no_crlf) if not filecmp.cmp(ref_log, out_log): print(f"FAILURE: {test_file_path}\n", end = "") else: diff --git a/kernel/trunk/test/common/__init__.py b/kernel/trunk/test/common/__init__.py index 004c86062c..1b2455e44d 100644 --- a/kernel/trunk/test/common/__init__.py +++ b/kernel/trunk/test/common/__init__.py @@ -89,7 +89,8 @@ def get_file_directory(path): def run_qemu(root_dir, test_dir, debug_log): # Make local copy of IMG, so we will be able to run the test in parallel - os.remove(f"{test_dir}/kolibri_test.img") # If previous test run interrupted the file may be busy + if os.path.exists(f"{test_dir}/kolibri_test.img"): # If previous test run interrupted the file may be busy + os.remove(f"{test_dir}/kolibri_test.img") shutil.copyfile(f"{root_dir}/kolibri_test.img", f"{test_dir}/kolibri_test.img") qemu_command = f"qemu-system-i386" flags = "" diff --git a/kernel/trunk/umka/shell.c b/kernel/trunk/umka/shell.c index 33388c0371..0ef6c93f25 100644 --- a/kernel/trunk/umka/shell.c +++ b/kernel/trunk/umka/shell.c @@ -307,7 +307,7 @@ shell_ramdisk_init(int argc, char **argv) { return; } const char *fname = argv[1]; - FILE *f = fopen(fname, "r"); + FILE *f = fopen(fname, "rb"); if (!f) { fprintf(fout, "[!] can't open file '%s': %s\n", fname, strerror(errno)); return; @@ -1093,7 +1093,7 @@ shell_put_image(int argc, char **argv) { fputs(usage, fout); return; } - FILE *f = fopen(argv[1], "r"); + FILE *f = fopen(argv[1], "rb"); fseek(f, 0, SEEK_END); size_t fsize = ftell(f); rewind(f); @@ -1126,7 +1126,7 @@ shell_put_image_palette(int argc, char **argv) { fputs(usage, fout); return; } - FILE *f = fopen(argv[1], "r"); + FILE *f = fopen(argv[1], "rb"); fseek(f, 0, SEEK_END); size_t fsize = ftell(f); rewind(f); @@ -1332,7 +1332,7 @@ shell_blit_bitmap(int argc, char **argv) { return; } const char *fname = argv[1]; - FILE *f = fopen(fname, "r"); + FILE *f = fopen(fname, "rb"); if (!f) { fprintf(fout, "[!] can't open file '%s': %s\n", fname, strerror(errno)); return; @@ -1763,7 +1763,7 @@ shell_acpi_preload_table(int argc, char **argv) { fputs(usage, fout); return; } - FILE *f = fopen(argv[1], "r"); + FILE *f = fopen(argv[1], "rb"); if (!f) { fprintf(fout, "[umka] can't open file: %s\n", argv[1]); return; @@ -1780,30 +1780,6 @@ shell_acpi_preload_table(int argc, char **argv) { kos_acpi_ssdt_cnt++; } -static void -shell_pci_set_path(int argc, char **argv) { - const char *usage = \ - "usage: pci_set_path \n" - " path where aaaa:bb:cc.d dirs are"; - if (argc != 2) { - fputs(usage, fout); - return; - } - strcpy(pci_path, argv[1]); -} - -static void -shell_pci_get_path(int argc, char **argv) { - (void)argv; - const char *usage = \ - "usage: pci_get_path"; - if (argc != 1) { - fputs(usage, fout); - return; - } - fprintf(fout, "pci path: %s\n", pci_path); -} - static void shell_bg_set_size(int argc, char **argv) { const char *usage = \ @@ -1869,7 +1845,7 @@ shell_bg_put_img(int argc, char **argv) { fputs(usage, fout); return; } - FILE *f = fopen(argv[1], "r"); + FILE *f = fopen(argv[1], "rb"); fseek(f, 0, SEEK_END); size_t fsize = ftell(f); rewind(f); @@ -1949,8 +1925,6 @@ func_table_t shell_cmds[] = { { "ls80", shell_ls80 }, { "move_window", shell_move_window }, { "mouse_move", shell_mouse_move }, - { "pci_get_path", shell_pci_get_path }, - { "pci_set_path", shell_pci_set_path }, { "process_info", shell_process_info }, { "put_image", shell_put_image }, { "put_image_palette", shell_put_image_palette }, diff --git a/kernel/trunk/umka/umka.asm b/kernel/trunk/umka/umka.asm index 76c054daac..ce3da048ab 100644 --- a/kernel/trunk/umka/umka.asm +++ b/kernel/trunk/umka/umka.asm @@ -1,6 +1,36 @@ ; TODO: SPDX -format ELF +if ~ defined win32 + format ELF +else + format MS COFF + + macro c_public name, _alias, _argsize { + if ~ _argsize eq nomangle + if _alias eqtype 'string' + if _argsize eqtype 1 + public name as '_' # _alias # '@' # `_argsize + else + public name as '_' # _alias + end if + else if _alias eqtype 1 + public name as '_' # `name # '@' # `_alias + else + public name as '_' # `name + end if + else + public name as _alias + end if + } + + macro extrn name, _argsize { + if _argsize eqtype 1 + extrn '_' # `name # '@' # `_argsize as name + else + extrn '_' # `name as name + end if + } +end if __DEBUG__ = 1 __DEBUG_LEVEL__ = 1 @@ -11,73 +41,73 @@ UMKA_OS = 3 UMKA_MEMORY_BYTES = 256 SHL 20 -public umka_sys_put_image_palette -public disk_add -public disk_del -public disk_list -public disk_media_changed +c_public umka_sys_put_image_palette +c_public disk_add, 16 +c_public disk_del, 4 +c_public disk_list +c_public disk_media_changed,8 -public xfs._.user_functions as 'xfs_user_functions' -public ext_user_functions -public fat_user_functions -public ntfs_user_functions +c_public xfs._.user_functions, 'xfs_user_functions' +c_public ext_user_functions +c_public fat_user_functions +c_public ntfs_user_functions -public i40 +c_public i40, 'i40', nomangle -public coverage_begin -public coverage_end +c_public coverage_begin +c_public coverage_end -public sha3_256_oneshot as 'hash_oneshot' -public kos_time_to_epoch -public umka_init +c_public sha3_256_oneshot, 'hash_oneshot' +c_public kos_time_to_epoch +c_public umka_init -public current_process as 'kos_current_process' -public current_slot as 'kos_current_slot' -public current_slot_idx as 'kos_current_slot_idx' +c_public current_process, 'kos_current_process' +c_public current_slot, 'kos_current_slot' +c_public current_slot_idx, 'kos_current_slot_idx' -public thread_count as 'kos_thread_count' -public TASK_TABLE as 'kos_task_table' -public TASK_BASE as 'kos_task_base' -public TASK_DATA as 'kos_task_data' -public SLOT_BASE as 'kos_slot_base' -public window_data as 'kos_window_data' +c_public thread_count, 'kos_thread_count' +c_public TASK_TABLE, 'kos_task_table' +c_public TASK_BASE, 'kos_task_base' +c_public TASK_DATA, 'kos_task_data' +c_public SLOT_BASE, 'kos_slot_base' +c_public window_data, 'kos_window_data' -public WIN_STACK as 'kos_win_stack' -public WIN_POS as 'kos_win_pos' -public lfb_base as 'kos_lfb_base' +c_public WIN_STACK, 'kos_win_stack' +c_public WIN_POS, 'kos_win_pos' +c_public lfb_base, 'kos_lfb_base' -public RAMDISK as 'kos_ramdisk' -public ramdisk_init as 'kos_ramdisk_init' +c_public RAMDISK, 'kos_ramdisk' +c_public ramdisk_init, 'kos_ramdisk_init' -public acpi_ssdt_cnt as 'kos_acpi_ssdt_cnt' -public acpi_ssdt_base as 'kos_acpi_ssdt_base' -public acpi_ssdt_size as 'kos_acpi_ssdt_size' +c_public acpi_ssdt_cnt, 'kos_acpi_ssdt_cnt' +c_public acpi_ssdt_base, 'kos_acpi_ssdt_base' +c_public acpi_ssdt_size, 'kos_acpi_ssdt_size' -public stack_init as 'kos_stack_init' -public net_add_device +c_public stack_init, 'kos_stack_init' +c_public net_add_device -public draw_data -public img_background -public mem_BACKGROUND -public sys_background -public REDRAW_BACKGROUND as 'kos_redraw_background' -public new_sys_threads as 'kos_new_sys_threads' -public osloop as 'kos_osloop' -public set_mouse_data as 'kos_set_mouse_data' -public scheduler_current as 'kos_scheduler_current' -public eth_input as 'kos_eth_input' -public net_buff_alloc as 'kos_net_buff_alloc' +c_public draw_data +c_public img_background +c_public mem_BACKGROUND +c_public sys_background +c_public REDRAW_BACKGROUND, 'kos_redraw_background' +c_public new_sys_threads, 'kos_new_sys_threads', nomangle +c_public osloop, 'kos_osloop' +c_public set_mouse_data, 'kos_set_mouse_data', 20 +c_public scheduler_current, 'kos_scheduler_current' +c_public eth_input, 'kos_eth_input' +c_public net_buff_alloc, 'kos_net_buff_alloc' -public mem_block_list +c_public mem_block_list -public acpi_dev_data as "kos_acpi_dev_data" -public acpi_dev_size as "kos_acpi_dev_size" -public kernel_alloc as "kos_kernel_alloc" +c_public acpi_dev_data, "kos_acpi_dev_data" +c_public acpi_dev_size, "kos_acpi_dev_size" +c_public kernel_alloc, "kos_kernel_alloc" -public window._.set_screen as 'kos_window_set_screen' -public _display as 'kos_display' +c_public window._.set_screen, 'kos_window_set_screen' +c_public _display, 'kos_display' -public BOOT as 'kos_boot' +c_public BOOT, 'kos_boot' macro cli { pushfd @@ -112,7 +142,7 @@ include 'macros.inc' macro diff16 msg,blah2,blah3 { if msg eq "end of .data segment" ; fasm doesn't align on 65536, but ld script does -section '.bss.aligned65k' writeable align 65536 +section '.bss.65k' writeable align 512 bss_base: end if } @@ -505,7 +535,7 @@ proc umka_init c uses ebx esi edi ebp ret endp -public skin_udata +c_public skin_udata proc idle uses ebx esi edi .loop: mov ecx, 10000000 @@ -517,7 +547,7 @@ proc idle uses ebx esi edi ret endp -extrn pci_read +extrn pci_read, 20 proc pci_read_reg uses ebx esi edi mov ecx, eax and ecx, 3 @@ -541,6 +571,7 @@ endp proc sys_msg_board cmp cl, 0x0d jz @f +if ~ defined win32 pushad mov eax, SYS_WRITE mov ebx, STDOUT @@ -550,6 +581,15 @@ proc sys_msg_board int 0x80 pop ecx popad +else + extrn putchar + + pushad + push ecx + call putchar + add esp, 4 + popad +end if @@: ret endp @@ -559,13 +599,13 @@ proc delay_ms ret endp -public umka_cli +c_public umka_cli proc umka_cli cli ; macro ret endp -public umka_sti +c_public umka_sti proc umka_sti sti ; macro ret @@ -593,7 +633,7 @@ endp extrn reset_procmask extrn get_fake_if -public irq0 +c_public irq0 proc irq0 c, _signo, _info, _context DEBUGF 2, "### irq0\n" pushfd @@ -735,10 +775,10 @@ restore sys_msg_board,delay_ms coverage_end: ; fasm doesn't align on 65536, but ld script does -section '.data.aligned65k' writeable align 65536 -public umka_tool +section '.data' readable writeable align 512 +c_public umka_tool umka_tool dd ? -public umka_initialized +c_public umka_initialized umka_initialized dd 0 fpu_owner dd ? diff --git a/kernel/trunk/umka/umka.h b/kernel/trunk/umka/umka.h index 81eb76b66b..99d26e09c8 100644 --- a/kernel/trunk/umka/umka.h +++ b/kernel/trunk/umka/umka.h @@ -9,6 +9,12 @@ #define BDFE_LEN_CP866 304 #define BDFE_LEN_UNICODE 560 +#if defined(WIN32) || defined(_WIN32) + typedef size_t off_t; + typedef long ssize_t; +# define PATH_MAX 256 +#endif + typedef struct { uint32_t left, top, right, bottom; } rect_t; diff --git a/kernel/trunk/umka/vdisk.c b/kernel/trunk/umka/vdisk.c index 1af23a768a..b9ee16a351 100644 --- a/kernel/trunk/umka/vdisk.c +++ b/kernel/trunk/umka/vdisk.c @@ -21,9 +21,9 @@ void *vdisk_init(const char *fname, int adjust_cache_size, size_t cache_size) { printf("vdisk: can't open file '%s': %s\n", fname, strerror(errno)); return NULL; } - fseeko(f, 0, SEEK_END); - off_t fsize = ftello(f); - fseeko(f, 0, SEEK_SET); + fseek(f, 0, SEEK_END); + off_t fsize = ftell(f); + fseek(f, 0, SEEK_SET); size_t sect_size = 512; if (strstr(fname, "s4096") != NULL || strstr(fname, "s4k") != NULL) { sect_size = 4096; @@ -51,7 +51,7 @@ vdisk_read(void *userdata, void *buffer, off_t startsector, size_t *numsectors) { COVERAGE_OFF(); vdisk_t *vdisk = userdata; - fseeko(vdisk->file, startsector * vdisk->sect_size, SEEK_SET); + fseek(vdisk->file, startsector * vdisk->sect_size, SEEK_SET); fread(buffer, *numsectors * vdisk->sect_size, 1, vdisk->file); COVERAGE_ON(); return ERROR_SUCCESS; @@ -62,7 +62,7 @@ vdisk_write(void *userdata, void *buffer, off_t startsector, size_t *numsectors) { COVERAGE_OFF(); vdisk_t *vdisk = userdata; - fseeko(vdisk->file, startsector * vdisk->sect_size, SEEK_SET); + fseek(vdisk->file, startsector * vdisk->sect_size, SEEK_SET); fwrite(buffer, *numsectors * vdisk->sect_size, 1, vdisk->file); COVERAGE_ON(); return ERROR_SUCCESS; diff --git a/kernel/trunk/umka/win32/pci.c b/kernel/trunk/umka/win32/pci.c new file mode 100644 index 0000000000..eea19aff20 --- /dev/null +++ b/kernel/trunk/umka/win32/pci.c @@ -0,0 +1,8 @@ +#include "pci.h" + +__attribute__((stdcall)) uint32_t pci_read(uint32_t bus, uint32_t dev, + uint32_t fun, uint32_t offset, + size_t len) { + printf("STUB: %s() -> 0", __func__); + return 0; +} diff --git a/kernel/trunk/umka/win32/pci.h b/kernel/trunk/umka/win32/pci.h new file mode 100644 index 0000000000..454a44c8ec --- /dev/null +++ b/kernel/trunk/umka/win32/pci.h @@ -0,0 +1,8 @@ +#ifndef PCI_H_INCLUDED +#define PCI_H_INCLUDED + +#include "umka.h" + +extern char pci_path[PATH_MAX]; + +#endif // PCI_H_INCLUDED diff --git a/kernel/trunk/umka/win32/thread.c b/kernel/trunk/umka/win32/thread.c new file mode 100644 index 0000000000..ef28c95407 --- /dev/null +++ b/kernel/trunk/umka/win32/thread.c @@ -0,0 +1,8 @@ +void reset_procmask(void) { + printf("STUB: %s()", __func__); +} + +int get_fake_if(void *ctx) { + printf("STUB: %s() -> 0", __func__); + return 0; +}