2019-05-30 12:44:56 +02:00
|
|
|
// The OS/K Team licenses this file to you under the MIT license.
|
2019-05-15 21:56:42 +02:00
|
|
|
// See the LICENSE file in the project root for more information.
|
2019-05-15 21:47:08 +02:00
|
|
|
|
2019-06-05 12:53:09 +02:00
|
|
|
#include <pc/arch.h>
|
2019-06-06 22:07:34 +02:00
|
|
|
|
|
|
|
#include <in/cond.h>
|
|
|
|
#include <in/flags.h>
|
2019-06-05 12:53:09 +02:00
|
|
|
#include <in/arch_i.h>
|
2019-05-15 21:47:08 +02:00
|
|
|
|
2019-06-06 14:57:34 +02:00
|
|
|
#define DECV(p, v) \
|
|
|
|
ulong v = (p->type == A_REG ? R(p->val) : p->val); \
|
|
|
|
if (p->mem) { \
|
|
|
|
v = readmem(ctx, v + p->off + R(p->offreg), p1->mlen); \
|
|
|
|
}
|
|
|
|
|
2019-05-15 22:08:37 +02:00
|
|
|
#define IMPL_START_0(name) \
|
|
|
|
void i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2) \
|
2019-05-30 20:23:27 +02:00
|
|
|
{
|
2019-05-15 22:08:37 +02:00
|
|
|
|
2019-05-15 21:47:08 +02:00
|
|
|
#define IMPL_START_1(name) \
|
|
|
|
void i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2) \
|
|
|
|
{ \
|
2019-06-06 14:57:34 +02:00
|
|
|
DECV(p1, v1);
|
2019-05-15 21:47:08 +02:00
|
|
|
|
|
|
|
#define IMPL_START_2(name) \
|
|
|
|
void i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2) \
|
|
|
|
{ \
|
2019-06-06 14:57:34 +02:00
|
|
|
DECV(p1, v1); \
|
|
|
|
DECV(p2, v2);
|
2019-05-16 16:48:45 +02:00
|
|
|
|
|
|
|
#define IMPL_START_3(name) \
|
|
|
|
void i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2) \
|
|
|
|
{ \
|
2019-06-06 14:57:34 +02:00
|
|
|
DECV(p2, v2);
|
2019-05-16 16:48:45 +02:00
|
|
|
|
2019-06-06 22:07:34 +02:00
|
|
|
#define IMPL_OUT_ZSF \
|
|
|
|
SET_ZSF(v1); \
|
|
|
|
IMPL_OUT
|
|
|
|
|
2019-05-15 21:47:08 +02:00
|
|
|
#define IMPL_OUT \
|
2019-05-16 16:48:45 +02:00
|
|
|
assert(p1->type == A_REG || p1->mem); \
|
|
|
|
if (p1->mem) { \
|
2019-06-02 16:33:28 +02:00
|
|
|
ulong addr = p1->type == A_REG ? R(p1->val) : p1->val; \
|
2019-06-06 14:57:34 +02:00
|
|
|
writemem(ctx, v1, addr + p1->off + R(p1->offreg), p1->mlen); \
|
2019-05-16 16:48:45 +02:00
|
|
|
} \
|
2019-06-02 16:33:28 +02:00
|
|
|
else R(p1->val) = v1; \
|
2019-05-30 20:23:27 +02:00
|
|
|
}
|
2019-05-15 21:47:08 +02:00
|
|
|
|
2019-06-05 22:59:32 +02:00
|
|
|
#define IMPL_OUT_2 \
|
|
|
|
assert(p1->type == A_REG || p1->mem); \
|
|
|
|
if (p1->mem) { \
|
|
|
|
ulong addr = p1->type == A_REG ? R(p1->val) : p1->val; \
|
2019-06-06 14:57:34 +02:00
|
|
|
writemem(ctx, v1, addr + p1->off + R(p1->offreg), p1->mlen); \
|
2019-06-05 22:59:32 +02:00
|
|
|
} \
|
|
|
|
else R(p1->val) = v1; \
|
|
|
|
\
|
|
|
|
assert(p2->type == A_REG || p2->mem); \
|
|
|
|
if (p2->mem) { \
|
|
|
|
ulong addr = p2->type == A_REG ? R(p2->val) : p2->val; \
|
2019-06-06 14:57:34 +02:00
|
|
|
writemem(ctx, v2, addr + p2->off + R(p2->offreg), p2->mlen); \
|
2019-06-05 22:59:32 +02:00
|
|
|
} \
|
|
|
|
else R(p2->val) = v2; \
|
|
|
|
}
|
|
|
|
|
2019-05-15 21:47:08 +02:00
|
|
|
#define IMPL_END \
|
|
|
|
}
|
2019-05-16 16:48:45 +02:00
|
|
|
|
2019-05-30 20:23:27 +02:00
|
|
|
//
|
|
|
|
// Consistency checks
|
|
|
|
//
|
|
|
|
|
2019-05-16 16:48:45 +02:00
|
|
|
#define CHK_SUPERV() \
|
|
|
|
do { \
|
2019-06-02 16:33:28 +02:00
|
|
|
if ((cr0 & UF) == 1) { \
|
2019-05-16 16:48:45 +02:00
|
|
|
_except(ctx, E_SYS, "Supervisor-only INSTR"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2019-05-15 21:47:08 +02:00
|
|
|
|
2019-05-29 16:57:22 +02:00
|
|
|
#define CHK_STACK(op) \
|
2019-06-02 16:33:28 +02:00
|
|
|
if (rsp % 8 > 0 || rbp % 8 > 0) { \
|
2019-06-07 13:38:34 +02:00
|
|
|
_except(ctx, E_STA, "Misaligned stack REGS"); \
|
2019-05-29 16:57:22 +02:00
|
|
|
} \
|
2019-06-02 16:33:28 +02:00
|
|
|
if (rbp op rsp) { \
|
2019-06-07 13:38:34 +02:00
|
|
|
_except(ctx, E_STU, "Stack underflow"); \
|
2019-05-29 16:57:22 +02:00
|
|
|
}
|
|
|
|
|
2019-05-30 20:23:27 +02:00
|
|
|
//
|
|
|
|
// Common operations
|
|
|
|
//
|
|
|
|
|
2019-05-29 16:57:22 +02:00
|
|
|
#define PUSH(v) \
|
2019-06-07 13:38:34 +02:00
|
|
|
rsp -= 8; \
|
|
|
|
writemem64(ctx, v, rsp);
|
2019-05-29 16:57:22 +02:00
|
|
|
|
|
|
|
#define POP(v) \
|
2019-06-07 13:38:34 +02:00
|
|
|
v = readmem64(ctx, rsp); \
|
|
|
|
rsp += 8;
|
2019-05-29 16:57:22 +02:00
|
|
|
|
|
|
|
#define JUMP(v) \
|
2019-06-02 16:33:28 +02:00
|
|
|
rip = v + cr1
|
2019-05-29 16:57:22 +02:00
|
|
|
|