From 0d07b5810f9366b690e84e6ee72f3fd2bd8bb6da Mon Sep 17 00:00:00 2001 From: Ivan Baravy Date: Sat, 9 May 2020 05:15:57 +0300 Subject: [PATCH] Run net stack, jump to the osloop. Compiles, doesn't crash. --- linux/thread.c | 16 ++---- makefile | 15 +++--- system.inc | 2 + umka.asm | 140 +++++++++++++++++++++++++++++++++++++++++++++++-- umka.h | 88 ++++++++++++++++++++++++++++++- umka_os.c | 87 +++++------------------------- 6 files changed, 250 insertions(+), 98 deletions(-) diff --git a/linux/thread.c b/linux/thread.c index be15c49..19b6fff 100644 --- a/linux/thread.c +++ b/linux/thread.c @@ -8,26 +8,20 @@ sigjmp_buf trampoline; __attribute__((__stdcall__)) -uint32_t umka_new_sys_threads(void (*func)(void), void *stack, int type) { - (void)type; - (void)func; +uint32_t umka_sched_add_thread(appdata_t *app) { fprintf(stderr, "umka_new_sys_threads before\n"); -// kos_slot_base[kos_task_count].fpu_state = malloc(4096); fprintf(stderr, "kos_task_count: %d\n", kos_task_count); if (!sigsetjmp(trampoline, 1)) { __asm__ __inline__ __volatile__ ( - "mov esp, eax;" - "push ecx" + "mov esp, eax" : - : "a"(stack), - "c"(func) + : "a"(app->saved_esp) : "memory"); - if (!sigsetjmp(*kos_slot_base[kos_task_count++].fpu_state, 1)) { + if (!sigsetjmp(*app->fpu_state, 1)) { longjmp(trampoline, 1); } else { __asm__ __inline__ __volatile__ ( - "pop ecx;" - "call ecx" + "ret" : : : "memory"); diff --git a/makefile b/makefile index 81c5bc4..bf72212 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,7 @@ FASM=fasm CC=gcc WARNINGS=-Wall -Wextra -Wduplicated-cond -Wduplicated-branches -Wlogical-op -Wrestrict -Wnull-dereference -Wjump-misses-init -Wshadow -Wformat=2 -Wswitch -Wswitch-enum #-Wconversion -Wsign-conversion -CFLAGS=$(WARNINGS) -std=c99 -g -O0 -D_FILE_OFFSET_BITS=64 -Wno-address-of-packed-member -DNDEBUG -masm=intel -D_POSIX_C_SOURCE=200809L +CFLAGS=$(WARNINGS) -std=c99 -g -O0 -D_FILE_OFFSET_BITS=64 -Wno-address-of-packed-member -DNDEBUG -masm=intel -D_POSIX_C_SOURCE=200809L -Ilinux CFLAGS_32=$(CFLAGS) -m32 LDFLAGS= LDFLAGS_32=$(LDFLAGS) -m32 @@ -11,19 +11,22 @@ all: umka_shell umka_fuse umka_os umka.sym umka.prp umka.lst tags tools/mkdirran covpreproc: covpreproc.c $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ -umka_shell: umka_shell.o umka.o trace.o trace_lbr.o vdisk.o vnet.o lodepng.o pci.o +umka_shell: umka_shell.o umka.o trace.o trace_lbr.o vdisk.o vnet.o lodepng.o pci.o thread.o $(CC) $(LDFLAGS_32) $^ -o $@ -static -umka_fuse: umka_fuse.o umka.o trace.o trace_lbr.o vdisk.o pci.o +umka_fuse: umka_fuse.o umka.o trace.o trace_lbr.o vdisk.o pci.o thread.o $(CC) $(LDFLAGS_32) $^ -o $@ `pkg-config fuse3 --libs` -umka_os: umka_os.o umka.o trace.o trace_lbr.o vdisk.o pci.o - $(CC) $(LDFLAGS_32) $^ -o $@ +umka_os: umka_os.o umka.o trace.o trace_lbr.o vdisk.o pci.o thread.o + $(CC) $(LDFLAGS_32) $^ -o $@ -static umka.o umka.fas: umka.asm INCLUDE="$(KOLIBRI)/kernel/trunk;$(KOLIBRI)/programs/develop/libraries/libcrash/trunk" $(FASM) $< umka.o -s umka.fas -m 1234567 -pci.o: pci.c +thread.o: linux/thread.c + $(CC) $(CFLAGS_32) -c $< + +pci.o: linux/pci.c $(CC) $(CFLAGS_32) -c $< lodepng.o: lodepng.c lodepng.h diff --git a/system.inc b/system.inc index 4d39bd4..12a8248 100644 --- a/system.inc +++ b/system.inc @@ -18,3 +18,5 @@ STDOUT = 1 STDERR = 2 ENOENT = 2 + +SIGPROF = 27 diff --git a/umka.asm b/umka.asm index 44a2489..94e142e 100644 --- a/umka.asm +++ b/umka.asm @@ -22,6 +22,13 @@ public sha3_256_oneshot as 'hash_oneshot' public kos_time_to_epoch public kos_init +public CURRENT_TASK as 'kos_current_task' +public current_slot as 'kos_current_slot' +public TASK_COUNT as 'kos_task_count' +public task_base_addr as 'kos_task_base' +public task_data_addr as 'kos_task_data' +public slot_base_addr as 'kos_slot_base' + public win_stack_addr as 'kos_win_stack' public win_pos_addr as 'kos_win_pos' public lfb_base_addr as 'kos_lfb_base' @@ -118,9 +125,9 @@ include 'core/string.inc' include 'core/malloc.inc' include 'core/heap.inc' include 'core/dll.inc' -new_sys_threads equ __pew_pew +;new_sys_threads equ __pew_pew include 'core/taskman.inc' -restore new_sys_threads +;restore new_sys_threads include 'core/timers.inc' include 'core/clipboard.inc' include 'core/syscall.inc' @@ -174,6 +181,11 @@ proc kos_init c uses ebx esi edi ebp call init_kernel_heap call init_malloc + mov eax, sys_proc + list_init eax + add eax, PROC.thr_list + list_init eax + mov [BOOT.bpp], 32 mov [BOOT.x_res], 400 mov [BOOT.y_res], 300 @@ -258,6 +270,109 @@ proc kos_init c uses ebx esi edi ebp ret endp +public idle as 'umka_idle' +proc idle uses ebx esi edi +.loop: + mov ecx, 10000000 +@@: + loop @b + DEBUGF 1, "1 idle\n" + jmp .loop + + ret +endp + +extrn raise +public umka_os +proc umka_os uses ebx esi edi + call kos_init + +; mov [fdd_motor_status], 0 + + mov eax, -1 + mov edi, thr_slot_map+4 + mov [edi-4], dword 0xFFFFFFF8 + stosd + stosd + stosd + stosd + stosd + stosd + stosd + + mov [current_process], sys_proc + + mov dword[CURRENT_TASK], 0 + mov dword[TASK_COUNT], 0 + + stdcall kernel_alloc, RING0_STACK_SIZE + mov ebx, eax + mov edx, SLOT_BASE+256*1 + call setup_os_slot + mov dword [edx], 'IDLE' + sub [edx+APPDATA.saved_esp], 4 + mov eax, [edx+APPDATA.saved_esp] + mov dword[eax], idle ; _thread + mov ecx, IDLE_PRIORITY + call sched_add_thread + + stdcall kernel_alloc, RING0_STACK_SIZE + mov ebx, eax + mov edx, SLOT_BASE+256*2 + call setup_os_slot + mov dword [edx], 'OS' + sub [edx+APPDATA.saved_esp], 4 + mov eax, [edx+APPDATA.saved_esp] + mov dword[eax], 0 + xor ecx, ecx + call sched_add_thread + + mov dword[CURRENT_TASK], 2 + mov dword[TASK_COUNT], 3 + mov [current_slot], SLOT_BASE+256*2 + +; movi ebx, 1 +; mov ecx, eth_process_input +; call new_sys_threads + + call stack_init + + mov edx, SLOT_BASE+256*3 + xor ecx, ecx + call sched_add_thread + + mov edx, SLOT_BASE+256*4 + xor ecx, ecx + call sched_add_thread + + mov edx, SLOT_BASE+256*5 + xor ecx, ecx + call sched_add_thread + + mov dword[TASK_COUNT], 6 + + ccall raise, SIGPROF + + jmp osloop + +.loop: + mov ecx, 10000000 +@@: + loop @b + DEBUGF 1, "2 os\n" + jmp .loop + + ret +endp + +; in: edx -> APPDATA for OS/IDLE slot +; in: ebx = stack base +proc s2etup_os_slot + + + ret +endp + extrn pci_read proc pci_read_reg uses ebx esi edi mov ecx, eax @@ -300,8 +415,9 @@ proc map_io_mem _base, _size, _flags ret endp -new_sys_threads: - xor eax, eax +extrn umka_sched_add_thread +sched_add_thread: + stdcall umka_sched_add_thread, edx ret change_task: @@ -383,6 +499,7 @@ map_memEx: HEAP_BASE equ include 'init.inc' sys_msg_board equ __pew +;setup_os_slot equ ___pew include fix pew macro pew x {} @@ -402,12 +519,20 @@ macro lea r, v { macro add r, v { if v eq CURRENT_TASK - (SLOT_BASE shr 3) - int3 +; int3 + push r + mov r, SLOT_BASE + shr r, 3 + neg r + add r, CURRENT_TASK + add r, [esp] + add esp, 4 else add r, v end if } + include 'kernel.asm' purge lea,add,org @@ -421,6 +546,11 @@ section '.data' writeable align 64 timer_ticks dd 0 fpu_owner dd ? +task_base_addr dd TASK_BASE +task_data_addr dd TASK_DATA +slot_base_addr dd SLOT_BASE + + win_stack_addr dd WIN_STACK win_pos_addr dd WIN_POS lfb_base_addr dd lfb_base diff --git a/umka.h b/umka.h index 4916c10..6e9f58e 100644 --- a/umka.h +++ b/umka.h @@ -2,6 +2,7 @@ #define UMKA_H_INCLUDED #include +#include #include #include #include @@ -315,7 +316,8 @@ typedef struct { uint16_t ttl; } arp_entry_t; - +void umka_os(void); +//void umka_idle(void); void kos_init(void); void i40(void); uint32_t kos_time_to_epoch(uint32_t *time); @@ -375,6 +377,90 @@ static inline int32_t kos_net_add_device(net_device_t *dev) { void coverage_begin(void); void coverage_end(void); +typedef struct appobj_t appobj_t; + +struct appobj_t { + uint32_t magic; + void *destroy; // internal destructor + appobj_t *fd; // next object in list + appobj_t *bk; // prev object in list + uint32_t pid; // owner id +}; + +typedef struct { + uint32_t magic; + void *destroy; // internal destructor + appobj_t *fd; // next object in list + appobj_t *bk; // prev object in list + uint32_t pid; // owner id + uint32_t id; // event uid + uint32_t state; // internal flags + uint32_t code; + uint32_t pad[5]; +} event_t; + +typedef struct { + char app_name[11]; + uint8_t pad1[5]; + + lhead_t list; // +16 + uint32_t process; // +24 + sigjmp_buf *fpu_state; // +28 + void *exc_handler; // +32 + uint32_t except_mask; // +36 + void *pl0_stack; // +40 + void *cursor; // +44 + event_t *fd_ev; // +48 + event_t *bk_ev; // +52 + appobj_t *fd_obj; // +56 + appobj_t *bk_obj; // +60 + uint32_t saved_esp; // +64 + uint32_t io_map[2]; // +68 + uint32_t dbg_state; // +76 +/* + cur_dir dd ? ;+80 + wait_timeout dd ? ;+84 + saved_esp0 dd ? ;+88 + wait_begin dd ? ;+92 +++ + wait_test dd ? ;+96 +++ + wait_param dd ? ;+100 +++ + tls_base dd ? ;+104 + dd ? ;+108 + event_filter dd ? ;+112 + draw_bgr_x dd ? ;+116 + draw_bgr_y dd ? ;+120 + dd ? ;+124 + wnd_shape dd ? ;+128 + wnd_shape_scale dd ? ;+132 + dd ? ;+136 + dd ? ;+140 + saved_box BOX ;+144 + ipc_start dd ? ;+160 + ipc_size dd ? ;+164 + event_mask dd ? ;+168 + debugger_slot dd ? ;+172 + terminate_protection dd ? ;+176 + keyboard_mode db ? ;+180 + captionEncoding db ? + rb 2 + exec_params dd ? ;+184 + dbg_event_mem dd ? ;+188 + dbg_regs DBG_REGS ;+192 + wnd_caption dd ? ;+212 + wnd_clientbox BOX ;+216 + priority dd ? ;+232 + in_schedule LHEAD ;+236 +*/ + uint8_t pad[256-80]; +} appdata_t; + +extern uint32_t kos_current_task; +extern appdata_t *kos_current_slot; +extern size_t kos_task_count; +extern void *kos_task_base; +extern void *kos_task_data; +extern appdata_t *kos_slot_base; + extern uint32_t *kos_lfb_base; extern uint16_t *kos_win_stack; extern uint16_t *kos_win_pos; diff --git a/umka_os.c b/umka_os.c index 657f711..e54b605 100644 --- a/umka_os.c +++ b/umka_os.c @@ -3,87 +3,28 @@ #include #include #include "umka.h" +#include "thread.h" -#define THREAD_STACK_SIZE 0x2000 -#define MAX_THREADS 16 - -struct itimerval timeout = {.it_value = {.tv_sec = 0, .tv_usec = 999}}; -sigjmp_buf threads[MAX_THREADS], trampoline; -size_t cur_thread = 0; -size_t thread_cnt = 1; -uint8_t stack_os[THREAD_STACK_SIZE]; -uint8_t stack_thrda[THREAD_STACK_SIZE]; -uint8_t stack_thrdb[THREAD_STACK_SIZE]; - -static void add_new_thread(void (*func)(void), void *stack) { - if (!sigsetjmp(trampoline, 1)) { - __asm__ __inline__ __volatile__ ( - "mov esp, eax;" - "push ecx" - : - : "a"(stack), - "c"(func) - : "memory"); - if (!sigsetjmp(threads[thread_cnt++], 1)) { - longjmp(trampoline, 1); - } else { - __asm__ __inline__ __volatile__ ( - "pop ecx;" - "call ecx" - : - : - : "memory"); - } - } -} +struct itimerval timeout = {.it_value = {.tv_sec = 0, .tv_usec = 10000}}; void scheduler(int signo, siginfo_t *info, void *context) { (void)signo; (void)info; (void)context; - printf("##### cur_thread: %u\n", cur_thread); - if (!sigsetjmp(threads[cur_thread], 1)) { - printf("##### saved\n"); - cur_thread += 1; - if (cur_thread == thread_cnt) { - cur_thread = 0; +// printf("##### switching from task %u\n", kos_current_task); + if (!sigsetjmp(*kos_slot_base[kos_current_task].fpu_state, 1)) { +// printf("##### saved\n"); + kos_current_task += 1; + if (kos_current_task == kos_task_count) { + kos_current_task = 1; } + kos_current_slot = kos_slot_base + kos_current_task; + printf("##### kos_current_task: %u\n", kos_current_task); setitimer(ITIMER_PROF, &timeout, NULL); - siglongjmp(threads[cur_thread], 1); + siglongjmp(*kos_slot_base[kos_current_task].fpu_state, 1); } } -void os() { - printf("0 osidle begin\n"); - raise(SIGPROF); - while (1) { // osloop - for (int i = 0; i < 10000000; i++) ; - printf("0 osloop\n"); - fflush(stdout); - } - printf("0 osidle end\n"); -} - -void thrda() { - printf("1 thrd a begin\n"); - while (1) { - for (int i = 0; i < 10000000; i++) ; - printf("1 thrd a\n"); - fflush(stdout); - } - printf("1 thrd a end\n"); -} - -void thrdb() { - printf("2 thrd b begin\n"); - while (1) { - for (int i = 0; i < 10000000; i++) ; - printf("2 thrd b\n"); - fflush(stdout); - } - printf("2 thrd b end\n"); -} - int main() { struct sigaction sa; @@ -96,11 +37,7 @@ int main() { return 1; } -// add_new_thread(os, stack_os[0x1000]); - add_new_thread(thrda, stack_thrda+THREAD_STACK_SIZE); - add_new_thread(thrdb, stack_thrdb+THREAD_STACK_SIZE); - - os(); + umka_os(); return 0; }