#ifndef _CPU_C_
#define _CPU_C_
#include <setjmp.h>
#include "cpu.h"
#include "idecode.h"
#ifdef HAVE_STRING_H
#include <string.h>
#else
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#endif
struct _cpu {
registers regs;
unsigned_word program_counter;
core *physical;
vm *virtual;
vm_instruction_map *instruction_map;
vm_data_map *data_map;
cpu_mon *monitor;
os_emul *os_emulation;
psim *system;
event_queue *events;
int cpu_nr;
model_data *model_ptr;
#if WITH_IDECODE_CACHE_SIZE
idecode_cache icache[WITH_IDECODE_CACHE_SIZE];
#endif
interrupts ints;
memory_reservation reservation;
signed64 time_base_local_time;
signed64 decrementer_local_time;
event_entry_tag decrementer_event;
};
INLINE_CPU\
(cpu *)
cpu_create(psim *system,
core *memory,
cpu_mon *monitor,
os_emul *os_emulation,
int cpu_nr)
{
cpu *processor = ZALLOC(cpu);
processor->physical = memory;
processor->virtual = vm_create(memory);
processor->instruction_map = vm_create_instruction_map(processor->virtual);
processor->data_map = vm_create_data_map(processor->virtual);
if (CURRENT_MODEL_ISSUE > 0)
processor->model_ptr = model_create (processor);
processor->system = system;
processor->events = psim_event_queue(system);
processor->cpu_nr = cpu_nr;
processor->monitor = monitor;
processor->os_emulation = os_emulation;
return processor;
}
INLINE_CPU\
(void)
cpu_init(cpu *processor)
{
memset(&processor->regs, 0, sizeof(processor->regs));
if (CURRENT_MODEL_ISSUE > 0)
model_init (processor->model_ptr);
}
INLINE_CPU\
(psim *)
cpu_system(cpu *processor)
{
return processor->system;
}
INLINE_CPU\
(int)
cpu_nr(cpu *processor)
{
return processor->cpu_nr;
}
INLINE_CPU\
(cpu_mon *)
cpu_monitor(cpu *processor)
{
return processor->monitor;
}
INLINE_CPU\
(os_emul *)
cpu_os_emulation(cpu *processor)
{
return processor->os_emulation;
}
INLINE_CPU\
(model_data *)
cpu_model(cpu *processor)
{
return processor->model_ptr;
}
INLINE_CPU\
(void)
cpu_set_program_counter(cpu *processor,
unsigned_word new_program_counter)
{
processor->program_counter = new_program_counter;
}
INLINE_CPU\
(unsigned_word)
cpu_get_program_counter(cpu *processor)
{
return processor->program_counter;
}
INLINE_CPU\
(void)
cpu_restart(cpu *processor,
unsigned_word nia)
{
ASSERT(processor != NULL);
cpu_set_program_counter(processor, nia);
psim_restart(processor->system, processor->cpu_nr);
}
INLINE_CPU\
(void)
cpu_halt(cpu *processor,
unsigned_word nia,
stop_reason reason,
int signal)
{
ASSERT(processor != NULL);
if (CURRENT_MODEL_ISSUE > 0)
model_halt(processor->model_ptr);
cpu_set_program_counter(processor, nia);
psim_halt(processor->system, processor->cpu_nr, reason, signal);
}
EXTERN_CPU\
(void)
cpu_error(cpu *processor,
unsigned_word cia,
const char *fmt,
...)
{
char message[1024];
va_list ap;
va_start(ap, fmt);
vsprintf(message, fmt, ap);
va_end(ap);
if (strlen(message) >= sizeof(message))
error("cpu_error: buffer overflow");
if (processor != NULL) {
printf_filtered("cpu %d, cia 0x%lx: %s\n",
processor->cpu_nr + 1, (unsigned long)cia, message);
cpu_halt(processor, cia, was_signalled, -1);
}
else {
error("cpu: %s", message);
}
}
INLINE_CPU\
(signed64)
cpu_get_time_base(cpu *processor)
{
return (event_queue_time(processor->events)
- processor->time_base_local_time);
}
INLINE_CPU\
(void)
cpu_set_time_base(cpu *processor,
signed64 time_base)
{
processor->time_base_local_time = (event_queue_time(processor->events)
- time_base);
}
INLINE_CPU\
(signed32)
cpu_get_decrementer(cpu *processor)
{
return (processor->decrementer_local_time
- event_queue_time(processor->events));
}
STATIC_INLINE_CPU\
(void)
cpu_decrement_event(void *data)
{
cpu *processor = (cpu*)data;
processor->decrementer_event = NULL;
decrementer_interrupt(processor);
}
INLINE_CPU\
(void)
cpu_set_decrementer(cpu *processor,
signed32 decrementer)
{
signed64 old_decrementer = cpu_get_decrementer(processor);
event_queue_deschedule(processor->events, processor->decrementer_event);
processor->decrementer_event = NULL;
processor->decrementer_local_time = (event_queue_time(processor->events)
+ decrementer);
if (decrementer < 0 && old_decrementer >= 0)
decrementer_interrupt(processor);
else if (decrementer >= 0)
processor->decrementer_event = event_queue_schedule(processor->events,
decrementer,
cpu_decrement_event,
processor);
}
#if WITH_IDECODE_CACHE_SIZE
INLINE_CPU\
(idecode_cache *)
cpu_icache_entry(cpu *processor,
unsigned_word cia)
{
return &processor->icache[cia / 4 % WITH_IDECODE_CACHE_SIZE];
}
INLINE_CPU\
(void)
cpu_flush_icache(cpu *processor)
{
int i;
for (i = 0; i < WITH_IDECODE_CACHE_SIZE; i++)
processor->icache[i].address = MASK(0, 63);
}
#endif
INLINE_CPU\
(vm_instruction_map *)
cpu_instruction_map(cpu *processor)
{
return processor->instruction_map;
}
INLINE_CPU\
(vm_data_map *)
cpu_data_map(cpu *processor)
{
return processor->data_map;
}
INLINE_CPU\
(void)
cpu_page_tlb_invalidate_entry(cpu *processor,
unsigned_word ea)
{
vm_page_tlb_invalidate_entry(processor->virtual, ea);
}
INLINE_CPU\
(void)
cpu_page_tlb_invalidate_all(cpu *processor)
{
vm_page_tlb_invalidate_all(processor->virtual);
}
INLINE_CPU\
(interrupts *)
cpu_interrupts(cpu *processor)
{
return &processor->ints;
}
INLINE_CPU\
(memory_reservation *)
cpu_reservation(cpu *processor)
{
return &processor->reservation;
}
INLINE_CPU\
(registers *)
cpu_registers(cpu *processor)
{
return &processor->regs;
}
INLINE_CPU\
(void)
cpu_synchronize_context(cpu *processor,
unsigned_word cia)
{
#if (WITH_IDECODE_CACHE_SIZE)
cpu_flush_icache(processor);
#endif
vm_synchronize_context(processor->virtual,
processor->regs.spr,
processor->regs.sr,
processor->regs.msr,
processor, cia);
}
INLINE_CPU\
(void)
cpu_print_info(cpu *processor, int verbose)
{
}
#endif