diff mbox

[for-next,41/74] target-lm32: Let cpu_lm32_init() return LM32CPU

Message ID 1336318514-30906-42-git-send-email-afaerber@suse.de
State New
Headers show

Commit Message

Andreas Färber May 6, 2012, 3:34 p.m. UTC
Make the include paths for cpu-qom.h consistent to allow using LM32CPU
in cpu.h.

Let cpu_init() return CPULM32State for backwards compatibility.

Signed-off-by: Andreas Färber <afaerber@suse.de>
---
 target-lm32/cpu.c    |    2 +-
 target-lm32/cpu.h    |    4 ++--
 target-lm32/helper.c |    4 ++--
 3 files changed, 5 insertions(+), 5 deletions(-)

Comments

Michael Walle May 9, 2012, 6:13 p.m. UTC | #1
> Make the include paths for cpu-qom.h consistent to allow using LM32CPU
> in cpu.h.
> 
> Let cpu_init() return CPULM32State for backwards compatibility.
> 
> Signed-off-by: Andreas Färber <afaerber@suse.de>

[..]

Acked-by: Michael Walle <michael@walle.cc>
Andreas Färber May 9, 2012, 6:36 p.m. UTC | #2
Am 09.05.2012 20:13, schrieb Michael Walle:
>> Make the include paths for cpu-qom.h consistent to allow using LM32CPU
>> in cpu.h.
>>
>> Let cpu_init() return CPULM32State for backwards compatibility.
>>
>> Signed-off-by: Andreas Färber <afaerber@suse.de>
> 
> [..]
> 
> Acked-by: Michael Walle <michael@walle.cc>

Thanks. Needs a respin though: Peter has noticed that cpu_init() does a
NULL pointer deference when cpu_xxx_init() returns NULL. I'm rewriting
as static inline function.

Andreas
Peter Maydell May 9, 2012, 8:21 p.m. UTC | #3
On 9 May 2012 19:36, Andreas Färber <afaerber@suse.de> wrote:
> Thanks. Needs a respin though: Peter has noticed that cpu_init() does a
> NULL pointer deference when cpu_xxx_init() returns NULL. I'm rewriting
> as static inline function.

Well, strictly cpu_init() doesn't dereference NULL, because of
the address-of operator: instead it just effectively returns
offsetof(LM32CPU, env). This (being non-zero) doesn't trigger
the caller's "did that fail?" check, and we get a segfault
when the caller later tries to use it as a real pointer.

-- PMM
diff mbox

Patch

diff --git a/target-lm32/cpu.c b/target-lm32/cpu.c
index 48a5fe3..caa4834 100644
--- a/target-lm32/cpu.c
+++ b/target-lm32/cpu.c
@@ -18,7 +18,7 @@ 
  * <http://www.gnu.org/licenses/lgpl-2.1.html>
  */
 
-#include "cpu-qom.h"
+#include "cpu.h"
 #include "qemu-common.h"
 
 
diff --git a/target-lm32/cpu.h b/target-lm32/cpu.h
index 422a55b..642f19b 100644
--- a/target-lm32/cpu.h
+++ b/target-lm32/cpu.h
@@ -186,7 +186,7 @@  struct CPULM32State {
 
 #include "cpu-qom.h"
 
-CPULM32State *cpu_lm32_init(const char *cpu_model);
+LM32CPU *cpu_lm32_init(const char *cpu_model);
 void cpu_lm32_list(FILE *f, fprintf_function cpu_fprintf);
 int cpu_lm32_exec(CPULM32State *s);
 void cpu_lm32_close(CPULM32State *s);
@@ -200,7 +200,7 @@  void lm32_translate_init(void);
 void cpu_lm32_set_phys_msb_ignore(CPULM32State *env, int value);
 
 #define cpu_list cpu_lm32_list
-#define cpu_init cpu_lm32_init
+#define cpu_init(model) (&cpu_lm32_init(model)->env)
 #define cpu_exec cpu_lm32_exec
 #define cpu_gen_code cpu_lm32_gen_code
 #define cpu_signal_handler cpu_lm32_signal_handler
diff --git a/target-lm32/helper.c b/target-lm32/helper.c
index d0bc193..3b1cee7 100644
--- a/target-lm32/helper.c
+++ b/target-lm32/helper.c
@@ -192,7 +192,7 @@  static uint32_t cfg_by_def(const LM32Def *def)
     return cfg;
 }
 
-CPULM32State *cpu_lm32_init(const char *cpu_model)
+LM32CPU *cpu_lm32_init(const char *cpu_model)
 {
     LM32CPU *cpu;
     CPULM32State *env;
@@ -219,7 +219,7 @@  CPULM32State *cpu_lm32_init(const char *cpu_model)
         lm32_translate_init();
     }
 
-    return env;
+    return cpu;
 }
 
 /* Some soc ignores the MSB on the address bus. Thus creating a shadow memory