ovni/emu_ovni.c

492 lines
10 KiB
C
Raw Normal View History

2021-07-28 11:56:35 +02:00
#include "ovni.h"
#include "emu.h"
2021-08-02 10:08:58 +02:00
#include "prv.h"
2021-10-21 16:15:29 +02:00
#include "chan.h"
2021-07-28 11:56:35 +02:00
#include <assert.h>
/* The emulator ovni module provides the execution model by tracking the thread
* state and which threads run in each CPU */
/* --------------------------- init ------------------------------- */
void
hook_init_ovni(struct ovni_emu *emu)
{
struct ovni_ethread *th;
struct ovni_cpu *cpu;
struct ovni_trace *trace;
int i, row, type;
FILE *prv_th, *prv_cpu;
int64_t *clock;
clock = &emu->delta_time;
prv_th = emu->prv_thread;
prv_cpu = emu->prv_cpu;
trace = &emu->trace;
/* Init the ovni channels in all threads */
for(i=0; i<emu->total_nthreads; i++)
{
th = emu->global_thread[i];
row = th->gindex + 1;
chan_th_init(th, CHAN_OVNI_TID, CHAN_TRACK_NONE, row, prv_th, clock);
chan_th_init(th, CHAN_OVNI_PID, CHAN_TRACK_NONE, row, prv_th, clock);
chan_th_init(th, CHAN_OVNI_CPU, CHAN_TRACK_NONE, row, prv_th, clock);
chan_th_init(th, CHAN_OVNI_STATE, CHAN_TRACK_NONE, row, prv_th, clock);
chan_enable(&th->chan[CHAN_OVNI_TID], 1);
chan_set(&th->chan[CHAN_OVNI_TID], th->tid);
chan_enable(&th->chan[CHAN_OVNI_TID], 0);
chan_enable(&th->chan[CHAN_OVNI_PID], 1);
chan_set(&th->chan[CHAN_OVNI_PID], th->proc->pid);
chan_enable(&th->chan[CHAN_OVNI_PID], 0);
}
/* Init the ovni channels in all cpus */
for(i=0; i<emu->total_ncpus; i++)
{
cpu = emu->global_cpu[i];
row = cpu->gindex + 1;
chan_cpu_init(cpu, CHAN_OVNI_TID, CHAN_TRACK_NONE, row, prv_cpu, clock);
chan_cpu_init(cpu, CHAN_OVNI_PID, CHAN_TRACK_NONE, row, prv_cpu, clock);
chan_cpu_init(cpu, CHAN_OVNI_NTHREADS, CHAN_TRACK_NONE, row, prv_cpu, clock);
chan_enable(&cpu->chan[CHAN_OVNI_TID], 1);
chan_set(&cpu->chan[CHAN_OVNI_TID], 0);
chan_emit(&cpu->chan[CHAN_OVNI_TID]);
chan_enable(&cpu->chan[CHAN_OVNI_PID], 1);
chan_set(&cpu->chan[CHAN_OVNI_PID], 0);
chan_emit(&cpu->chan[CHAN_OVNI_PID]);
chan_enable(&cpu->chan[CHAN_OVNI_NTHREADS], 1);
chan_set(&cpu->chan[CHAN_OVNI_NTHREADS], cpu->nthreads);
chan_emit(&cpu->chan[CHAN_OVNI_NTHREADS]);
}
}
2021-10-21 16:15:29 +02:00
/* --------------------------- pre ------------------------------- */
static void
thread_set_channel_enabled(struct ovni_ethread *th, int enabled)
{
struct ovni_chan *chan;
2021-10-21 16:15:29 +02:00
int i;
2021-10-21 16:15:29 +02:00
for(i=0; i<CHAN_MAX; i++)
{
chan = &th->chan[i];
if(chan->track == CHAN_TRACK_TH_RUNNING)
{
chan_enable(chan, enabled);
dbg("thread %d: %s chan %d\n",
th->tid,
enabled ? "enable" : "disable",
i);
}
}
2021-10-21 16:15:29 +02:00
}
static void
thread_set_state(struct ovni_ethread *th, int state)
{
int enabled;
th->state = state;
enabled = (state == TH_ST_RUNNING ? 1 : 0);
thread_set_channel_enabled(th, enabled);
}
void
update_cpu(struct ovni_emu *emu, struct ovni_cpu *cpu)
{
int i, tid, pid, enabled;
struct ovni_loom *loom;
struct ovni_ethread *th;
struct ovni_chan *chan;
loom = emu->cur_loom;
chan_set(&cpu->chan[CHAN_OVNI_NTHREADS], cpu->nthreads);
th = NULL;
if(cpu->nthreads == 0)
{
/* No thread running */
tid = pid = 0;
}
else if(cpu->nthreads == 1)
{
/* Take the info from the unique thread */
tid = cpu->thread[0]->tid;
pid = cpu->thread[0]->proc->pid;
th = cpu->thread[0];
}
else
{
/* Multiple threads */
tid = pid = 1;
}
chan_set(&cpu->chan[CHAN_OVNI_TID], tid);
chan_set(&cpu->chan[CHAN_OVNI_PID], pid);
// enabled = (th != NULL && th->state == TH_ST_RUNNING ? 1 : 0);
//
// /* Update all channels that need to follow the running thread */
// for(i=0; i<CHAN_MAX; i++)
// {
// chan = &cpu->chan[i];
//
// if(chan->track == CHAN_TRACK_TH_RUNNING)
// {
// chan_enable(chan, enabled);
// dbg("cpu thread %d: %s chan %d\n",
// tid,
// enabled ? "enable" : "disable",
// i);
// }
// }
}
2021-07-28 19:12:20 +02:00
struct ovni_cpu *
2021-08-02 21:13:03 +02:00
emu_get_cpu(struct ovni_loom *loom, int cpuid)
2021-07-28 19:12:20 +02:00
{
assert(cpuid < OVNI_MAX_CPU);
if(cpuid < 0)
{
2021-08-02 21:13:03 +02:00
return &loom->vcpu;
2021-07-28 19:12:20 +02:00
}
2021-08-02 21:13:03 +02:00
return &loom->cpu[cpuid];
2021-07-28 19:12:20 +02:00
}
int
emu_cpu_find_thread(struct ovni_cpu *cpu, struct ovni_ethread *thread)
{
int i;
for(i=0; i<cpu->nthreads; i++)
if(cpu->thread[i] == thread)
break;
/* Not found */
if(i >= cpu->nthreads)
return -1;
return i;
}
void
emu_cpu_add_thread(struct ovni_emu *emu, struct ovni_cpu *cpu, struct ovni_ethread *thread)
2021-07-28 19:12:20 +02:00
{
/* Found, abort */
if(emu_cpu_find_thread(cpu, thread) >= 0)
abort();
assert(cpu->nthreads < OVNI_MAX_THR);
cpu->thread[cpu->nthreads++] = thread;
update_cpu(emu, cpu);
2021-07-28 19:12:20 +02:00
}
void
emu_cpu_remove_thread(struct ovni_emu *emu, struct ovni_cpu *cpu, struct ovni_ethread *thread)
2021-07-28 19:12:20 +02:00
{
int i, j;
i = emu_cpu_find_thread(cpu, thread);
/* Not found, abort */
if(i < 0)
abort();
for(j=i; j+1 < cpu->nthreads; j++)
cpu->thread[i] = cpu->thread[j+1];
cpu->nthreads--;
update_cpu(emu, cpu);
2021-07-28 19:12:20 +02:00
}
2021-07-28 11:56:35 +02:00
static void
2021-08-02 21:13:03 +02:00
print_threads_state(struct ovni_loom *loom)
2021-07-28 11:56:35 +02:00
{
struct ovni_cpu *cpu;
int i, j;
2021-08-02 21:13:03 +02:00
for(i=0; i<loom->ncpus; i++)
2021-07-28 11:56:35 +02:00
{
2021-08-02 21:13:03 +02:00
cpu = &loom->cpu[i];
2021-07-28 11:56:35 +02:00
dbg("-- cpu %d runs %lu threads:", i, cpu->nthreads);
2021-07-28 11:56:35 +02:00
for(j=0; j<cpu->nthreads; j++)
{
dbg(" %d", cpu->thread[j]->tid);
}
dbg("\n");
}
dbg("-- vcpu runs %lu threads:", loom->vcpu.nthreads);
2021-08-02 21:13:03 +02:00
for(j=0; j<loom->vcpu.nthreads; j++)
2021-07-28 11:56:35 +02:00
{
2021-08-02 21:13:03 +02:00
dbg(" %d", loom->vcpu.thread[j]->tid);
2021-07-28 11:56:35 +02:00
}
dbg("\n");
}
static void
2021-10-21 16:15:29 +02:00
pre_thread_execute(struct ovni_emu *emu)
2021-07-28 11:56:35 +02:00
{
struct ovni_cpu *cpu;
2021-10-21 16:15:29 +02:00
struct ovni_ethread *th;
2021-07-28 11:56:35 +02:00
int cpuid;
2021-10-21 16:15:29 +02:00
th = emu->cur_thread;
2021-07-28 11:56:35 +02:00
/* The thread cannot be already running */
2021-10-21 16:15:29 +02:00
assert(th->state != TH_ST_RUNNING);
2021-07-28 11:56:35 +02:00
cpuid = emu->cur_ev->payload.i32[0];
2021-10-21 16:15:29 +02:00
//dbg("thread %d runs in cpuid %d\n", th->tid,
2021-07-29 17:46:25 +02:00
// cpuid);
2021-08-02 21:13:03 +02:00
cpu = emu_get_cpu(emu->cur_loom, cpuid);
2021-07-28 11:56:35 +02:00
2021-10-21 16:15:29 +02:00
thread_set_state(th, TH_ST_RUNNING);
th->cpu = cpu;
2021-07-28 11:56:35 +02:00
2021-10-21 16:15:29 +02:00
emu_cpu_add_thread(emu, cpu, th);
/* Enable the cpu channel of the thread */
chan_enable(&th->chan[CHAN_OVNI_CPU], 1);
chan_set(&th->chan[CHAN_OVNI_CPU], cpu->gindex + 1);
/* Enable thread TID and PID */
chan_enable(&th->chan[CHAN_OVNI_TID], 1);
chan_enable(&th->chan[CHAN_OVNI_PID], 1);
/* Enable and set the thread state */
chan_enable(&th->chan[CHAN_OVNI_STATE], 1);
chan_set(&th->chan[CHAN_OVNI_STATE], th->state);
2021-07-28 11:56:35 +02:00
}
static void
2021-10-21 16:15:29 +02:00
pre_thread_end(struct ovni_emu *emu)
2021-07-28 11:56:35 +02:00
{
struct ovni_ethread *th;
2021-07-28 11:56:35 +02:00
th = emu->cur_thread;
assert(th->state == TH_ST_RUNNING);
assert(th->cpu);
emu_cpu_remove_thread(emu, th->cpu, th);
thread_set_state(th, TH_ST_DEAD);
th->cpu = NULL;
/* Disable the cpu channel of the thread */
chan_enable(&th->chan[CHAN_OVNI_CPU], 0);
2021-07-28 11:56:35 +02:00
/* Disable thread TID and PID */
chan_enable(&th->chan[CHAN_OVNI_TID], 0);
chan_enable(&th->chan[CHAN_OVNI_PID], 0);
/* Disable thread state */
chan_enable(&th->chan[CHAN_OVNI_STATE], 0);
2021-07-28 11:56:35 +02:00
}
static void
2021-10-21 16:15:29 +02:00
pre_thread_pause(struct ovni_emu *emu)
2021-07-28 11:56:35 +02:00
{
assert(emu->cur_thread->state == TH_ST_RUNNING);
assert(emu->cur_thread->cpu);
emu_cpu_remove_thread(emu, emu->cur_thread->cpu, emu->cur_thread);
2021-07-28 11:56:35 +02:00
2021-10-21 16:15:29 +02:00
thread_set_state(emu->cur_thread, TH_ST_PAUSED);
chan_set(&emu->cur_thread->chan[CHAN_OVNI_STATE], emu->cur_thread->state);
2021-07-28 11:56:35 +02:00
}
static void
2021-10-21 16:15:29 +02:00
pre_thread_resume(struct ovni_emu *emu)
2021-07-28 11:56:35 +02:00
{
assert(emu->cur_thread->state == TH_ST_PAUSED);
assert(emu->cur_thread->cpu);
emu_cpu_add_thread(emu, emu->cur_thread->cpu, emu->cur_thread);
2021-07-28 11:56:35 +02:00
2021-10-21 16:15:29 +02:00
thread_set_state(emu->cur_thread, TH_ST_RUNNING);
chan_set(&emu->cur_thread->chan[CHAN_OVNI_STATE], emu->cur_thread->state);
2021-07-28 11:56:35 +02:00
}
static void
2021-10-21 16:15:29 +02:00
pre_thread(struct ovni_emu *emu)
2021-07-28 11:56:35 +02:00
{
struct ovni_ev *ev;
struct ovni_cpu *cpu;
struct ovni_ethread *thread, *remote_thread;
int i;
2021-07-29 17:46:25 +02:00
//emu_emit(emu);
2021-07-28 11:56:35 +02:00
thread = emu->cur_thread;
cpu = thread->cpu;
ev = emu->cur_ev;
2021-07-30 20:08:40 +02:00
switch(ev->header.value)
2021-07-28 11:56:35 +02:00
{
case 'c': /* create */
dbg("thread %d creates a new thread at cpu=%d with args=%x %x\n",
thread->tid,
ev->payload.u32[0],
ev->payload.u32[1],
ev->payload.u32[2]);
break;
2021-10-21 16:15:29 +02:00
case 'x': pre_thread_execute(emu); break;
case 'e': pre_thread_end(emu); break;
case 'p': pre_thread_pause(emu); break;
case 'r': pre_thread_resume(emu); break;
2021-07-28 11:56:35 +02:00
default:
2021-09-27 17:42:14 +02:00
err("unknown thread event value %c\n",
ev->header.value);
exit(EXIT_FAILURE);
2021-07-28 11:56:35 +02:00
}
}
static void
2021-10-21 16:15:29 +02:00
pre_affinity_set(struct ovni_emu *emu)
2021-07-28 11:56:35 +02:00
{
int cpuid;
struct ovni_cpu *newcpu;
cpuid = emu->cur_ev->payload.i32[0];
assert(emu->cur_thread->state == TH_ST_RUNNING);
assert(emu->cur_thread->cpu);
/* Migrate current cpu to the one at cpuid */
2021-08-02 21:13:03 +02:00
newcpu = emu_get_cpu(emu->cur_loom, cpuid);
2021-07-28 11:56:35 +02:00
if(emu->cur_thread->cpu == newcpu)
{
err("warning: thread %d affinity already set to cpu %d\n",
emu->cur_thread->tid,
emu->cur_thread->cpu->gindex);
return;
}
emu_cpu_remove_thread(emu, emu->cur_thread->cpu, emu->cur_thread);
emu_cpu_add_thread(emu, newcpu, emu->cur_thread);
2021-07-28 11:56:35 +02:00
emu->cur_thread->cpu = newcpu;
chan_set(&emu->cur_thread->chan[CHAN_OVNI_CPU],
newcpu->gindex + 1);
2021-07-29 17:46:25 +02:00
//dbg("cpu %d now runs %d\n", cpuid, emu->cur_thread->tid);
2021-07-28 11:56:35 +02:00
}
static void
2021-10-21 16:15:29 +02:00
pre_affinity_remote(struct ovni_emu *emu)
2021-07-28 11:56:35 +02:00
{
int cpuid, tid;
struct ovni_cpu *newcpu;
struct ovni_ethread *thread;
cpuid = emu->cur_ev->payload.i32[0];
tid = emu->cur_ev->payload.i32[1];
thread = emu_get_thread(emu->cur_proc, tid);
2021-07-28 11:56:35 +02:00
assert(thread);
/* The thread may still be running */
assert(thread->state == TH_ST_RUNNING ||
thread->state == TH_ST_PAUSED);
/* It must have an assigned CPU */
2021-07-28 11:56:35 +02:00
assert(thread->cpu);
/* Migrate current cpu to the one at cpuid */
2021-08-02 21:13:03 +02:00
newcpu = emu_get_cpu(emu->cur_loom, cpuid);
2021-07-28 11:56:35 +02:00
/* If running, update the CPU thread lists */
if(thread->state == TH_ST_RUNNING)
{
emu_cpu_remove_thread(emu, thread->cpu, thread);
emu_cpu_add_thread(emu, newcpu, thread);
}
else
{
/* Otherwise, ensure that it is not in any CPU list */
assert(emu_cpu_find_thread(thread->cpu, thread) == -1);
assert(emu_cpu_find_thread(newcpu, thread) == -1);
}
2021-07-28 11:56:35 +02:00
/* Always set the assigned CPU in the thread */
2021-07-28 11:56:35 +02:00
thread->cpu = newcpu;
chan_set(&thread->chan[CHAN_OVNI_CPU],
newcpu->gindex + 1);
2021-07-29 17:46:25 +02:00
//dbg("thread %d switches to cpu %d by remote petition\n", tid,
// cpuid);
2021-07-28 11:56:35 +02:00
}
static void
2021-10-21 16:15:29 +02:00
pre_affinity(struct ovni_emu *emu)
2021-07-28 11:56:35 +02:00
{
2021-07-29 17:46:25 +02:00
//emu_emit(emu);
2021-07-30 20:08:40 +02:00
switch(emu->cur_ev->header.value)
2021-07-28 11:56:35 +02:00
{
2021-10-21 16:15:29 +02:00
case 's': pre_affinity_set(emu); break;
case 'r': pre_affinity_remote(emu); break;
2021-07-28 11:56:35 +02:00
default:
dbg("unknown affinity event value %c\n",
2021-07-30 20:08:40 +02:00
emu->cur_ev->header.value);
2021-07-28 11:56:35 +02:00
break;
}
}
void
2021-07-28 19:12:20 +02:00
hook_pre_ovni(struct ovni_emu *emu)
2021-07-28 11:56:35 +02:00
{
//emu_emit(emu);
if(emu->cur_ev->header.model != 'O')
return;
2021-07-30 20:08:40 +02:00
switch(emu->cur_ev->header.class)
2021-07-28 11:56:35 +02:00
{
2021-10-21 16:15:29 +02:00
case 'H': pre_thread(emu); break;
case 'A': pre_affinity(emu); break;
2021-08-03 19:56:31 +02:00
case 'B':
//dbg("burst %c\n", emu->cur_ev->header.value);
break;
2021-07-28 11:56:35 +02:00
default:
dbg("unknown ovni event class %c\n",
2021-07-30 20:08:40 +02:00
emu->cur_ev->header.class);
2021-07-28 11:56:35 +02:00
break;
}
2021-07-29 17:46:25 +02:00
//print_threads_state(emu);
2021-07-28 11:56:35 +02:00
}
2021-07-28 19:12:20 +02:00
2021-10-21 16:15:29 +02:00
/* --------------------------- post ------------------------------- */
void
hook_post_ovni(struct ovni_emu *emu)
{
2021-07-28 19:12:20 +02:00
}