qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: imp@bsdimp.com
To: qemu-devel@nongnu.org
Cc: kevans@freebsd.org, arichardson@freebsd.org,
	Warner Losh <imp@bsdimp.com>, Stacey Son <sson@FreeBSD.org>
Subject: [PATCH v2 25/48] bsd-user: move x86 (i386 and x86_64) cpu_loop to target_arch_cpu.h
Date: Sat, 24 Apr 2021 09:59:53 -0600	[thread overview]
Message-ID: <20210424160016.15200-26-imp@bsdimp.com> (raw)
In-Reply-To: <20210424160016.15200-1-imp@bsdimp.com>

From: Warner Losh <imp@bsdimp.com>

Move the x86 version of the cpu_loop to target_arch_cpu.h as
target_cpu_loop. Create a cpu_loop that calls the target_cpu_loop function, but
only for x86 for now. This is code-movement only commit.

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/i386/target_arch_cpu.h | 177 +++++++++++++++++++++++++++++++-
 bsd-user/main.c                 | 176 +------------------------------
 2 files changed, 179 insertions(+), 174 deletions(-)

diff --git a/bsd-user/i386/target_arch_cpu.h b/bsd-user/i386/target_arch_cpu.h
index c05e048b9b..e8b306c832 100644
--- a/bsd-user/i386/target_arch_cpu.h
+++ b/bsd-user/i386/target_arch_cpu.h
@@ -19,4 +19,179 @@
 #ifndef _TARGET_ARCH_CPU_H_
 #define _TARGET_ARCH_CPU_H_
 
-#endif /* ! _TARGET_ARCH_CPU_H_ */
+/***********************************************************/
+/* CPUX86 core interface */
+
+uint64_t cpu_get_tsc(CPUX86State *env)
+{
+    return cpu_get_host_ticks();
+}
+
+static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
+                     int flags)
+{
+    unsigned int e1, e2;
+    uint32_t *p;
+    e1 = (addr << 16) | (limit & 0xffff);
+    e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
+    e2 |= flags;
+    p = ptr;
+    p[0] = tswap32(e1);
+    p[1] = tswap32(e2);
+}
+
+static uint64_t *idt_table;
+#ifdef TARGET_X86_64
+static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
+                       uint64_t addr, unsigned int sel)
+{
+    uint32_t *p, e1, e2;
+    e1 = (addr & 0xffff) | (sel << 16);
+    e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
+    p = ptr;
+    p[0] = tswap32(e1);
+    p[1] = tswap32(e2);
+    p[2] = tswap32(addr >> 32);
+    p[3] = 0;
+}
+/* only dpl matters as we do only user space emulation */
+static void set_idt(int n, unsigned int dpl)
+{
+    set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
+}
+#else
+static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
+                     uint32_t addr, unsigned int sel)
+{
+    uint32_t *p, e1, e2;
+    e1 = (addr & 0xffff) | (sel << 16);
+    e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
+    p = ptr;
+    p[0] = tswap32(e1);
+    p[1] = tswap32(e2);
+}
+
+/* only dpl matters as we do only user space emulation */
+static void set_idt(int n, unsigned int dpl)
+{
+    set_gate(idt_table + n, 0, dpl, 0, 0);
+}
+#endif
+
+static void target_cpu_loop(CPUArchState *env)
+{
+    CPUState *cs = env_cpu(env);
+    int trapnr;
+    abi_ulong pc;
+    /* target_siginfo_t info; */
+
+    for (;;) {
+        cpu_exec_start(cs);
+        trapnr = cpu_exec(cs);
+        cpu_exec_end(cs);
+        process_queued_cpu_work(cs);
+
+        switch (trapnr) {
+        case 0x80:
+            /* syscall from int $0x80 */
+            if (bsd_type == target_freebsd) {
+                abi_ulong params = (abi_ulong) env->regs[R_ESP] +
+                    sizeof(int32_t);
+                int32_t syscall_nr = env->regs[R_EAX];
+                int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
+
+                if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
+                    get_user_s32(syscall_nr, params);
+                    params += sizeof(int32_t);
+                } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
+                    get_user_s32(syscall_nr, params);
+                    params += sizeof(int64_t);
+                }
+                get_user_s32(arg1, params);
+                params += sizeof(int32_t);
+                get_user_s32(arg2, params);
+                params += sizeof(int32_t);
+                get_user_s32(arg3, params);
+                params += sizeof(int32_t);
+                get_user_s32(arg4, params);
+                params += sizeof(int32_t);
+                get_user_s32(arg5, params);
+                params += sizeof(int32_t);
+                get_user_s32(arg6, params);
+                params += sizeof(int32_t);
+                get_user_s32(arg7, params);
+                params += sizeof(int32_t);
+                get_user_s32(arg8, params);
+                env->regs[R_EAX] = do_freebsd_syscall(env,
+                                                      syscall_nr,
+                                                      arg1,
+                                                      arg2,
+                                                      arg3,
+                                                      arg4,
+                                                      arg5,
+                                                      arg6,
+                                                      arg7,
+                                                      arg8);
+            } else { /* if (bsd_type == target_openbsd) */
+                env->regs[R_EAX] = do_openbsd_syscall(env,
+                                                      env->regs[R_EAX],
+                                                      env->regs[R_EBX],
+                                                      env->regs[R_ECX],
+                                                      env->regs[R_EDX],
+                                                      env->regs[R_ESI],
+                                                      env->regs[R_EDI],
+                                                      env->regs[R_EBP]);
+            }
+            if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
+                env->regs[R_EAX] = -env->regs[R_EAX];
+                env->eflags |= CC_C;
+            } else {
+                env->eflags &= ~CC_C;
+            }
+            break;
+#ifndef TARGET_ABI32
+        case EXCP_SYSCALL:
+            /* syscall from syscall instruction */
+            if (bsd_type == target_freebsd) {
+                env->regs[R_EAX] = do_freebsd_syscall(env,
+                                                      env->regs[R_EAX],
+                                                      env->regs[R_EDI],
+                                                      env->regs[R_ESI],
+                                                      env->regs[R_EDX],
+                                                      env->regs[R_ECX],
+                                                      env->regs[8],
+                                                      env->regs[9], 0, 0);
+            } else { /* if (bsd_type == target_openbsd) */
+                env->regs[R_EAX] = do_openbsd_syscall(env,
+                                                      env->regs[R_EAX],
+                                                      env->regs[R_EDI],
+                                                      env->regs[R_ESI],
+                                                      env->regs[R_EDX],
+                                                      env->regs[10],
+                                                      env->regs[8],
+                                                      env->regs[9]);
+            }
+            env->eip = env->exception_next_eip;
+            if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
+                env->regs[R_EAX] = -env->regs[R_EAX];
+                env->eflags |= CC_C;
+            } else {
+                env->eflags &= ~CC_C;
+            }
+            break;
+#endif
+        case EXCP_INTERRUPT:
+            /* just indicate that signals should be handled asap */
+            break;
+        default:
+            pc = env->segs[R_CS].base + env->eip;
+            fprintf(stderr,
+                    "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
+                    (long)pc, trapnr);
+            abort();
+        }
+        process_pending_signals(env);
+    }
+}
+
+#endif /* _TARGET_ARCH_CPU_H_ */
diff --git a/bsd-user/main.c b/bsd-user/main.c
index 8a270ccfe6..43c578c760 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -88,180 +88,10 @@ void fork_end(int child)
     }
 }
 
-#ifdef TARGET_I386
-/***********************************************************/
-/* CPUX86 core interface */
-
-uint64_t cpu_get_tsc(CPUX86State *env)
-{
-    return cpu_get_host_ticks();
-}
-
-static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
-                     int flags)
-{
-    unsigned int e1, e2;
-    uint32_t *p;
-    e1 = (addr << 16) | (limit & 0xffff);
-    e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
-    e2 |= flags;
-    p = ptr;
-    p[0] = tswap32(e1);
-    p[1] = tswap32(e2);
-}
-
-static uint64_t *idt_table;
-#ifdef TARGET_X86_64
-static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
-                       uint64_t addr, unsigned int sel)
-{
-    uint32_t *p, e1, e2;
-    e1 = (addr & 0xffff) | (sel << 16);
-    e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
-    p = ptr;
-    p[0] = tswap32(e1);
-    p[1] = tswap32(e2);
-    p[2] = tswap32(addr >> 32);
-    p[3] = 0;
-}
-/* only dpl matters as we do only user space emulation */
-static void set_idt(int n, unsigned int dpl)
-{
-    set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
-}
-#else
-static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
-                     uint32_t addr, unsigned int sel)
-{
-    uint32_t *p, e1, e2;
-    e1 = (addr & 0xffff) | (sel << 16);
-    e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
-    p = ptr;
-    p[0] = tswap32(e1);
-    p[1] = tswap32(e2);
-}
-
-/* only dpl matters as we do only user space emulation */
-static void set_idt(int n, unsigned int dpl)
-{
-    set_gate(idt_table + n, 0, dpl, 0, 0);
-}
-#endif
-
-void cpu_loop(CPUX86State *env)
+#ifdef TARGET_I386 /* stopgap ifdef */
+void cpu_loop(CPUArchState *env)
 {
-    CPUState *cs = env_cpu(env);
-    int trapnr;
-    abi_ulong pc;
-    /* target_siginfo_t info; */
-
-    for (;;) {
-        cpu_exec_start(cs);
-        trapnr = cpu_exec(cs);
-        cpu_exec_end(cs);
-        process_queued_cpu_work(cs);
-
-        switch (trapnr) {
-        case 0x80:
-            /* syscall from int $0x80 */
-            if (bsd_type == target_freebsd) {
-                abi_ulong params = (abi_ulong) env->regs[R_ESP] +
-                    sizeof(int32_t);
-                int32_t syscall_nr = env->regs[R_EAX];
-                int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
-
-                if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
-                    get_user_s32(syscall_nr, params);
-                    params += sizeof(int32_t);
-                } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
-                    get_user_s32(syscall_nr, params);
-                    params += sizeof(int64_t);
-                }
-                get_user_s32(arg1, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg2, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg3, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg4, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg5, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg6, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg7, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg8, params);
-                env->regs[R_EAX] = do_freebsd_syscall(env,
-                                                      syscall_nr,
-                                                      arg1,
-                                                      arg2,
-                                                      arg3,
-                                                      arg4,
-                                                      arg5,
-                                                      arg6,
-                                                      arg7,
-                                                      arg8);
-            } else { /* if (bsd_type == target_openbsd) */
-                env->regs[R_EAX] = do_openbsd_syscall(env,
-                                                      env->regs[R_EAX],
-                                                      env->regs[R_EBX],
-                                                      env->regs[R_ECX],
-                                                      env->regs[R_EDX],
-                                                      env->regs[R_ESI],
-                                                      env->regs[R_EDI],
-                                                      env->regs[R_EBP]);
-            }
-            if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
-                env->regs[R_EAX] = -env->regs[R_EAX];
-                env->eflags |= CC_C;
-            } else {
-                env->eflags &= ~CC_C;
-            }
-            break;
-#ifndef TARGET_ABI32
-        case EXCP_SYSCALL:
-            /* syscall from syscall instruction */
-            if (bsd_type == target_freebsd) {
-                env->regs[R_EAX] = do_freebsd_syscall(env,
-                                                      env->regs[R_EAX],
-                                                      env->regs[R_EDI],
-                                                      env->regs[R_ESI],
-                                                      env->regs[R_EDX],
-                                                      env->regs[R_ECX],
-                                                      env->regs[8],
-                                                      env->regs[9], 0, 0);
-            } else { /* if (bsd_type == target_openbsd) */
-                env->regs[R_EAX] = do_openbsd_syscall(env,
-                                                      env->regs[R_EAX],
-                                                      env->regs[R_EDI],
-                                                      env->regs[R_ESI],
-                                                      env->regs[R_EDX],
-                                                      env->regs[10],
-                                                      env->regs[8],
-                                                      env->regs[9]);
-            }
-            env->eip = env->exception_next_eip;
-            if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
-                env->regs[R_EAX] = -env->regs[R_EAX];
-                env->eflags |= CC_C;
-            } else {
-                env->eflags &= ~CC_C;
-            }
-            break;
-#endif
-        case EXCP_INTERRUPT:
-            /* just indicate that signals should be handled asap */
-            break;
-        default:
-            pc = env->segs[R_CS].base + env->eip;
-            fprintf(stderr,
-                    "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
-                    (long)pc, trapnr);
-            abort();
-        }
-        process_pending_signals(env);
-    }
+    target_cpu_loop(env);
 }
 #endif
 
-- 
2.22.1



  parent reply	other threads:[~2021-04-24 16:34 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-24 15:59 [PATCH v2 00/48] bsd-user style and reorg patches imp
2021-04-24 15:59 ` [PATCH v2 01/48] bsd-user: whitespace changes imp
2021-04-24 15:59 ` [PATCH v2 02/48] " imp
2021-04-24 15:59 ` [PATCH v2 03/48] " imp
2021-04-24 15:59 ` [PATCH v2 04/48] bsd-user: style tweak: keyword space ( imp
2021-04-24 15:59 ` [PATCH v2 05/48] " imp
2021-04-24 15:59 ` [PATCH v2 06/48] " imp
2021-04-24 15:59 ` [PATCH v2 07/48] " imp
2021-04-24 15:59 ` [PATCH v2 08/48] bsd-user: style tweak: use C not C++ comments imp
2021-04-24 15:59 ` [PATCH v2 09/48] " imp
2021-04-24 15:59 ` [PATCH v2 10/48] bsd-user: Remove commented out code imp
2021-04-24 17:19   ` Richard Henderson
2021-04-24 15:59 ` [PATCH v2 11/48] bsd-user: style tweak: Remove #if 0'd code imp
2021-04-24 17:21   ` Richard Henderson
2021-04-24 15:59 ` [PATCH v2 12/48] " imp
2021-04-24 17:23   ` Richard Henderson
2021-04-24 15:59 ` [PATCH v2 13/48] " imp
2021-04-24 17:23   ` Richard Henderson
2021-04-24 15:59 ` [PATCH v2 14/48] " imp
2021-04-24 17:24   ` Richard Henderson
2021-04-24 15:59 ` [PATCH v2 15/48] bsd-user: style tweak: return is not a function, eliminate () imp
2021-04-24 17:24   ` Richard Henderson
2021-04-24 15:59 ` [PATCH v2 16/48] bsd-user: style tweak: Put {} around all if/else/for statements imp
2021-04-24 17:25   ` Richard Henderson
2021-04-24 15:59 ` [PATCH v2 17/48] bsd-user: style tweak: Fix commentary issues imp
2021-04-24 15:59 ` [PATCH v2 18/48] bsd-user: style tweak: Use preferred block comments imp
2021-04-24 15:59 ` [PATCH v2 19/48] bsd-user: style tweak: move extern to header file imp
2021-04-24 15:59 ` [PATCH v2 20/48] bsd-user: style tweak: use {} consistently in for / if / else statements imp
2021-04-24 15:59 ` [PATCH v2 21/48] bsd-user: style nits: return is not a function imp
2021-04-24 17:27   ` Richard Henderson
2021-04-24 15:59 ` [PATCH v2 22/48] bsd-user: use qemu_strtoul in preference to strtol imp
2021-04-24 17:34   ` Richard Henderson
2021-04-24 15:59 ` [PATCH v2 23/48] bsd-user: introduce host_os.h for bsd-specific code and defaults imp
2021-04-24 15:59 ` [PATCH v2 24/48] bsd-user: create target_arch_cpu.h imp
2021-04-24 15:59 ` imp [this message]
2021-04-24 17:43   ` [PATCH v2 25/48] bsd-user: move x86 (i386 and x86_64) cpu_loop to target_arch_cpu.h Richard Henderson
2021-04-24 15:59 ` [PATCH v2 26/48] bsd-user: move sparc cpu_loop into target_arch_cpu.h as target_cpu_loop imp
2021-04-24 17:45   ` Richard Henderson
2021-04-24 15:59 ` [PATCH v2 27/48] bsd-user: style tweak: space pedantry imp
2021-04-24 17:46   ` Richard Henderson
2021-04-24 15:59 ` [PATCH v2 28/48] bsd-user: style tweak: comments imp
2021-04-24 17:51   ` Richard Henderson
2021-04-24 15:59 ` [PATCH v2 29/48] bsd-user: style tweak: use {} correctly imp
2021-04-24 17:55   ` Richard Henderson
2021-04-24 15:59 ` [PATCH v2 30/48] bsd-user: style tweak: fix block comments imp
2021-04-24 17:59   ` Richard Henderson
2021-04-24 15:59 ` [PATCH v2 31/48] bsd-user: style tweak: use {} for all if statements, format else correctly imp
2021-04-24 18:01   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 32/48] bsd-user: style tweak: remove spacing after '*' and add after } imp
2021-04-24 18:03   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 33/48] bsd-user: style tweak: Use preferred block comments imp
2021-04-24 18:07   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 34/48] bsd-user: style tweak: don't assign in if statements imp
2021-04-24 18:09   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 35/48] bsd-user: style tweak: use {} for all if statements, format else correctly imp
2021-04-24 18:12   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 36/48] bsd-user: style tweak: Use preferred block comments imp
2021-04-24 18:13   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 37/48] bsd-user: style tweak: don't assign in if statements imp
2021-04-24 18:14   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 38/48] bsd-user: style tweak: use {} for all if statements, format else correctly imp
2021-04-24 18:15   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 39/48] bsd-user: style tweak: spaces around =, remove stray space imp
2021-04-24 18:16   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 40/48] bsd-user: style tweak: Use preferred block comments imp
2021-04-24 18:16   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 41/48] bsd-user: style tweak: don't assign in if statements imp
2021-04-24 18:17   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 42/48] bsd-user: style tweak: spaces around operators and commas imp
2021-04-24 18:18   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 43/48] bsd-user: style tweak: fold long lines imp
2021-04-24 18:18   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 44/48] bsd-user: style tweak: use preferred block comments imp
2021-04-24 18:19   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 45/48] bsd-user: style tweak: Use preferred {} in if/else statements imp
2021-04-24 18:21   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 46/48] bsd-user: style tweak: Return is not a function call imp
2021-04-24 18:21   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 47/48] bsd-user: style tweak: don't assign in if statement imp
2021-04-24 18:21   ` Richard Henderson
2021-04-24 16:00 ` [PATCH v2 48/48] bsd-user: put back a break; that had gone missing imp
2021-04-24 18:22   ` Richard Henderson
2021-04-24 16:55 ` [PATCH v2 00/48] bsd-user style and reorg patches no-reply
2021-04-24 17:00   ` Warner Losh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210424160016.15200-26-imp@bsdimp.com \
    --to=imp@bsdimp.com \
    --cc=arichardson@freebsd.org \
    --cc=kevans@freebsd.org \
    --cc=qemu-devel@nongnu.org \
    --cc=sson@FreeBSD.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).