Skip to content

Commit

Permalink
cpu: Factor out cpu_generic_init()
Browse files Browse the repository at this point in the history
All targets using it gain the ability to set -cpu name,key=value,...
options via the default TYPE_CPU CPUClass::parse_features() implementation.

Signed-off-by: Andreas Färber <[email protected]>
  • Loading branch information
afaerber committed Mar 13, 2014
1 parent 1590bbc commit 9262685
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 103 deletions.
11 changes: 11 additions & 0 deletions include/qom/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,17 @@ void cpu_reset(CPUState *cpu);
*/
ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);

/**
* cpu_generic_init:
* @typename: The CPU base type.
* @cpu_model: The model string including optional parameters.
*
* Instantiates a CPU, processes optional parameters and realizes the CPU.
*
* Returns: A #CPUState or %NULL if an error occurred.
*/
CPUState *cpu_generic_init(const char *typename, const char *cpu_model);

/**
* cpu_has_work:
* @cpu: The vCPU to check.
Expand Down
41 changes: 41 additions & 0 deletions qom/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "sysemu/kvm.h"
#include "qemu/notify.h"
#include "qemu/log.h"
#include "qemu/error-report.h"
#include "sysemu/sysemu.h"

bool cpu_exists(int64_t id)
Expand All @@ -39,6 +40,46 @@ bool cpu_exists(int64_t id)
return false;
}

CPUState *cpu_generic_init(const char *typename, const char *cpu_model)
{
char *str, *name, *featurestr;
CPUState *cpu;
ObjectClass *oc;
CPUClass *cc;
Error *err = NULL;

str = g_strdup(cpu_model);
name = strtok(str, ",");

oc = cpu_class_by_name(typename, name);
if (oc == NULL) {
g_free(str);
return NULL;
}

cpu = CPU(object_new(object_class_get_name(oc)));
cc = CPU_GET_CLASS(cpu);

featurestr = strtok(NULL, ",");
cc->parse_features(cpu, featurestr, &err);
g_free(str);
if (err != NULL) {
goto out;
}

object_property_set_bool(OBJECT(cpu), true, "realized", &err);

out:
if (err != NULL) {
error_report("%s", error_get_pretty(err));
error_free(err);
object_unref(OBJECT(cpu));
return NULL;
}

return cpu;
}

bool cpu_paging_enabled(const CPUState *cpu)
{
CPUClass *cc = CPU_GET_CLASS(cpu);
Expand Down
14 changes: 1 addition & 13 deletions target-arm/helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -2186,19 +2186,7 @@ void register_cp_regs_for_features(ARMCPU *cpu)

ARMCPU *cpu_arm_init(const char *cpu_model)
{
ARMCPU *cpu;
ObjectClass *oc;

oc = cpu_class_by_name(TYPE_ARM_CPU, cpu_model);
if (!oc) {
return NULL;
}
cpu = ARM_CPU(object_new(object_class_get_name(oc)));

/* TODO this should be set centrally, once possible */
object_property_set_bool(OBJECT(cpu), true, "realized", NULL);

return cpu;
return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model));
}

void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
Expand Down
13 changes: 1 addition & 12 deletions target-cris/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,7 @@ static ObjectClass *cris_cpu_class_by_name(const char *cpu_model)

CRISCPU *cpu_cris_init(const char *cpu_model)
{
CRISCPU *cpu;
ObjectClass *oc;

oc = cris_cpu_class_by_name(cpu_model);
if (oc == NULL) {
return NULL;
}
cpu = CRIS_CPU(object_new(object_class_get_name(oc)));

object_property_set_bool(OBJECT(cpu), true, "realized", NULL);

return cpu;
return CRIS_CPU(cpu_generic_init(TYPE_CRIS_CPU, cpu_model));
}

/* Sort alphabetically by VR. */
Expand Down
13 changes: 1 addition & 12 deletions target-lm32/helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,7 @@ void lm32_cpu_do_interrupt(CPUState *cs)

LM32CPU *cpu_lm32_init(const char *cpu_model)
{
LM32CPU *cpu;
ObjectClass *oc;

oc = cpu_class_by_name(TYPE_LM32_CPU, cpu_model);
if (oc == NULL) {
return NULL;
}
cpu = LM32_CPU(object_new(object_class_get_name(oc)));

object_property_set_bool(OBJECT(cpu), true, "realized", NULL);

return cpu;
return LM32_CPU(cpu_generic_init(TYPE_LM32_CPU, cpu_model));
}

/* Some soc ignores the MSB on the address bus. Thus creating a shadow memory
Expand Down
13 changes: 1 addition & 12 deletions target-moxie/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,7 @@ static const MoxieCPUInfo moxie_cpus[] = {

MoxieCPU *cpu_moxie_init(const char *cpu_model)
{
MoxieCPU *cpu;
ObjectClass *oc;

oc = moxie_cpu_class_by_name(cpu_model);
if (oc == NULL) {
return NULL;
}
cpu = MOXIE_CPU(object_new(object_class_get_name(oc)));

object_property_set_bool(OBJECT(cpu), true, "realized", NULL);

return cpu;
return MOXIE_CPU(cpu_generic_init(TYPE_MOXIE_CPU, cpu_model));
}

static void cpu_register(const MoxieCPUInfo *info)
Expand Down
13 changes: 1 addition & 12 deletions target-openrisc/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,7 @@ static void openrisc_cpu_register_types(void)

OpenRISCCPU *cpu_openrisc_init(const char *cpu_model)
{
OpenRISCCPU *cpu;
ObjectClass *oc;

oc = openrisc_cpu_class_by_name(cpu_model);
if (oc == NULL) {
return NULL;
}
cpu = OPENRISC_CPU(object_new(object_class_get_name(oc)));

object_property_set_bool(OBJECT(cpu), true, "realized", NULL);

return cpu;
return OPENRISC_CPU(cpu_generic_init(TYPE_OPENRISC_CPU, cpu_model));
}

/* Sort alphabetically by type name, except for "any". */
Expand Down
21 changes: 1 addition & 20 deletions target-ppc/translate_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -8220,26 +8220,7 @@ static ObjectClass *ppc_cpu_class_by_name(const char *name)

PowerPCCPU *cpu_ppc_init(const char *cpu_model)
{
PowerPCCPU *cpu;
ObjectClass *oc;
Error *err = NULL;

oc = ppc_cpu_class_by_name(cpu_model);
if (oc == NULL) {
return NULL;
}

cpu = POWERPC_CPU(object_new(object_class_get_name(oc)));

object_property_set_bool(OBJECT(cpu), true, "realized", &err);
if (err != NULL) {
error_report("%s", error_get_pretty(err));
error_free(err);
object_unref(OBJECT(cpu));
return NULL;
}

return cpu;
return POWERPC_CPU(cpu_generic_init(TYPE_POWERPC_CPU, cpu_model));
}

/* Sort by PVR, ordering special case "host" last. */
Expand Down
13 changes: 1 addition & 12 deletions target-sh4/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,7 @@ static ObjectClass *superh_cpu_class_by_name(const char *cpu_model)

SuperHCPU *cpu_sh4_init(const char *cpu_model)
{
SuperHCPU *cpu;
ObjectClass *oc;

oc = superh_cpu_class_by_name(cpu_model);
if (oc == NULL) {
return NULL;
}
cpu = SUPERH_CPU(object_new(object_class_get_name(oc)));

object_property_set_bool(OBJECT(cpu), true, "realized", NULL);

return cpu;
return SUPERH_CPU(cpu_generic_init(TYPE_SUPERH_CPU, cpu_model));
}

static void sh7750r_cpu_initfn(Object *obj)
Expand Down
13 changes: 3 additions & 10 deletions target-unicore32/helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,12 @@
CPUUniCore32State *uc32_cpu_init(const char *cpu_model)
{
UniCore32CPU *cpu;
CPUUniCore32State *env;
ObjectClass *oc;

oc = cpu_class_by_name(TYPE_UNICORE32_CPU, cpu_model);
if (oc == NULL) {
cpu = UNICORE32_CPU(cpu_generic_init(TYPE_UNICORE32_CPU, cpu_model));
if (cpu == NULL) {
return NULL;
}
cpu = UNICORE32_CPU(object_new(object_class_get_name(oc)));
env = &cpu->env;

object_property_set_bool(OBJECT(cpu), true, "realized", NULL);

return env;
return &cpu->env;
}

uint32_t HELPER(clo)(uint32_t x)
Expand Down

0 comments on commit 9262685

Please sign in to comment.