1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/vm/pc/main.c

143 lines
2.4 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 19:26:40 +02:00
2019-06-16 12:48:30 +02:00
#include <sys/time.h>
2019-06-08 09:47:19 +02:00
#include <signal.h>
2019-06-13 23:12:11 +02:00
#include <pc/dev.h>
2019-05-15 19:26:40 +02:00
2019-05-22 18:39:46 +02:00
#define FWPROGSIZE (1024 * 1024 * 1024)
static ssize_t fwsize;
static ushort *fwprog;
2019-05-15 19:26:40 +02:00
ushort bget(ctx_t *ctx)
{
2019-06-16 12:21:18 +02:00
ulong addr = rip + cr1;
2019-06-16 12:48:30 +02:00
/*
2019-06-16 12:21:18 +02:00
if (addr % 2) {
_except(ctx, E_ALI, "Misaligned RIP and/or CR1: "
"rip=0x%lX cr1=0x%lX",
rip, cr1);
2019-05-22 18:39:46 +02:00
}
2019-06-16 12:48:30 +02:00
*/
2019-06-16 12:21:18 +02:00
if (addr2real(addr) >= ctx->mz) {
_except(ctx, E_ACC, "Executing out of memory: "
"rip=0x%lX cr1=0x%lX",
rip, cr1);
2019-05-15 19:26:40 +02:00
}
2019-05-29 16:57:22 +02:00
2019-06-16 12:21:18 +02:00
ushort c = ctx->mp[addr2real(addr)];
2019-05-30 12:44:56 +02:00
2019-06-02 16:33:28 +02:00
rip += 2;
2019-05-30 12:44:56 +02:00
2019-05-16 10:03:01 +02:00
return c;
2019-05-15 19:26:40 +02:00
}
2019-06-05 19:31:48 +02:00
ctx_t main_ctx;
2019-06-16 12:48:30 +02:00
void sigcommon(void)
2019-06-05 22:59:32 +02:00
{
2019-06-16 12:48:30 +02:00
dying = 1;
enable_stdin_echoing();
2019-06-05 22:59:32 +02:00
//
// Shut down devices
//
devfiniall(&main_ctx);
2019-06-16 12:48:30 +02:00
exit(-13);
}
void sigint(int _)
{
sigcommon();
}
void sigsegv(int _)
{
log("Segmentation fault\n");
sigcommon();
2019-06-05 22:59:32 +02:00
}
2019-05-16 21:42:23 +02:00
int main(int argc, char **argv)
2019-05-15 19:26:40 +02:00
{
2019-05-22 18:39:46 +02:00
FILE *fwfile;
2019-05-30 12:44:56 +02:00
2019-06-05 22:59:32 +02:00
main_ctx.r = arch_r;
main_ctx.i = arch_i;
2019-06-05 19:31:48 +02:00
2019-06-05 22:59:32 +02:00
//
// srand
//
struct timeval time;
2019-06-05 19:31:48 +02:00
gettimeofday(&time, NULL);
srandom((time.tv_sec * 1000) + (time.tv_usec / 1000));
2019-06-05 22:59:32 +02:00
//
// Signal handling
//
2019-06-08 09:47:19 +02:00
signal(SIGINT, sigint);
2019-06-16 12:48:30 +02:00
signal(SIGSEGV, sigsegv);
2019-06-05 22:59:32 +02:00
//
// Load program
//
2019-05-22 18:39:46 +02:00
if (argc < 2) {
log("Not enough arguments\n");
exit(-3);
}
2019-05-30 12:44:56 +02:00
2019-05-22 18:39:46 +02:00
fwprog = malloc(FWPROGSIZE);
fwfile = fopen(argv[1], "rb");
2019-05-30 12:44:56 +02:00
2019-05-22 18:39:46 +02:00
if (!fwprog) {
log("Couldn't allocate firmware buffer\n");
exit(-1);
}
2019-05-30 12:44:56 +02:00
2019-05-22 18:39:46 +02:00
if (!fwfile) {
log("Couldn't open program file\n");
exit(-2);
}
2019-05-30 12:44:56 +02:00
2019-05-22 18:39:46 +02:00
fwsize = fread(fwprog, 1, FWPROGSIZE, fwfile);
2019-05-30 12:44:56 +02:00
2019-05-22 18:39:46 +02:00
if (fwsize < 2) {
2019-05-30 18:31:50 +02:00
log("Program file too small or empty\n");
2019-05-22 18:39:46 +02:00
exit(-3);
}
2019-06-07 22:23:38 +02:00
main_ctx.mp = malloc(MEMSIZE + 16);
main_ctx.mz = MEMSIZE;
2019-05-30 12:44:56 +02:00
2019-06-07 22:23:38 +02:00
main_ctx.get = bget;
2019-06-05 19:31:48 +02:00
2019-06-07 22:23:38 +02:00
main_ctx.r[RIP].val = MEMOFF;
2019-06-05 22:59:32 +02:00
2019-06-07 22:23:38 +02:00
if (main_ctx.mp == 0) {
log("Couldn't allocate RAM\n");
exit(-1);
2019-05-16 10:03:01 +02:00
}
2019-05-15 19:26:40 +02:00
2019-06-07 22:23:38 +02:00
memcpy(&main_ctx.mp[addr2real(main_ctx.r[RIP].val)], fwprog, fwsize);
2019-05-16 21:42:23 +02:00
2019-06-07 22:23:38 +02:00
main_ctx.dh = 0;
if (devinitall(&main_ctx) < 0) {
log("Couldn't initialize devices\n");
exit(-10);
2019-05-15 19:26:40 +02:00
}
2019-06-16 19:42:10 +02:00
disable_stdin_echoing();
2019-06-07 22:23:38 +02:00
while (1) {
decode(&main_ctx);
2019-05-16 21:42:23 +02:00
2019-06-07 22:23:38 +02:00
if (main_ctx.step)
getchar();
}
2019-05-15 19:26:40 +02:00
}