This commit is contained in:
julianb0 2019-05-30 12:44:56 +02:00
parent 1331ccdff5
commit 0641094a96
No known key found for this signature in database
GPG Key ID: DDF8325C95299A62
21 changed files with 86 additions and 87 deletions

View File

@ -1,4 +1,4 @@
; The OS/K Team licences this file to you under the MIT license.
; The OS/K Team licenses this file to you under the MIT license.
; See the LICENSE file in the project root for more information.
;

View File

@ -1,4 +1,4 @@
; The OS/K Team licences this file to you under the MIT license.
; The OS/K Team licenses this file to you under the MIT license.
; See the LICENSE file in the project root for more information.
;

View File

@ -1,4 +1,4 @@
; The OS/K Team licences this file to you under the MIT license.
; The OS/K Team licenses this file to you under the MIT license.
; See the LICENSE file in the project root for more information.
;

View File

@ -1,4 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The OS/K Team licenses this file to you under the MIT license.
See the LICENSE file in the project root for more information.
-->
<!DOCTYPE language SYSTEM "language.dtd">
<language _name="Assembler (K-Arch)" version="1.0" _section="Sources" globs="*.k" mimetypes="text/x-asm;text/x-assembler">
@ -13,7 +17,7 @@
<start-regex>/\*</start-regex>
<end-regex>\*/</end-regex>
</block-comment>
<pattern-item _name="Label" style="Preprocessor">
<regex>^[A-Za-z_][A-Za-z_0-9.]*\:</regex>
</pattern-item>
@ -26,25 +30,25 @@
<start-regex>&quot;</start-regex>
<end-regex>&quot;</end-regex>
</string>
<string _name="String 2" style = "String" end-at-line-end="TRUE">
<start-regex>&apos;</start-regex>
<end-regex>&apos;</end-regex>
</string>
<keyword-list _name="Assembler Cmds" style="def:preprocessor"
case-sensitive="TRUE"
match-empty-string-at-beginning = "TRUE"
match-empty-string-at-end = "TRUE">
<keyword>include</keyword>
</keyword-list>
<pattern-item _name="Instructions" style="def:keyword" case-sensitive="FALSE"
match-empty-string-at-beginning = "TRUE"
match-empty-string-at-end = "TRUE">
<regex>^\s+[a-zA-Z_]{2,}</regex>
</pattern-item>
<keyword-list _name="Registers K" style="def:type"
case-sensitive="FALSE"
match-empty-string-at-beginning = "TRUE"

View File

@ -11,11 +11,11 @@ static inline void _except(ctx_t *ctx, int x, char *fmt, ...)
va_list ap;
log("Disassembly error: ");
va_start(ap, fmt);
vlog(fmt, ap);
va_end(ap);
log("\n");
if (ctx->disf)
@ -39,14 +39,14 @@ static void scan_param(ctx_t *ctx, acc_t *p)
p->mlen = c & 0xF;
c = ctx->get(ctx);
}
else if (A_IS_OFF(c)) {
p->mem = 1;
p->mlen = c & 0xF;
p->off = (short)ctx->get(ctx);
c = ctx->get(ctx);
}
if (c >= A_IMM16) {
// Check for double access code
if (p->type != A_REG)
@ -57,26 +57,26 @@ static void scan_param(ctx_t *ctx, acc_t *p)
}
p->val = c;
if (p->type == A_IMM32) {
p->ilen = 4;
p->val = p->val | ((ulong)ctx->get(ctx) << 16);
}
else if (p->type == A_IMM64) {
p->ilen = 8;
p->val = p->val | ((ulong)ctx->get(ctx) << 16);
p->val = p->val | ((ulong)ctx->get(ctx) << 32);
p->val = p->val | ((ulong)ctx->get(ctx) << 48);
}
else if (p->type == A_REG) {
if (p->val > NREGS) {
_except(ctx, E_ILL, "Inexistent REG");
}
r = &ctx->r[p->val];
if (r->flags & (RES | CTL)) {
_except(ctx, E_ACC, "Reserved REG: %s", r->name);
}
@ -102,13 +102,13 @@ void disasm(ctx_t *ctx)
c = ctx->get(ctx);
}
}
else if (!ISINSTR(c)) {
_except(ctx, E_ILL, "Bad opcode: 0x%04hX", c);
}
i = &ctx->i[c];
// Scan for parameters
if (i->prm1 != NOPRM) {
scan_param(ctx, &p1);
@ -120,7 +120,7 @@ void disasm(ctx_t *ctx)
#ifdef _NEED_DISASM
dumpinstr(ctx, rip, rep, c, &p1, &p2);
#else
do_rep:
dumpinstr(ctx, rip, rep, c, &p1, &p2);
i->func(ctx, &p1, &p2);

View File

@ -31,16 +31,16 @@ void dumpfwstack(ctx_t *ctx)
void d_log(ctx_t *ctx, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (ctx->disf) {
vfprintf(ctx->disf, fmt, ap);
fflush(ctx->disf);
}
else
vlog(fmt, ap);
va_end(ap);
}
@ -53,19 +53,19 @@ void dumpinstr(ctx_t *ctx, ulong rip, uint rep,
d_log(ctx, "0x%08lX: %s%s", rip,
(rep ? "rep " : ""),
i->name);
if (i->prm1 != NOPRM)
p = p1;
lp:
if (p != 0) {
if (p->mem) d_log(ctx, " %c:[", getmempref(p->mlen));
else d_log(ctx, " ");
if (p->type == A_REG)
d_log(ctx, "%s", ctx->r[p->val].name);
else
d_log(ctx, "%c:0x%lX", getmempref(p->ilen), p->val);
if (p->mem) {
if (p->off)
d_log(ctx, "+%hd", p->off);

View File

@ -8,11 +8,11 @@ void _except(ctx_t *ctx, int code, char *fmt, ...)
va_list ap;
log("\nException %d - ", code);
va_start(ap, fmt);
vlog(fmt, ap);
va_end(ap);
log("\n");
dumpregs(ctx);

View File

@ -1,4 +1,4 @@
# The OS/K Team licences this file to you under the MIT license.
# The OS/K Team licenses this file to you under the MIT license.
# See the LICENSE file in the project root for more information.
stop

View File

@ -1,6 +1,6 @@
#!/usr/bin/python3
# The OS/K Team licences this file to you under the MIT license.
# The OS/K Team licenses this file to you under the MIT license.
# See the LICENSE file in the project root for more information.
fi = open("INSTRS")
@ -24,15 +24,15 @@ def getflag(s):
if s == "m":
return "P_MEM"
return "__ERROR__"
for _, line in enumerate(fi):
if line[0] == '#' or line[0] == ' ' or line[0] == '\n':
continue
tok = line.strip().split(' ')
if len(tok) == 1:
name = tok[0]
p1 = "NOPRM"
@ -42,12 +42,12 @@ for _, line in enumerate(fi):
name = "{}_{}".format(tok[0], tok[1].strip())
p1 = getflag(tok[1])
p2 = "NOPRM"
elif len(tok) == 3:
name = "{}_{}_{}".format(tok[0], tok[1], tok[2].strip())
p1 = getflag(tok[1])
p2 = getflag(tok[2])
else:
name = "__ERROR__"
p1 = "__ERROR__"

View File

@ -1,4 +1,4 @@
// The OS/K Team licences this file to you under the MIT license.
// 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 "instrs.h"
@ -31,16 +31,16 @@ IMPL_START_1(mul)
ulong t = (u1 * u2);
ulong w3 = (t & 0xffffffff);
ulong k = (t >> 32);
v1 >>= 32;
t = (v1 * u2) + k;
k = (t & 0xffffffff);
ulong w1 = (t >> 32);
v2 >>= 32;
t = (u1 * v2) + k;
k = (t >> 32);
ctx->r[RDX].val = (v1 * v2) + w1 + k;
ctx->r[RAX].val = (t << 32) + w3;
}

View File

@ -1,4 +1,4 @@
// The OS/K Team licences this file to you under the MIT license.
// 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 "instrs.h"
@ -35,7 +35,7 @@ IMPL_START_1(lea)
if (!p2->mem) {
_except(ctx, E_ILL, "Bad LEA format");
}
v1 = (p2->type == A_REG ? ctx->r[p2->val].val : p2->val) + p2->off;
}
IMPL_OUT;

View File

@ -1,4 +1,4 @@
// The OS/K Team licences this file to you under the MIT license.
// 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 "../arch.h"
@ -65,6 +65,3 @@ void i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2) \
#define JUMP(v) \
ctx->r[RIP].val = v + ctx->r[CR1].val

View File

@ -1,4 +1,4 @@
// The OS/K Team licences this file to you under the MIT license.
// 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 "instrs.h"

View File

@ -1,4 +1,4 @@
// The OS/K Team licences this file to you under the MIT license.
// 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 "instrs.h"
@ -11,7 +11,7 @@
IMPL_START_2(test)
{
ulong v = v1 & v2;
if (v == 0) ctx->r[FLG].val |= ZF;
else ctx->r[FLG].val &= ~ZF;
}
@ -23,7 +23,7 @@ IMPL_START_2(cmp)
ctx->r[FLG].val |= ZF;
ctx->r[FLG].val &= ~CF;
}
else if (v1 < v2) {
ctx->r[FLG].val &= ~ZF;
ctx->r[FLG].val |= CF;

View File

@ -1,4 +1,4 @@
// The OS/K Team licences this file to you under the MIT license.
// 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 "instrs.h"
@ -67,7 +67,7 @@ IMPL_END;
IMPL_START_0(popf)
{
CHK_STACK(<=);
// XXX
CHK_SUPERV();
@ -76,4 +76,3 @@ IMPL_START_0(popf)
}
IMPL_END;

View File

@ -1,4 +1,4 @@
// The OS/K Team licences this file to you under the MIT license.
// 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 "instrs.h"

View File

@ -6,7 +6,7 @@
void log(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);

View File

@ -1,4 +1,4 @@
// The OS/K Team licences this file to you under the MIT license.
// 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 "arch.h"
@ -20,9 +20,9 @@ ushort bget(ctx_t *ctx)
}
ushort c = ctx->mp[addr2real(ctx->r[RIP].val)];
ctx->r[RIP].val += 2;
return c;
}
@ -44,7 +44,7 @@ int main(int argc, char **argv)
{
ctx_t main_ctx;
FILE *fwfile;
main_ctx.r = arch_r;
main_ctx.i = arch_i;
@ -52,22 +52,22 @@ int main(int argc, char **argv)
log("Not enough arguments\n");
exit(-3);
}
fwprog = malloc(FWPROGSIZE);
fwfile = fopen(argv[1], "rb");
if (!fwprog) {
log("Couldn't allocate firmware buffer\n");
exit(-1);
}
if (!fwfile) {
log("Couldn't open program file\n");
exit(-2);
}
fwsize = fread(fwprog, 1, FWPROGSIZE, fwfile);
//log("Loaded %lu bytes
if (fwsize < 2) {
@ -78,19 +78,19 @@ int main(int argc, char **argv)
if (!(argc > 2 && !strcmp(argv[2], "-d"))) {
main_ctx.mp = malloc(MEMSIZE + 16);
main_ctx.mz = MEMSIZE;
main_ctx.get = bget;
main_ctx.disf = NULL;
main_ctx.r[RIP].val = MEMOFF;
if (main_ctx.mp == 0) {
log("Couldn't allocate RAM\n");
exit(-1);
}
memcpy(&main_ctx.mp[addr2real(main_ctx.r[RIP].val)], fwprog, fwsize);
memcpy(&main_ctx.mp[addr2real(main_ctx.r[RIP].val)], fwprog, fwsize);
while (1) {
decode(&main_ctx);
}
@ -99,7 +99,7 @@ int main(int argc, char **argv)
// Disassembly
else {
main_ctx.disf = fopen("fwprog.dis", "w");
if (main_ctx.disf == NULL) {
log("Couldn't open fwprog.dis\n");
exit(-2);
@ -109,7 +109,7 @@ int main(int argc, char **argv)
main_ctx.mz = 0;
main_ctx.get = dget;
while (1) {
disasm(&main_ctx);
}

View File

@ -47,7 +47,7 @@ ulong readmem8(ctx_t *ctx, ulong addr)
{
GETREAL();
CHK_RANGE();
if (addr % 2 == 0) return (ulong)ctx->mp[real] & 0xFF;
else return ((ulong)ctx->mp[real] & 0xFF00) >> 8;
}
@ -69,7 +69,7 @@ ulong readmem32(ctx_t *ctx, ulong addr)
ulong val = ctx->mp[real++];
val = val | ((ulong)ctx->mp[real] << 16);
return val;
}
@ -83,7 +83,7 @@ ulong readmem64(ctx_t *ctx, ulong addr)
val = val | ((ulong)ctx->mp[real++] << 16);
val = val | ((ulong)ctx->mp[real++] << 32);
val = val | ((ulong)ctx->mp[real] << 48);
return val;
}
@ -93,11 +93,11 @@ void writemem8(ctx_t *ctx, ulong val, ulong addr)
CHK_RANGE();
ushort v = ctx->mp[real];
if (addr % 2) {
ctx->mp[real] = (v & 0xFF00) | (val & 0xFF);
}
else {
ctx->mp[real] = (v & 0xFF) | (((val & 0xFF) << 8));
}

View File

@ -46,10 +46,10 @@ reg_t arch_r[NREGS] =
// cr0: various flags
{ "cr0", "Control register 0", "Control", 0, CTL },
// cr1: code offset
{ "cr1", "Control register 1", "Control", 0, CTL },
// cr2: data offset
{ "cr2", "Control register 2", "Control", 0, CTL },
@ -75,15 +75,15 @@ void dumpregs(ctx_t *ctx)
{
int i;
reg_t *r;
assert(ctx->r[INV].val == 0);
log("\nRegisters:");
for (i = RAX; i < CR4; i++) {
if ((i-1) % 4 == 0)
log("\n");
r = &ctx->r[i];
log("%s%s=0x%-16lX ", r->name,
(strlen(r->name) == 2 ? "=" : ""), r->val);

View File

@ -28,7 +28,7 @@ enum
NX5,
NX6,
NX7,
AX0,
AX1,
AX2,
@ -37,7 +37,7 @@ enum
AX5,
AX6,
AX7,
CR0,
CR1,
CR2,
@ -46,7 +46,7 @@ enum
CR5,
CR6,
CR7,
SA0,
SA1,
SA2,
@ -59,7 +59,7 @@ enum
NREGS
};
enum
enum
{
GPR = 1 << 0, // General
CTL = 1 << 1, // Control
@ -78,7 +78,7 @@ enum
OV = 1 << 4, // Overflow flag
DF = 1 << 5, // Direction flag
SF = 1 << 6, // Sign flag
UF = 1 << 16, // User-mode flag
IF = 1 << 17, // Interrupts enable flag
};
@ -92,4 +92,3 @@ struct reg_t
ulong flags;
};