kvisc/vm/in/instrs.h

85 lines
3.5 KiB
C
Raw Normal View History

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/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-12 15:30:35 +02:00
#define GETV(v, p) \
ulong v; \
assert(p); \
if (ACC_FMT_IS_MEM(p->type)) \
v = readmem(ctx, p->addr, p->mlen); \
else v = p->val
2019-05-15 22:08:37 +02:00
#define IMPL_START_0(name) \
2019-06-12 15:47:11 +02:00
bool i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2, ulong *r1, ulong *r2) \
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) \
2019-06-12 15:47:11 +02:00
bool i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2, ulong *r1, ulong *r2) \
2019-05-15 21:47:08 +02:00
{ \
2019-06-12 15:30:35 +02:00
GETV(v1, p1);
2019-05-15 21:47:08 +02:00
#define IMPL_START_2(name) \
2019-06-12 15:47:11 +02:00
bool i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2, ulong *r1, ulong *r2) \
2019-05-16 16:48:45 +02:00
{ \
2019-06-12 15:30:35 +02:00
GETV(v1, p1); \
GETV(v2, p2);
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-06-12 15:47:11 +02:00
*r1 = v1; \
2019-06-07 22:23:38 +02:00
return 1; \
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 \
2019-06-12 15:47:11 +02:00
*r1 = v1; \
*r2 = v2; \
return 2; \
2019-06-05 22:59:32 +02:00
}
2019-05-15 21:47:08 +02:00
#define IMPL_END \
2019-06-07 22:23:38 +02:00
return 0; \
2019-05-15 21:47:08 +02:00
}
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