ovni/emu_nosv.c

292 lines
5.3 KiB
C
Raw Normal View History

2021-08-02 10:08:58 +02:00
#include <assert.h>
#include "uthash.h"
2021-07-28 11:56:35 +02:00
#include "ovni.h"
#include "ovni_trace.h"
#include "emu.h"
2021-08-02 10:08:58 +02:00
#include "prv.h"
2021-07-28 11:56:35 +02:00
2021-07-30 21:37:25 +02:00
enum nosv_prv_type {
PRV_TYPE_PROCID
};
2021-07-29 17:46:25 +02:00
struct hook_entry {
char id[4];
void (*hook)(struct ovni_emu *);
};
2021-07-29 18:13:41 +02:00
/* --------------------------- pre ------------------------------- */
2021-07-29 17:46:25 +02:00
static void
pre_task_create(struct ovni_emu *emu)
{
struct nosv_task *task, *p = NULL;
2021-07-29 18:13:41 +02:00
task = calloc(1, sizeof(*task));
2021-07-29 17:46:25 +02:00
if(task == NULL)
{
2021-07-29 18:13:41 +02:00
perror("calloc");
2021-07-29 17:46:25 +02:00
abort();
}
task->id = emu->cur_ev->payload.i32[0];
2021-07-29 18:13:41 +02:00
task->type_id = emu->cur_ev->payload.i32[1];
2021-07-29 17:46:25 +02:00
task->state = TASK_ST_CREATED;
/* Ensure the task id is new */
2021-07-30 21:37:25 +02:00
HASH_FIND_INT(emu->cur_proc->tasks, &task->id, p);
2021-07-29 17:46:25 +02:00
if(p != NULL)
{
2021-07-29 18:13:41 +02:00
err("A task with id %d already exists\n", p->id);
2021-07-29 17:46:25 +02:00
abort();
}
/* Add the new task to the hash table */
2021-07-30 21:37:25 +02:00
HASH_ADD_INT(emu->cur_proc->tasks, id, task);
2021-07-29 17:46:25 +02:00
emu->cur_task = task;
dbg("new task created id=%d\n", task->id);
}
static void
pre_task_execute(struct ovni_emu *emu)
{
struct nosv_task *task;
int taskid;
taskid = emu->cur_ev->payload.i32[0];
2021-07-30 21:37:25 +02:00
HASH_FIND_INT(emu->cur_proc->tasks, &taskid, task);
2021-07-29 17:46:25 +02:00
assert(task != NULL);
assert(emu->cur_thread->state == TH_ST_RUNNING);
assert(emu->cur_thread->task == NULL);
task->state = TASK_ST_RUNNING;
task->thread = emu->cur_thread;
emu->cur_thread->task = task;
emu->cur_task = task;
dbg("task id=%d runs now\n", task->id);
}
static void
pre_task_pause(struct ovni_emu *emu)
{
struct nosv_task *task;
int taskid;
taskid = emu->cur_ev->payload.i32[0];
2021-07-30 21:37:25 +02:00
HASH_FIND_INT(emu->cur_proc->tasks, &taskid, task);
2021-07-29 17:46:25 +02:00
assert(task != NULL);
assert(task->state == TASK_ST_RUNNING);
assert(emu->cur_thread->state == TH_ST_RUNNING);
assert(emu->cur_thread->task == task);
assert(emu->cur_thread == task->thread);
task->state = TASK_ST_PAUSED;
emu->cur_task = task;
dbg("task id=%d pauses\n", task->id);
}
static void
pre_task_resume(struct ovni_emu *emu)
{
struct nosv_task *task;
int taskid;
taskid = emu->cur_ev->payload.i32[0];
2021-07-30 21:37:25 +02:00
HASH_FIND_INT(emu->cur_proc->tasks, &taskid, task);
2021-07-29 17:46:25 +02:00
assert(task != NULL);
assert(task->state == TASK_ST_PAUSED);
assert(emu->cur_thread->state == TH_ST_RUNNING);
assert(emu->cur_thread->task == task);
assert(emu->cur_thread == task->thread);
task->state = TASK_ST_RUNNING;
emu->cur_task = task;
dbg("task id=%d resumes\n", task->id);
}
static void
pre_task_end(struct ovni_emu *emu)
{
struct nosv_task *task;
int taskid;
taskid = emu->cur_ev->payload.i32[0];
2021-07-30 21:37:25 +02:00
HASH_FIND_INT(emu->cur_proc->tasks, &taskid, task);
2021-07-29 17:46:25 +02:00
assert(task != NULL);
assert(task->state == TASK_ST_RUNNING);
assert(emu->cur_thread->state == TH_ST_RUNNING);
assert(emu->cur_thread->task == task);
assert(emu->cur_thread == task->thread);
task->state = TASK_ST_DEAD;
task->thread = NULL;
emu->cur_thread->task = NULL;
emu->cur_task = task;
dbg("task id=%d ends\n", task->id);
}
static void
pre_task(struct ovni_emu *emu)
{
emu_emit(emu);
2021-07-30 20:08:40 +02:00
switch(emu->cur_ev->header.value)
2021-07-29 17:46:25 +02:00
{
case 'c': pre_task_create(emu); break;
case 'x': pre_task_execute(emu); break;
case 'e': pre_task_end(emu); break;
case 'p': pre_task_pause(emu); break;
case 'r': pre_task_resume(emu); break;
default:
emu->cur_task = NULL;
break;
}
}
2021-07-29 18:13:41 +02:00
static void
pre_type_create(struct ovni_emu *emu)
{
2021-07-30 21:37:25 +02:00
struct nosv_task_type *type;
2021-07-30 20:08:40 +02:00
uint8_t *data;
uint32_t *typeid;
const char *label;
2021-07-29 18:13:41 +02:00
2021-07-30 20:08:40 +02:00
if((emu->cur_ev->header.flags & OVNI_EV_JUMBO) == 0)
{
err("expecting a jumbo event\n");
abort();
}
data = &emu->cur_ev->payload.jumbo.data[0];
typeid = (uint32_t *) data;
data += sizeof(*typeid);
label = (const char *) data;
2021-07-29 18:13:41 +02:00
/* Ensure the type id is new */
2021-07-30 21:37:25 +02:00
HASH_FIND_INT(emu->cur_proc->types, typeid, type);
2021-07-29 18:13:41 +02:00
if(type != NULL)
{
2021-07-30 20:08:40 +02:00
err("A task type with id %d already exists\n", *typeid);
2021-07-29 18:13:41 +02:00
abort();
}
type = calloc(1, sizeof(*type));
if(type == NULL)
{
perror("calloc");
abort();
}
2021-07-30 20:08:40 +02:00
type->id = *typeid;
type->label = label;
2021-07-29 18:13:41 +02:00
/* Add the new task type to the hash table */
2021-07-30 21:37:25 +02:00
HASH_ADD_INT(emu->cur_proc->types, id, type);
2021-07-29 18:13:41 +02:00
2021-07-30 20:08:40 +02:00
dbg("new task type created id=%d label=%s\n", type->id,
type->label);
2021-07-29 18:13:41 +02:00
}
static void
pre_type(struct ovni_emu *emu)
{
2021-07-30 20:08:40 +02:00
switch(emu->cur_ev->header.value)
2021-07-29 18:13:41 +02:00
{
case 'c': pre_type_create(emu); break;
default:
break;
}
}
2021-07-29 17:46:25 +02:00
void
hook_pre_nosv(struct ovni_emu *emu)
{
dbg("pre nosv\n");
2021-07-30 20:08:40 +02:00
switch(emu->cur_ev->header.class)
2021-07-29 17:46:25 +02:00
{
case 'T': pre_task(emu); break;
2021-07-29 18:13:41 +02:00
case 'Y': pre_type(emu); break;
2021-07-29 17:46:25 +02:00
default:
break;
}
}
2021-07-29 18:13:41 +02:00
/* --------------------------- emit ------------------------------- */
2021-07-29 17:46:25 +02:00
static void
2021-08-02 10:08:58 +02:00
emit_task_running(struct ovni_emu *emu, struct nosv_task *task)
2021-07-28 11:56:35 +02:00
{
2021-08-02 10:08:58 +02:00
prv_ev_autocpu(emu, PTC_TASK_ID, task->id + 1);
prv_ev_autocpu(emu, PTC_TASK_TYPE_ID, task->type_id + 1);
2021-07-28 11:56:35 +02:00
}
2021-07-29 17:46:25 +02:00
static void
2021-08-02 10:08:58 +02:00
emit_task_not_running(struct ovni_emu *emu, struct nosv_task *task)
2021-07-29 17:46:25 +02:00
{
2021-08-02 10:08:58 +02:00
prv_ev_autocpu(emu, PTC_TASK_ID, 0);
prv_ev_autocpu(emu, PTC_TASK_TYPE_ID, 0);
2021-07-29 17:46:25 +02:00
}
static void
2021-08-02 10:08:58 +02:00
emit_task(struct ovni_emu *emu)
2021-07-29 17:46:25 +02:00
{
2021-08-02 10:08:58 +02:00
struct nosv_task *task;
2021-07-29 17:46:25 +02:00
2021-08-02 10:08:58 +02:00
task = emu->cur_task;
2021-07-29 17:46:25 +02:00
2021-07-30 20:08:40 +02:00
switch(emu->cur_ev->header.value)
2021-07-29 17:46:25 +02:00
{
2021-08-02 10:08:58 +02:00
case 'x':
case 'r':
emit_task_running(emu, task);
break;
case 'p':
case 'e':
emit_task_not_running(emu, task);
break;
case 'c':
2021-07-29 17:46:25 +02:00
default:
break;
}
}
void
2021-07-30 21:37:25 +02:00
hook_emit_nosv(struct ovni_emu *emu)
2021-07-29 17:46:25 +02:00
{
dbg("pre nosv\n");
2021-07-30 20:08:40 +02:00
switch(emu->cur_ev->header.class)
2021-07-29 17:46:25 +02:00
{
case 'T': emit_task(emu); break;
default:
break;
}
}
struct hook_entry pre_hooks[] = {
{ "VTc", pre_task_create },
{ "VTx", pre_task_create },
};