From 4bccea4a9d8db3f063e9d370ea938a0531c3afa2 Mon Sep 17 00:00:00 2001 From: julianb0 Date: Fri, 21 Jun 2019 22:19:55 +0200 Subject: [PATCH] lots of stuff! --- Makefile | 3 ++- ka/ABI | 8 ++++---- ka/dos.k | 13 +++++++++++-- ka/sys/main.k | 10 +++++++++- ka/sys/tests.k | 28 ++++++++++++---------------- vm/Makefile | 7 ++++--- vm/dv/clockdev.c | 2 +- vm/dv/cpudev.c | 2 +- vm/dv/keybdev.c | 2 +- vm/dv/memdev.c | 2 +- vm/in/INSTRS | 10 ++++++++++ vm/in/devctl.c | 2 +- vm/in/instrs.c | 7 +++++++ vm/in/trap.c | 2 ++ vm/pc/arch.h | 11 +++++++++-- vm/pc/console.c | 2 ++ vm/pc/{decd.c => decode.c} | 4 ---- vm/pc/{decd.h => decode.h} | 0 vm/pc/{dev.c => device.c} | 2 +- vm/pc/{dev.h => device.h} | 0 vm/pc/die.c | 2 +- vm/pc/dump.c | 4 ++-- vm/pc/except.c | 5 +++-- vm/pc/except.h | 1 + vm/pc/exec.c | 12 ++++++++++-- vm/pc/log.c | 2 ++ vm/pc/main.c | 6 +++++- vm/pc/regs.c | 6 ++---- 28 files changed, 104 insertions(+), 51 deletions(-) rename vm/pc/{decd.c => decode.c} (99%) rename vm/pc/{decd.h => decode.h} (100%) rename vm/pc/{dev.c => device.c} (99%) rename vm/pc/{dev.h => device.h} (100%) diff --git a/Makefile b/Makefile index b2bd77f..c13892e 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ KODIR=ka/obj all: kas -kpc: +kpc: vm/Makefile @cd vm && make --no-print-directory -s verbose=yes kas: kpc as/regs.lst as/k-as.py @@ -24,6 +24,7 @@ clean: test: $(KODIR)/a.out @vm/k.exe $(KODIR)/a.out $(KODIR)/a.sym @rm -f $(KODIR)/a.out $(KODIR)/a.sym + wc: clean @cat $(shell find -name *.[kch]) $(shell find -name *.py) | wc -l diff --git a/ka/ABI b/ka/ABI index 52c90bf..c488855 100644 --- a/ka/ABI +++ b/ka/ABI @@ -105,14 +105,14 @@ To call a variadic function, do this: add rsp, nargs * 8 To the variadic function, argN can be accessed the following way: - mov reg, [rbp + N*8 + 16] + mov reg, [rbp+N*8+16] For instance: - mov rax, [rbp + 16] ; arg0 - mov rdx, [rbp + 24] ; arg1 + mov rax, [rbp+16] ; arg0 + mov rdx, [rbp+24] ; arg1 It is recommended to use the reg+reg*imm16+imm16 memory format: - mov rax, [rbp + rcx * 8 + 16] ; accesses arg#rcx + mov rax, [rbp+rcx*8+16] ; accesses arg#rcx The 'va_list' type can be regarded as a pointer to the variadic function's rbp+16 diff --git a/ka/dos.k b/ka/dos.k index cd570a6..2973b85 100644 --- a/ka/dos.k +++ b/ka/dos.k @@ -4,16 +4,25 @@ ; ; Entry point ; -_start: +start: mov rsp, 0x200000 xor rbp, rbp call main + ; Wait for and print input indefinitely .1: - hlt + scan rax + xpause + + test rax, rax + j.z .1 + + prn rax jmp .1 + + include "inc/limits.k" include "crt/crt.k" diff --git a/ka/sys/main.k b/ka/sys/main.k index 9fcd66d..7b56274 100644 --- a/ka/sys/main.k +++ b/ka/sys/main.k @@ -1,10 +1,18 @@ ; The OS/K Team licenses this file to you under the MIT license. ; See the LICENSE file in the project root for more information. +PrintBootMsg: + mov ax0, .bootmsg + call print + ret + +.bootmsg = "Starting DOS...\n\n" + ; ; Main function ; main: - call keybd_test + call PrintBootMsg + ret diff --git a/ka/sys/tests.k b/ka/sys/tests.k index a191d6c..06bcbcd 100644 --- a/ka/sys/tests.k +++ b/ka/sys/tests.k @@ -1,18 +1,18 @@ ; The OS/K Team licenses this file to you under the MIT license. ; See the LICENSE file in the project root for more information. -keybd_test: -.1: - scan rax - xpause +speed_test: + utime lx0 - test rax, rax - j.z .1 + mov rcx, ax0 + nop.rep - prn rax - jmp .1 - - ret + utime lx1 + + mov rax, lx1 + sub rax, lx0 + + stop putc_scroll_test: mov rdx, 25 @@ -55,18 +55,14 @@ cpudev_test: mov ax1, 1 call IDT.AddHandler - trap 0 - trap 0 - trap 1 - ret trap0_test: mov rsp, 0x300000 mov nx7, rax - mov ax0, .msg - call print + ;mov ax0, .msg + ;call print mov ax0, nx7 call IDT.DoneHandling diff --git a/vm/Makefile b/vm/Makefile index 59974dd..062a929 100644 --- a/vm/Makefile +++ b/vm/Makefile @@ -5,6 +5,7 @@ verbose ?= yes OBJDIR = ob + FLAGS=-O2 -g -Wall -fno-builtin-log -I. dv_src = $(shell ls dv/*.c) @@ -39,9 +40,9 @@ $(OBJDIR)/%.o: %.c $(OBJDIR)/%.d: %.c @mkdir -p $(shell dirname $@) @cc -I. -MM -MT $(@:%.d=%.o) -MF $@ $< - #@if [ $(verbose) = "yes" ]; then \ - # echo ${CL2}[$@] ${CL}dependencies generated.${CL3};\ - #fi +# @if [ $(verbose) = "yes" ]; then \ +# echo ${CL2}[$@] ${CL}dependencies generated.${CL3};\ +# fi in/instrs.lst: in/INSTRS in/arch_i.py @cd in && python3 arch_i.py diff --git a/vm/dv/clockdev.c b/vm/dv/clockdev.c index 6c3d7af..af1449f 100644 --- a/vm/dv/clockdev.c +++ b/vm/dv/clockdev.c @@ -1,7 +1,7 @@ // The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#include +#include //----------------------------------------------------------------------------// diff --git a/vm/dv/cpudev.c b/vm/dv/cpudev.c index a80516a..68cf81e 100644 --- a/vm/dv/cpudev.c +++ b/vm/dv/cpudev.c @@ -1,7 +1,7 @@ // The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#include +#include //----------------------------------------------------------------------------// diff --git a/vm/dv/keybdev.c b/vm/dv/keybdev.c index 8ccde02..497d122 100644 --- a/vm/dv/keybdev.c +++ b/vm/dv/keybdev.c @@ -1,7 +1,7 @@ // The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#include +#include //----------------------------------------------------------------------------// diff --git a/vm/dv/memdev.c b/vm/dv/memdev.c index 357af9e..6b5d510 100644 --- a/vm/dv/memdev.c +++ b/vm/dv/memdev.c @@ -1,7 +1,7 @@ // The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#include +#include long memdev_getmemoff(ctx_t *ctx, dev_t *dev) { diff --git a/vm/in/INSTRS b/vm/in/INSTRS index 9851648..8b0aa8b 100644 --- a/vm/in/INSTRS +++ b/vm/in/INSTRS @@ -464,6 +464,16 @@ nop # pause +# +# Get timestamp in seconds +# +time rm + +# +# Get timestamp in µseconds +# +utime rm + # # CPU Identification Number # diff --git a/vm/in/devctl.c b/vm/in/devctl.c index d339823..f2cf3d2 100644 --- a/vm/in/devctl.c +++ b/vm/in/devctl.c @@ -1,7 +1,7 @@ // The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#include +#include #include // diff --git a/vm/in/instrs.c b/vm/in/instrs.c index 30532ef..e835c16 100644 --- a/vm/in/instrs.c +++ b/vm/in/instrs.c @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -57,6 +58,12 @@ IMPL_OUT; //----------------------------------------------------------------------------// IMPL_START_1(time) +{ + v1 = time(NULL); +} +IMPL_OUT; + +IMPL_START_1(utime) { struct timeval time; gettimeofday(&time, NULL); diff --git a/vm/in/trap.c b/vm/in/trap.c index 08de390..b85a0df 100644 --- a/vm/in/trap.c +++ b/vm/in/trap.c @@ -3,6 +3,8 @@ #include +static volatile long *prev_stk; + IMPL_START_1(trap) { if (v1 > 255) diff --git a/vm/pc/arch.h b/vm/pc/arch.h index 40f89ef..e476f93 100644 --- a/vm/pc/arch.h +++ b/vm/pc/arch.h @@ -4,12 +4,15 @@ #ifndef _ARCH_H #define _ARCH_H +//#define NDEBUG 1 + #define dev_t stddev_t #include #include #include #include +#include #include #include #include @@ -32,9 +35,11 @@ typedef struct arch_t arch_t; typedef struct dev_t dev_t; void logerr(const char *, ...); -void trace(const char *, ...); void vlog(const char *, va_list); +// void trace(const char *, ...); +#define trace printf + #define KARCH_MAJOR 0 #define KARCH_MINOR 1 #define KARCH_REVIS 0 @@ -83,7 +88,7 @@ void disable_stdin_echoing(void); #include #include #include -#include +#include #include #include @@ -91,5 +96,7 @@ extern ctx_t main_ctx; extern reg_t arch_r[NREGS]; extern instr_t arch_i[NINSTRS]; +extern size_t rfs_current_idx; + #endif diff --git a/vm/pc/console.c b/vm/pc/console.c index 826785b..2ae40db 100644 --- a/vm/pc/console.c +++ b/vm/pc/console.c @@ -262,6 +262,8 @@ void console_putc(ctx_t *ctx, char ch) { scr_rects[y]->y -= 16; // surf->h } + + console_render(ctx); } } diff --git a/vm/pc/decd.c b/vm/pc/decode.c similarity index 99% rename from vm/pc/decd.c rename to vm/pc/decode.c index 460689c..0f6bfee 100644 --- a/vm/pc/decd.c +++ b/vm/pc/decode.c @@ -167,10 +167,6 @@ void decode(ctx_t *ctx) if (sym) trace("0x%lX: %s:\n", pc, sym->name); */ - - // Instruction counter - ctx->ninstrs++; - // // Process the first word of the instruction // diff --git a/vm/pc/decd.h b/vm/pc/decode.h similarity index 100% rename from vm/pc/decd.h rename to vm/pc/decode.h diff --git a/vm/pc/dev.c b/vm/pc/device.c similarity index 99% rename from vm/pc/dev.c rename to vm/pc/device.c index 405288b..6feb993 100644 --- a/vm/pc/dev.c +++ b/vm/pc/device.c @@ -1,7 +1,7 @@ // The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#include +#include //----------------------------------------------------------------------------// diff --git a/vm/pc/dev.h b/vm/pc/device.h similarity index 100% rename from vm/pc/dev.h rename to vm/pc/device.h diff --git a/vm/pc/die.c b/vm/pc/die.c index fa04c7a..59328be 100644 --- a/vm/pc/die.c +++ b/vm/pc/die.c @@ -1,7 +1,7 @@ // The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#include +#include #include void die(int code) diff --git a/vm/pc/dump.c b/vm/pc/dump.c index dac58ac..7357321 100644 --- a/vm/pc/dump.c +++ b/vm/pc/dump.c @@ -17,7 +17,7 @@ char *cond_suffixes[] = #define _ATT_STYLE 0 #endif -static void dump_acc(ctx_t *ctx, acc_t *p); +void dump_acc(ctx_t *ctx, acc_t *p); void dump_instr(ctx_t *ctx, instr_t *in, @@ -28,7 +28,7 @@ void dump_instr(ctx_t *ctx, uint cond, ulong pc) { - trace("0x%lX:\t", pc); + trace("%03lu 0x%lX:\t", ctx->ninstrs, pc); if (lock) trace("lock"); diff --git a/vm/pc/except.c b/vm/pc/except.c index 202c87b..3f50014 100644 --- a/vm/pc/except.c +++ b/vm/pc/except.c @@ -1,7 +1,7 @@ // The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#include +#include int dying = 0; @@ -10,6 +10,7 @@ void _except(ctx_t *ctx, int _code, char *fmt, ...) va_list ap; ulong handler; uint code = _code; + volatile char ch; ulong orig_frame; @@ -87,7 +88,7 @@ void _except(ctx_t *ctx, int _code, char *fmt, ...) idt_handling[code]++; - main_loop(); + longjmp(exc_jmp_buf, code); } actually_die: diff --git a/vm/pc/except.h b/vm/pc/except.h index 1faadd2..eb21e21 100644 --- a/vm/pc/except.h +++ b/vm/pc/except.h @@ -25,6 +25,7 @@ enum }; extern int dying; +extern jmp_buf exc_jmp_buf; void main_loop(void) __attribute__((__noreturn__)); void _except(ctx_t *, int, char *, ...) __attribute__((__noreturn__)); diff --git a/vm/pc/exec.c b/vm/pc/exec.c index 7986f26..3c0f41d 100644 --- a/vm/pc/exec.c +++ b/vm/pc/exec.c @@ -3,7 +3,7 @@ #include -static bool eval_cond(ctx_t *ctx, uint cond) +bool eval_cond(ctx_t *ctx, uint cond) { bool neg = cond & (1 << 4); bool ok; @@ -56,8 +56,12 @@ void exec_instr(ctx_t *ctx, bool out; ulong r1 = 0, r2 = 0; - // Debugging + // Instruction counter + ctx->ninstrs++; + +#ifndef NDEBUG dump_instr(ctx, in, p1, p2, lock, rep, cond, pc); +#endif // // For REPs we evaluate the condition AFTER running the instruction, @@ -112,8 +116,12 @@ do_rep: if (rcx == 0) return; + ctx->ninstrs++; + +#ifndef NDEBUG // Show that we're REP'ing dump_instr(ctx, in, p1, p2, lock, rep, cond, pc); +#endif goto do_rep; } diff --git a/vm/pc/log.c b/vm/pc/log.c index 5daa57c..fdd28bb 100644 --- a/vm/pc/log.c +++ b/vm/pc/log.c @@ -3,6 +3,7 @@ #include +/* void trace(const char *fmt, ...) { va_list ap; @@ -11,6 +12,7 @@ void trace(const char *fmt, ...) vfprintf(stderr, fmt, ap); va_end(ap); } +*/ void logerr(const char *fmt, ...) { diff --git a/vm/pc/main.c b/vm/pc/main.c index f45a906..fd1cad2 100644 --- a/vm/pc/main.c +++ b/vm/pc/main.c @@ -4,7 +4,7 @@ #include #include -#include +#include #include #define FWPROGSIZE (1024 * 1024 * 1024) @@ -48,11 +48,15 @@ void sigsegv(int _) sigcommon(); } +jmp_buf exc_jmp_buf; + // // Main loop // void main_loop(void) { + setjmp(exc_jmp_buf); + // // Start decoding // diff --git a/vm/pc/regs.c b/vm/pc/regs.c index e174dc4..826a193 100644 --- a/vm/pc/regs.c +++ b/vm/pc/regs.c @@ -1,7 +1,7 @@ // The OS/K Team licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#include +#include reg_t arch_r[] = { @@ -79,14 +79,12 @@ reg_t arch_r[] = trace("%s=0x%-16lX ", r->name, R(i)); \ } \ -extern size_t rfs_current_idx; - void dumpregs(ctx_t *ctx) { int i; reg_t *r; - trace("Current RFRAME index: #%u\n", rfs_current_idx); + trace("Current RFRAME index: #%lu\n", rfs_current_idx); DUMPREGS(RAX, RDI); DUMPREGS(AX0, AX3);