Skip to contentSkip to conversation

cpu-exec: Restructure cpu_exec()

PATCH In review Sergey Fedorov 3 participants 15 messages May 10, 2016

lore
Share & tools
lore
#0 lore raw

cover cpu-exec: Restructure cpu_exec()

From: Sergey Fedorov <[email protected]>

cpu_exec() was a huge function also sprinkled with some preprocessor directives. It was hard to read and see the main loop crowded by all this code. Restructure cpu_exec() by moving its conceptual parts into separate static inline functions. That makes it possible to see the whole main loop at once, especially its sigsetjmp() handling part.

This series is based on commit

    40f646483a11 (cpu-exec: Remove relic orphaned comment)
from
    git://github.com/rth7680/qemu.git tcg-next

and is available at
git://github.com/sergefdrv/qemu.git cpu_exec-restructure

Sergey Fedorov (5):
cpu-exec: Move halt handling out of cpu_exec()
cpu-exec: Move exception handling out of cpu_exec()
cpu-exec: Move interrupt handling out of cpu_exec()
cpu-exec: Move TB execution stuff out of cpu_exec()
cpu-exec: Remove unused 'x86_cpu' and 'env' from cpu_exec()

 cpu-exec.c | 395 +++++++++++++++++++++++++++++++++----------------------------
 1 file changed, 211 insertions(+), 184 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/5 cpu-exec: Move halt handling out of cpu_exec()

From: Sergey Fedorov <[email protected]>

Simplify cpu_exec() by extracting CPU halt state handling code out of cpu_exec() into a new static inline function cpu_handle_halt().

---
 cpu-exec.c | 39 +++++++++++++++++++++++++--------------
 1 file changed, 25 insertions(+), 14 deletions(-)
cpu-exec.c +25 -14
index d55faa5114c7..fb72f102742c 100644
@@ -352,6 +352,29 @@ static inline TranslationBlock *tb_find_fast(CPUState *cpu,
     return tb;
 }
 
+static inline bool cpu_handle_halt(CPUState *cpu)
+{
+    if (cpu->halted) {
+#if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
+        X86CPU *x86_cpu = X86_CPU(cpu);
+
+        if ((cpu->interrupt_request & CPU_INTERRUPT_POLL)
+            && replay_interrupt()) {
+            apic_poll_irq(x86_cpu->apic_state);
+            cpu_reset_interrupt(cpu, CPU_INTERRUPT_POLL);
+        }
+#endif
+        if (!cpu_has_work(cpu)) {
+            current_cpu = NULL;
+            return true;
+        }
+
+        cpu->halted = 0;
+    }
+
+    return false;
+}
+
 static void cpu_handle_debug_exception(CPUState *cpu)
 {
     CPUClass *cc = CPU_GET_CLASS(cpu);
@@ -383,20 +406,8 @@ int cpu_exec(CPUState *cpu)
     /* replay_interrupt may need current_cpu */
     current_cpu = cpu;
 
-    if (cpu->halted) {
-#if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
-        if ((cpu->interrupt_request & CPU_INTERRUPT_POLL)
-            && replay_interrupt()) {
-            apic_poll_irq(x86_cpu->apic_state);
-            cpu_reset_interrupt(cpu, CPU_INTERRUPT_POLL);
-        }
-#endif
-        if (!cpu_has_work(cpu)) {
-            current_cpu = NULL;
-            return EXCP_HALTED;
-        }
-
-        cpu->halted = 0;
+    if (cpu_handle_halt(cpu)) {
+        return EXCP_HALTED;
     }
 
     atomic_mb_set(&tcg_current_cpu, cpu);
Signature · 1 line
1.9.1
Reviewed-by on v1 1/5
#2 lore raw

Re: [Qemu-devel] [PATCH 1/5] cpu-exec: Move halt handling out of cpu_exec()

cpu-exec.c static inline TranslationBlock *tb_find_fast(CPUState *cpu, · line 363

Quoted 6 lines · Sergey Fedorov
cpu-exec.c +6 -0
+#if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
+        X86CPU *x86_cpu = X86_CPU(cpu);
+
+        if ((cpu->interrupt_request & CPU_INTERRUPT_POLL)
+            && replay_interrupt()) {
+            apic_poll_irq(x86_cpu->apic_state);

Since you're moving this around, you might as well place the x86_cpu variable in the inner-most if, next to its only use.

Otherwise,

r~

#3 lore raw

Re: [Qemu-devel] [PATCH 1/5] cpu-exec: Move halt handling out of cpu_exec()

Quoted 10 lines · Richard Henderson

cpu-exec.c static inline TranslationBlock *tb_find_fast(CPUState *cpu, · line 363

Quoted 6 lines · Sergey Fedorov
cpu-exec.c +6 -0
+#if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
+        X86CPU *x86_cpu = X86_CPU(cpu);
+
+        if ((cpu->interrupt_request & CPU_INTERRUPT_POLL)
+            && replay_interrupt()) {
+            apic_poll_irq(x86_cpu->apic_state);

Since you're moving this around, you might as well place the x86_cpu variable in the inner-most if, next to its only use.

Agree, will fix in v2.

Quoted 4 lines

Otherwise,

Kind regards,
Sergey

#4 lore raw

PATCH 2/5 cpu-exec: Move exception handling out of cpu_exec()

In reply to Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Simplify cpu_exec() by extracting exception handling code out of cpu_exec() into a new static inline function cpu_handle_exception(). Also make cpu_handle_debug_exception() inline as it is used only once.

---
 cpu-exec.c | 93 +++++++++++++++++++++++++++++++++++---------------------------
 1 file changed, 52 insertions(+), 41 deletions(-)
cpu-exec.c +52 -41
index fb72f102742c..ad95ace38dd9 100644
@@ -375,7 +375,7 @@ static inline bool cpu_handle_halt(CPUState *cpu)
     return false;
 }
 
-static void cpu_handle_debug_exception(CPUState *cpu)
+static inline void cpu_handle_debug_exception(CPUState *cpu)
 {
     CPUClass *cc = CPU_GET_CLASS(cpu);
     CPUWatchpoint *wp;
@@ -389,6 +389,55 @@ static void cpu_handle_debug_exception(CPUState *cpu)
     cc->debug_excp_handler(cpu);
 }
 
+static inline bool cpu_handle_exception(CPUState *cpu, int *ret)
+{
+    if (cpu->exception_index >= 0) {
+        if (cpu->exception_index >= EXCP_INTERRUPT) {
+            /* exit request from the cpu execution loop */
+            *ret = cpu->exception_index;
+            if (*ret == EXCP_DEBUG) {
+                cpu_handle_debug_exception(cpu);
+            }
+            cpu->exception_index = -1;
+            return true;
+        } else {
+#if defined(CONFIG_USER_ONLY)
+            /* if user mode only, we simulate a fake exception
+               which will be handled outside the cpu execution
+               loop */
+#if defined(TARGET_I386)
+            CPUClass *cc = CPU_GET_CLASS(cpu);
+            cc->do_interrupt(cpu);
+#endif
+            *ret = cpu->exception_index;
+            cpu->exception_index = -1;
+            return true;
+#else
+            if (replay_exception()) {
+                CPUClass *cc = CPU_GET_CLASS(cpu);
+                cc->do_interrupt(cpu);
+                cpu->exception_index = -1;
+            } else if (!replay_has_interrupt()) {
+                /* give a chance to iothread in replay mode */
+                *ret = EXCP_INTERRUPT;
+                return true;
+            }
+#endif
+        }
+#ifndef CONFIG_USER_ONLY
+    } else if (replay_has_exception()
+               && cpu->icount_decr.u16.low + cpu->icount_extra == 0) {
+        /* try to cause an exception pending in the log */
+        TranslationBlock *last_tb = NULL; /* Avoid chaining TBs */
+        cpu_exec_nocache(cpu, 1, tb_find_fast(cpu, &last_tb, 0), true);
+        *ret = -1;
+        return true;
+#endif
+    }
+
+    return false;
+}
+
 /* main execution loop */
 
 int cpu_exec(CPUState *cpu)
@@ -426,50 +475,12 @@ int cpu_exec(CPUState *cpu)
      */
     init_delay_params(&sc, cpu);
 
-    /* prepare setjmp context for exception handling */
     for(;;) {
+        /* prepare setjmp context for exception handling */
         if (sigsetjmp(cpu->jmp_env, 0) == 0) {
             /* if an exception is pending, we execute it here */
-            if (cpu->exception_index >= 0) {
-                if (cpu->exception_index >= EXCP_INTERRUPT) {
-                    /* exit request from the cpu execution loop */
-                    ret = cpu->exception_index;
-                    if (ret == EXCP_DEBUG) {
-                        cpu_handle_debug_exception(cpu);
-                    }
-                    cpu->exception_index = -1;
-                    break;
-                } else {
-#if defined(CONFIG_USER_ONLY)
-                    /* if user mode only, we simulate a fake exception
-                       which will be handled outside the cpu execution
-                       loop */
-#if defined(TARGET_I386)
-                    cc->do_interrupt(cpu);
-#endif
-                    ret = cpu->exception_index;
-                    cpu->exception_index = -1;
-                    break;
-#else
-                    if (replay_exception()) {
-                        cc->do_interrupt(cpu);
-                        cpu->exception_index = -1;
-                    } else if (!replay_has_interrupt()) {
-                        /* give a chance to iothread in replay mode */
-                        ret = EXCP_INTERRUPT;
-                        break;
-                    }
-#endif
-                }
-#ifndef CONFIG_USER_ONLY
-            } else if (replay_has_exception()
-                       && cpu->icount_decr.u16.low + cpu->icount_extra == 0) {
-                /* try to cause an exception pending in the log */
-                last_tb = NULL; /* Avoid chaining TBs */
-                cpu_exec_nocache(cpu, 1, tb_find_fast(cpu, &last_tb, 0), true);
-                ret = -1;
+            if (cpu_handle_exception(cpu, &ret)) {
                 break;
-#endif
             }
 
             last_tb = NULL; /* forget the last executed TB after exception */
Signature · 1 line
1.9.1
Reviewed-by on v1 2/5
#5 lore raw

Re: [Qemu-devel] [PATCH 2/5] cpu-exec: Move exception handling out of cpu_exec()

Quoted 5 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Simplify cpu_exec() by extracting exception handling code out of cpu_exec() into a new static inline function cpu_handle_exception(). Also make cpu_handle_debug_exception() inline as it is used only once.

If it's used only once, the compiler is going to do this anyway, and therefore there's no point in making the change. Let's just leave off all the inline markers and trust the compiler, eh?

Otherwise,

r~

#6 lore raw

Re: [Qemu-devel] [PATCH 2/5] cpu-exec: Move exception handling out of cpu_exec()

Quoted 10 lines · Richard Henderson
Quoted 5 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Simplify cpu_exec() by extracting exception handling code out of cpu_exec() into a new static inline function cpu_handle_exception(). Also make cpu_handle_debug_exception() inline as it is used only once.

If it's used only once, the compiler is going to do this anyway, and therefore there's no point in making the change. Let's just leave off all the inline markers and trust the compiler, eh?

I agree the compiler is smart enough to decide and inline such functions by itself. But actually, I hope such "static inline" in .c file could indicate for a reader of the code that this function is going to be used this way.

Quoted 4 lines

Otherwise,

Kind regards,
Sergey

#7 lore raw

PATCH 3/5 cpu-exec: Move interrupt handling out of cpu_exec()

In reply to Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Simplify cpu_exec() by extracting interrupt handling code outside of cpu_exec() into a new static inline function cpu_handle_interrupt().

---
 cpu-exec.c | 132 ++++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 70 insertions(+), 62 deletions(-)
cpu-exec.c +70 -62
index ad95ace38dd9..0760d5dc274d 100644
@@ -438,6 +438,74 @@ static inline bool cpu_handle_exception(CPUState *cpu, int *ret)
     return false;
 }
 
+static inline void cpu_handle_interrupt(CPUState *cpu,
+                                        TranslationBlock **last_tb)
+{
+    CPUClass *cc = CPU_GET_CLASS(cpu);
+    int interrupt_request = cpu->interrupt_request;
+
+    if (unlikely(interrupt_request)) {
+        if (unlikely(cpu->singlestep_enabled & SSTEP_NOIRQ)) {
+            /* Mask out external interrupts for this step. */
+            interrupt_request &= ~CPU_INTERRUPT_SSTEP_MASK;
+        }
+        if (interrupt_request & CPU_INTERRUPT_DEBUG) {
+            cpu->interrupt_request &= ~CPU_INTERRUPT_DEBUG;
+            cpu->exception_index = EXCP_DEBUG;
+            cpu_loop_exit(cpu);
+        }
+        if (replay_mode == REPLAY_MODE_PLAY && !replay_has_interrupt()) {
+            /* Do nothing */
+        } else if (interrupt_request & CPU_INTERRUPT_HALT) {
+            replay_interrupt();
+            cpu->interrupt_request &= ~CPU_INTERRUPT_HALT;
+            cpu->halted = 1;
+            cpu->exception_index = EXCP_HLT;
+            cpu_loop_exit(cpu);
+        }
+#if defined(TARGET_I386)
+        else if (interrupt_request & CPU_INTERRUPT_INIT) {
+            X86CPU *x86_cpu = X86_CPU(cpu);
+            CPUArchState *env = &x86_cpu->env;
+            replay_interrupt();
+            cpu_svm_check_intercept_param(env, SVM_EXIT_INIT, 0);
+            do_cpu_init(x86_cpu);
+            cpu->exception_index = EXCP_HALTED;
+            cpu_loop_exit(cpu);
+        }
+#else
+        else if (interrupt_request & CPU_INTERRUPT_RESET) {
+            replay_interrupt();
+            cpu_reset(cpu);
+            cpu_loop_exit(cpu);
+        }
+#endif
+        /* The target hook has 3 exit conditions:
+           False when the interrupt isn't processed,
+           True when it is, and we should restart on a new TB,
+           and via longjmp via cpu_loop_exit.  */
+        else {
+            replay_interrupt();
+            if (cc->cpu_exec_interrupt(cpu, interrupt_request)) {
+                *last_tb = NULL;
+            }
+        }
+        /* Don't use the cached interrupt_request value,
+           do_interrupt may have updated the EXITTB flag. */
+        if (cpu->interrupt_request & CPU_INTERRUPT_EXITTB) {
+            cpu->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
+            /* ensure that no TB jump will be modified as
+               the program flow was changed */
+            *last_tb = NULL;
+        }
+    }
+    if (unlikely(cpu->exit_request || replay_has_interrupt())) {
+        cpu->exit_request = 0;
+        cpu->exception_index = EXCP_INTERRUPT;
+        cpu_loop_exit(cpu);
+    }
+}
+
 /* main execution loop */
 
 int cpu_exec(CPUState *cpu)
@@ -447,7 +515,7 @@ int cpu_exec(CPUState *cpu)
     X86CPU *x86_cpu = X86_CPU(cpu);
     CPUArchState *env = &x86_cpu->env;
 #endif
-    int ret, interrupt_request;
+    int ret;
     TranslationBlock *tb, *last_tb;
     int tb_exit = 0;
     SyncClocks sc;
@@ -486,67 +554,7 @@ int cpu_exec(CPUState *cpu)
             last_tb = NULL; /* forget the last executed TB after exception */
             cpu->tb_flushed = false; /* reset before first TB lookup */
             for(;;) {
-                interrupt_request = cpu->interrupt_request;
-                if (unlikely(interrupt_request)) {
-                    if (unlikely(cpu->singlestep_enabled & SSTEP_NOIRQ)) {
-                        /* Mask out external interrupts for this step. */
-                        interrupt_request &= ~CPU_INTERRUPT_SSTEP_MASK;
-                    }
-                    if (interrupt_request & CPU_INTERRUPT_DEBUG) {
-                        cpu->interrupt_request &= ~CPU_INTERRUPT_DEBUG;
-                        cpu->exception_index = EXCP_DEBUG;
-                        cpu_loop_exit(cpu);
-                    }
-                    if (replay_mode == REPLAY_MODE_PLAY
-                        && !replay_has_interrupt()) {
-                        /* Do nothing */
-                    } else if (interrupt_request & CPU_INTERRUPT_HALT) {
-                        replay_interrupt();
-                        cpu->interrupt_request &= ~CPU_INTERRUPT_HALT;
-                        cpu->halted = 1;
-                        cpu->exception_index = EXCP_HLT;
-                        cpu_loop_exit(cpu);
-                    }
-#if defined(TARGET_I386)
-                    else if (interrupt_request & CPU_INTERRUPT_INIT) {
-                        replay_interrupt();
-                        cpu_svm_check_intercept_param(env, SVM_EXIT_INIT, 0);
-                        do_cpu_init(x86_cpu);
-                        cpu->exception_index = EXCP_HALTED;
-                        cpu_loop_exit(cpu);
-                    }
-#else
-                    else if (interrupt_request & CPU_INTERRUPT_RESET) {
-                        replay_interrupt();
-                        cpu_reset(cpu);
-                        cpu_loop_exit(cpu);
-                    }
-#endif
-                    /* The target hook has 3 exit conditions:
-                       False when the interrupt isn't processed,
-                       True when it is, and we should restart on a new TB,
-                       and via longjmp via cpu_loop_exit.  */
-                    else {
-                        replay_interrupt();
-                        if (cc->cpu_exec_interrupt(cpu, interrupt_request)) {
-                            last_tb = NULL;
-                        }
-                    }
-                    /* Don't use the cached interrupt_request value,
-                       do_interrupt may have updated the EXITTB flag. */
-                    if (cpu->interrupt_request & CPU_INTERRUPT_EXITTB) {
-                        cpu->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
-                        /* ensure that no TB jump will be modified as
-                           the program flow was changed */
-                        last_tb = NULL;
-                    }
-                }
-                if (unlikely(cpu->exit_request
-                             || replay_has_interrupt())) {
-                    cpu->exit_request = 0;
-                    cpu->exception_index = EXCP_INTERRUPT;
-                    cpu_loop_exit(cpu);
-                }
+                cpu_handle_interrupt(cpu, &last_tb);
                 tb = tb_find_fast(cpu, &last_tb, tb_exit);
                 if (likely(!cpu->exit_request)) {
                     uintptr_t ret;
Signature · 1 line
1.9.1
Reviewed-by on v1 3/5
#8 lore raw

Re: [Qemu-devel] [PATCH 3/5] cpu-exec: Move interrupt handling out of cpu_exec()

Quoted 10 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Simplify cpu_exec() by extracting interrupt handling code outside of cpu_exec() into a new static inline function cpu_handle_interrupt().

---
 cpu-exec.c | 132 ++++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 70 insertions(+), 62 deletions(-)
Quoted 5 lines
cpu-exec.c +5 -0
+        if (replay_mode == REPLAY_MODE_PLAY && !replay_has_interrupt()) {
+            /* Do nothing */
+        } else if (interrupt_request & CPU_INTERRUPT_HALT) {
+        }
+        else if (interrupt_request & CPU_INTERRUPT_RESET) {

cpu-exec.c static inline bool cpu_handle_exception(CPUState *cpu, int *ret) · line 495

Quoted 10 lines
cpu-exec.c +10 -0
+        }
+        else {
+            replay_interrupt();
+            if (cc->cpu_exec_interrupt(cpu, interrupt_request)) {
+                *last_tb = NULL;
+            }
+        }
+        /* Don't use the cached interrupt_request value,
+           do_interrupt may have updated the EXITTB flag. */
+        if (cpu->interrupt_request & CPU_INTERRUPT_EXITTB) {

Note for future cleanup: IMO this comment is cleaner if it's actually put where it's meaningful (and updated to reflect that do_interrupt no longer exists). E.g.

    else {
	if (cc->cpu_exec_interrupt(cpu, interrupt_request)) {
	    *last_tb = NULL;
	}
	/* Reload the interrupt_request value as it may have
	   been updated by the target hook.  */
	interrupt_request = cpu->interrupt_request;
    }
    if (interupt_request & CPU_INTERRUPT_EXITTB) {
	...

But such a change of course belongs in a separate patch.

r~

#9 lore raw

Re: [Qemu-devel] [PATCH 3/5] cpu-exec: Move interrupt handling out of cpu_exec()

Quoted 48 lines · Richard Henderson
Quoted 11 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Simplify cpu_exec() by extracting interrupt handling code outside of cpu_exec() into a new static inline function cpu_handle_interrupt().

---
 cpu-exec.c | 132
++++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 70 insertions(+), 62 deletions(-)
Quoted 6 lines

+ if (replay_mode == REPLAY_MODE_PLAY &&
!replay_has_interrupt()) {

cpu-exec.c +4 -0
+            /* Do nothing */
+        } else if (interrupt_request & CPU_INTERRUPT_HALT) {
+        }
+        else if (interrupt_request & CPU_INTERRUPT_RESET) {

cpu-exec.c static inline bool cpu_handle_exception(CPUState *cpu, int *ret) · line 495

Quoted 10 lines
cpu-exec.c +10 -0
+        }
+        else {
+            replay_interrupt();
+            if (cc->cpu_exec_interrupt(cpu, interrupt_request)) {
+                *last_tb = NULL;
+            }
+        }
+        /* Don't use the cached interrupt_request value,
+           do_interrupt may have updated the EXITTB flag. */
+        if (cpu->interrupt_request & CPU_INTERRUPT_EXITTB) {

Note for future cleanup: IMO this comment is cleaner if it's actually put where it's meaningful (and updated to reflect that do_interrupt no longer exists). E.g.

    else {
    if (cc->cpu_exec_interrupt(cpu, interrupt_request)) {
        *last_tb = NULL;
    }
    /* Reload the interrupt_request value as it may have
       been updated by the target hook.  */
    interrupt_request = cpu->interrupt_request;
    }
    if (interupt_request & CPU_INTERRUPT_EXITTB) {
    ...
Quoted 1 line

But such a change of course belongs in a separate patch.

Cool, thanks for the suggestion. I've had feeling this could be expressed in a better way, like you suggest :)

Kind regards,
Sergey

#10 lore raw

PATCH 4/5 cpu-exec: Move TB execution stuff out of cpu_exec()

In reply to Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Simplify cpu_exec() by extracting TB execution code outside of cpu_exec() into a new static inline function cpu_loop_exec_tb().

---
 cpu-exec.c | 119 +++++++++++++++++++++++++++++++++----------------------------
 1 file changed, 64 insertions(+), 55 deletions(-)
cpu-exec.c +64 -55
index 0760d5dc274d..60f565074618 100644
@@ -506,6 +506,66 @@ static inline void cpu_handle_interrupt(CPUState *cpu,
     }
 }
 
+static inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb,
+                                    TranslationBlock **last_tb, int *tb_exit,
+                                    SyncClocks *sc)
+{
+    uintptr_t ret;
+
+    if (unlikely(cpu->exit_request)) {
+        return;
+    }
+
+    trace_exec_tb(tb, tb->pc);
+    ret = cpu_tb_exec(cpu, tb);
+    *last_tb = (TranslationBlock *)(ret & ~TB_EXIT_MASK);
+    *tb_exit = ret & TB_EXIT_MASK;
+    switch (*tb_exit) {
+    case TB_EXIT_REQUESTED:
+        /* Something asked us to stop executing
+         * chained TBs; just continue round the main
+         * loop. Whatever requested the exit will also
+         * have set something else (eg exit_request or
+         * interrupt_request) which we will handle
+         * next time around the loop.  But we need to
+         * ensure the tcg_exit_req read in generated code
+         * comes before the next read of cpu->exit_request
+         * or cpu->interrupt_request.
+         */
+        smp_rmb();
+        *last_tb = NULL;
+        break;
+    case TB_EXIT_ICOUNT_EXPIRED:
+    {
+        /* Instruction counter expired.  */
+#ifdef CONFIG_USER_ONLY
+        abort();
+#else
+        int insns_left = cpu->icount_decr.u32;
+        if (cpu->icount_extra && insns_left >= 0) {
+            /* Refill decrementer and continue execution.  */
+            cpu->icount_extra += insns_left;
+            insns_left = MIN(0xffff, cpu->icount_extra);
+            cpu->icount_extra -= insns_left;
+            cpu->icount_decr.u16.low = insns_left;
+        } else {
+            if (insns_left > 0) {
+                /* Execute remaining instructions.  */
+                cpu_exec_nocache(cpu, insns_left, *last_tb, false);
+                align_clocks(sc, cpu);
+            }
+            cpu->exception_index = EXCP_INTERRUPT;
+            *last_tb = NULL;
+            cpu_loop_exit(cpu);
+        }
+        break;
+#endif
+    }
+    default:
+        break;
+    }
+}
+
 /* main execution loop */
 
 int cpu_exec(CPUState *cpu)
@@ -516,8 +576,6 @@ int cpu_exec(CPUState *cpu)
     CPUArchState *env = &x86_cpu->env;
 #endif
     int ret;
-    TranslationBlock *tb, *last_tb;
-    int tb_exit = 0;
     SyncClocks sc;
 
     /* replay_interrupt may need current_cpu */
@@ -544,6 +602,9 @@ int cpu_exec(CPUState *cpu)
     init_delay_params(&sc, cpu);
 
     for(;;) {
+        TranslationBlock *tb, *last_tb;
+        int tb_exit = 0;
+
         /* prepare setjmp context for exception handling */
         if (sigsetjmp(cpu->jmp_env, 0) == 0) {
             /* if an exception is pending, we execute it here */
@@ -556,59 +617,7 @@ int cpu_exec(CPUState *cpu)
             for(;;) {
                 cpu_handle_interrupt(cpu, &last_tb);
                 tb = tb_find_fast(cpu, &last_tb, tb_exit);
-                if (likely(!cpu->exit_request)) {
-                    uintptr_t ret;
-                    trace_exec_tb(tb, tb->pc);
-                    /* execute the generated code */
-                    ret = cpu_tb_exec(cpu, tb);
-                    last_tb = (TranslationBlock *)(ret & ~TB_EXIT_MASK);
-                    tb_exit = ret & TB_EXIT_MASK;
-                    switch (tb_exit) {
-                    case TB_EXIT_REQUESTED:
-                        /* Something asked us to stop executing
-                         * chained TBs; just continue round the main
-                         * loop. Whatever requested the exit will also
-                         * have set something else (eg exit_request or
-                         * interrupt_request) which we will handle
-                         * next time around the loop.  But we need to
-                         * ensure the tcg_exit_req read in generated code
-                         * comes before the next read of cpu->exit_request
-                         * or cpu->interrupt_request.
-                         */
-                        smp_rmb();
-                        last_tb = NULL;
-                        break;
-                    case TB_EXIT_ICOUNT_EXPIRED:
-                    {
-                        /* Instruction counter expired.  */
-#ifdef CONFIG_USER_ONLY
-                        abort();
-#else
-                        int insns_left = cpu->icount_decr.u32;
-                        if (cpu->icount_extra && insns_left >= 0) {
-                            /* Refill decrementer and continue execution.  */
-                            cpu->icount_extra += insns_left;
-                            insns_left = MIN(0xffff, cpu->icount_extra);
-                            cpu->icount_extra -= insns_left;
-                            cpu->icount_decr.u16.low = insns_left;
-                        } else {
-                            if (insns_left > 0) {
-                                /* Execute remaining instructions.  */
-                                cpu_exec_nocache(cpu, insns_left,
-                                                 last_tb, false);
-                                align_clocks(&sc, cpu);
-                            }
-                            cpu->exception_index = EXCP_INTERRUPT;
-                            last_tb = NULL;
-                            cpu_loop_exit(cpu);
-                        }
-                        break;
-#endif
-                    }
-                    default:
-                        break;
-                    }
-                }
+                cpu_loop_exec_tb(cpu, tb, &last_tb, &tb_exit, &sc);
                 /* Try to align the host and virtual clocks
                    if the guest is in advance */
                 align_clocks(&sc, cpu);
Signature · 1 line
1.9.1
Reviewed-by on v1 4/5
#11 lore raw

Re: [Qemu-devel] [PATCH 4/5] cpu-exec: Move TB execution stuff out of cpu_exec()

Quoted 10 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Simplify cpu_exec() by extracting TB execution code outside of cpu_exec() into a new static inline function cpu_loop_exec_tb().

---
 cpu-exec.c | 119 +++++++++++++++++++++++++++++++++----------------------------
 1 file changed, 64 insertions(+), 55 deletions(-)

r~

#12 lore raw

PATCH 5/5 cpu-exec: Remove unused 'x86_cpu' and 'env' from cpu_exec()

In reply to Sergey Fedorov

From: Sergey Fedorov <[email protected]>

---
 cpu-exec.c | 12 ------------
 1 file changed, 12 deletions(-)
cpu-exec.c +0 -12
index 60f565074618..2526df0e3e40 100644
@@ -571,10 +571,6 @@ static inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb,
 int cpu_exec(CPUState *cpu)
 {
     CPUClass *cc = CPU_GET_CLASS(cpu);
-#ifdef TARGET_I386
-    X86CPU *x86_cpu = X86_CPU(cpu);
-    CPUArchState *env = &x86_cpu->env;
-#endif
     int ret;
     SyncClocks sc;
 
@@ -630,18 +626,10 @@ int cpu_exec(CPUState *cpu)
              * Newer versions of gcc would complain about this code (-Wclobbered). */
             cpu = current_cpu;
             cc = CPU_GET_CLASS(cpu);
-#ifdef TARGET_I386
-            x86_cpu = X86_CPU(cpu);
-            env = &x86_cpu->env;
-#endif
 #else /* buggy compiler */
             /* Assert that the compiler does not smash local variables. */
             g_assert(cpu == current_cpu);
             g_assert(cc == CPU_GET_CLASS(cpu));
-#ifdef TARGET_I386
-            g_assert(x86_cpu == X86_CPU(cpu));
-            g_assert(env == &x86_cpu->env);
-#endif
 #endif /* buggy compiler */
             cpu->can_do_io = 1;
             tb_lock_reset();
Signature · 1 line
1.9.1
#14 lore raw
In reply to Sergey Fedorov
Quoted 22 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

cpu_exec() was a huge function also sprinkled with some preprocessor directives. It was hard to read and see the main loop crowded by all this code. Restructure cpu_exec() by moving its conceptual parts into separate static inline functions. That makes it possible to see the whole main loop at once, especially its sigsetjmp() handling part.

This series is based on commit

    40f646483a11 (cpu-exec: Remove relic orphaned comment)
from
    git://github.com/rth7680/qemu.git tcg-next

and is available at
git://github.com/sergefdrv/qemu.git cpu_exec-restructure

Sergey Fedorov (5):
cpu-exec: Move halt handling out of cpu_exec()
cpu-exec: Move exception handling out of cpu_exec()
cpu-exec: Move interrupt handling out of cpu_exec()
cpu-exec: Move TB execution stuff out of cpu_exec()
cpu-exec: Remove unused 'x86_cpu' and 'env' from cpu_exec()

Quoted 3 lines
 cpu-exec.c | 395 +++++++++++++++++++++++++++++++++----------------------------
 1 file changed, 211 insertions(+), 184 deletions(-)

Sorry, +CC.

Regards,
Sergey