diff mbox series

[PULL,27/47] bsd-user: Add RISC-V CPU execution loop and syscall handling

Message ID 20240912052953.2552501-28-alistair.francis@wdc.com
State New
Headers show
Series [PULL,01/47] target/riscv: Add a property to set vl to ceil(AVL/2) | expand

Commit Message

Alistair Francis Sept. 12, 2024, 5:29 a.m. UTC
From: Mark Corbin <mark@dibsco.co.uk>

Implemented the RISC-V CPU execution loop, including handling various
exceptions and system calls. The loop continuously executes CPU
instructions,processes exceptions, and handles system calls by invoking
FreeBSD syscall handlers.

Signed-off-by: Mark Corbin <mark@dibsco.co.uk>
Signed-off-by: Ajeet Singh <itachis@FreeBSD.org>
Co-authored-by: Jessica Clarke <jrtc27@jrtc27.com>
Co-authored-by: Kyle Evans <kevans@FreeBSD.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-ID: <20240907031927.1908-3-itachis@FreeBSD.org>
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
---
 bsd-user/riscv/target_arch_cpu.h | 94 ++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)

Comments

Daniel Henrique Barboza Sept. 15, 2024, 7:49 p.m. UTC | #1
On 9/12/24 2:29 AM, Alistair Francis wrote:
> From: Mark Corbin <mark@dibsco.co.uk>
> 
> Implemented the RISC-V CPU execution loop, including handling various
> exceptions and system calls. The loop continuously executes CPU
> instructions,processes exceptions, and handles system calls by invoking
> FreeBSD syscall handlers.
> 
> Signed-off-by: Mark Corbin <mark@dibsco.co.uk>
> Signed-off-by: Ajeet Singh <itachis@FreeBSD.org>
> Co-authored-by: Jessica Clarke <jrtc27@jrtc27.com>
> Co-authored-by: Kyle Evans <kevans@FreeBSD.org>
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> Message-ID: <20240907031927.1908-3-itachis@FreeBSD.org>
> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
> ---
>   bsd-user/riscv/target_arch_cpu.h | 94 ++++++++++++++++++++++++++++++++
>   1 file changed, 94 insertions(+)
> 
> diff --git a/bsd-user/riscv/target_arch_cpu.h b/bsd-user/riscv/target_arch_cpu.h
> index e17c910ae9..ea2a8d1ace 100644
> --- a/bsd-user/riscv/target_arch_cpu.h
> +++ b/bsd-user/riscv/target_arch_cpu.h
> @@ -36,4 +36,98 @@ static inline void target_cpu_init(CPURISCVState *env,
>       env->pc = regs->sepc;
>   }
>   

This patch won't build:

In file included from ../bsd-user/main.c:53:
../bsd-user/riscv/target_arch_cpu.h:126:13: error: call to undeclared function 'force_sig_fault'; ISO C99 and later do not support implicit function declarations [-Werror,-Wimplicit-function-declaration]
   126 |             force_sig_fault(signo, code, env->pc);
       |             ^
../bsd-user/riscv/target_arch_cpu.h:129:9: error: call to undeclared function 'process_pending_signals'; ISO C99 and later do not support implicit function declarations [-Werror,-Wimplicit-function-declaration]
   129 |         process_pending_signals(env);
       |         ^

You're missing the following header:


diff --git a/bsd-user/riscv/target_arch_cpu.h b/bsd-user/riscv/target_arch_cpu.h
index 57abfbd556..a93ea3915a 100644
--- a/bsd-user/riscv/target_arch_cpu.h
+++ b/bsd-user/riscv/target_arch_cpu.h
@@ -21,6 +21,7 @@
  #define TARGET_ARCH_CPU_H
  
  #include "target_arch.h"
+#include "signal-common.h"
  
  #define TARGET_DEFAULT_CPU_MODEL "max"





This diff will also fix this other build error:


../bsd-user/main.c:608:5: error: call to undeclared function 'signal_init'; ISO C99 and later do not support implicit function declarations [-Werror,-Wimplicit-function-declaration]
   608 |     signal_init();
       |     ^
3 errors generated.


Because bsd-user/main.c is including target_arch_cpu.h.





Thanks,

Daniel

> +static inline void target_cpu_loop(CPURISCVState *env)
> +{
> +    CPUState *cs = env_cpu(env);
> +    int trapnr;
> +    abi_long ret;
> +    unsigned int syscall_num;
> +    int32_t signo, code;
> +
> +    for (;;) {
> +        cpu_exec_start(cs);
> +        trapnr = cpu_exec(cs);
> +        cpu_exec_end(cs);
> +        process_queued_cpu_work(cs);
> +
> +        signo = 0;
> +
> +        switch (trapnr) {
> +        case EXCP_INTERRUPT:
> +            /* just indicate that signals should be handled asap */
> +            break;
> +        case EXCP_ATOMIC:
> +            cpu_exec_step_atomic(cs);
> +            break;
> +        case RISCV_EXCP_U_ECALL:
> +            syscall_num = env->gpr[xT0];
> +            env->pc += TARGET_INSN_SIZE;
> +            /* Compare to cpu_fetch_syscall_args() in riscv/riscv/trap.c */
> +            if (TARGET_FREEBSD_NR___syscall == syscall_num ||
> +                TARGET_FREEBSD_NR_syscall == syscall_num) {
> +                ret = do_freebsd_syscall(env,
> +                                         env->gpr[xA0],
> +                                         env->gpr[xA1],
> +                                         env->gpr[xA2],
> +                                         env->gpr[xA3],
> +                                         env->gpr[xA4],
> +                                         env->gpr[xA5],
> +                                         env->gpr[xA6],
> +                                         env->gpr[xA7],
> +                                         0);
> +            } else {
> +                ret = do_freebsd_syscall(env,
> +                                         syscall_num,
> +                                         env->gpr[xA0],
> +                                         env->gpr[xA1],
> +                                         env->gpr[xA2],
> +                                         env->gpr[xA3],
> +                                         env->gpr[xA4],
> +                                         env->gpr[xA5],
> +                                         env->gpr[xA6],
> +                                         env->gpr[xA7]
> +                    );
> +            }
> +
> +            /*
> +             * Compare to cpu_set_syscall_retval() in
> +             * riscv/riscv/vm_machdep.c
> +             */
> +            if (ret >= 0) {
> +                env->gpr[xA0] = ret;
> +                env->gpr[xT0] = 0;
> +            } else if (ret == -TARGET_ERESTART) {
> +                env->pc -= TARGET_INSN_SIZE;
> +            } else if (ret != -TARGET_EJUSTRETURN) {
> +                env->gpr[xA0] = -ret;
> +                env->gpr[xT0] = 1;
> +            }
> +            break;
> +        case RISCV_EXCP_ILLEGAL_INST:
> +            signo = TARGET_SIGILL;
> +            code = TARGET_ILL_ILLOPC;
> +            break;
> +        case RISCV_EXCP_BREAKPOINT:
> +            signo = TARGET_SIGTRAP;
> +            code = TARGET_TRAP_BRKPT;
> +            break;
> +        case EXCP_DEBUG:
> +            signo = TARGET_SIGTRAP;
> +            code = TARGET_TRAP_BRKPT;
> +            break;
> +        default:
> +            fprintf(stderr, "qemu: unhandled CPU exception "
> +                "0x%x - aborting\n", trapnr);
> +            cpu_dump_state(cs, stderr, 0);
> +            abort();
> +        }
> +
> +        if (signo) {
> +            force_sig_fault(signo, code, env->pc);
> +        }
> +
> +        process_pending_signals(env);
> +    }
> +}
> +
>   #endif /* TARGET_ARCH_CPU_H */
diff mbox series

Patch

diff --git a/bsd-user/riscv/target_arch_cpu.h b/bsd-user/riscv/target_arch_cpu.h
index e17c910ae9..ea2a8d1ace 100644
--- a/bsd-user/riscv/target_arch_cpu.h
+++ b/bsd-user/riscv/target_arch_cpu.h
@@ -36,4 +36,98 @@  static inline void target_cpu_init(CPURISCVState *env,
     env->pc = regs->sepc;
 }
 
+static inline void target_cpu_loop(CPURISCVState *env)
+{
+    CPUState *cs = env_cpu(env);
+    int trapnr;
+    abi_long ret;
+    unsigned int syscall_num;
+    int32_t signo, code;
+
+    for (;;) {
+        cpu_exec_start(cs);
+        trapnr = cpu_exec(cs);
+        cpu_exec_end(cs);
+        process_queued_cpu_work(cs);
+
+        signo = 0;
+
+        switch (trapnr) {
+        case EXCP_INTERRUPT:
+            /* just indicate that signals should be handled asap */
+            break;
+        case EXCP_ATOMIC:
+            cpu_exec_step_atomic(cs);
+            break;
+        case RISCV_EXCP_U_ECALL:
+            syscall_num = env->gpr[xT0];
+            env->pc += TARGET_INSN_SIZE;
+            /* Compare to cpu_fetch_syscall_args() in riscv/riscv/trap.c */
+            if (TARGET_FREEBSD_NR___syscall == syscall_num ||
+                TARGET_FREEBSD_NR_syscall == syscall_num) {
+                ret = do_freebsd_syscall(env,
+                                         env->gpr[xA0],
+                                         env->gpr[xA1],
+                                         env->gpr[xA2],
+                                         env->gpr[xA3],
+                                         env->gpr[xA4],
+                                         env->gpr[xA5],
+                                         env->gpr[xA6],
+                                         env->gpr[xA7],
+                                         0);
+            } else {
+                ret = do_freebsd_syscall(env,
+                                         syscall_num,
+                                         env->gpr[xA0],
+                                         env->gpr[xA1],
+                                         env->gpr[xA2],
+                                         env->gpr[xA3],
+                                         env->gpr[xA4],
+                                         env->gpr[xA5],
+                                         env->gpr[xA6],
+                                         env->gpr[xA7]
+                    );
+            }
+
+            /*
+             * Compare to cpu_set_syscall_retval() in
+             * riscv/riscv/vm_machdep.c
+             */
+            if (ret >= 0) {
+                env->gpr[xA0] = ret;
+                env->gpr[xT0] = 0;
+            } else if (ret == -TARGET_ERESTART) {
+                env->pc -= TARGET_INSN_SIZE;
+            } else if (ret != -TARGET_EJUSTRETURN) {
+                env->gpr[xA0] = -ret;
+                env->gpr[xT0] = 1;
+            }
+            break;
+        case RISCV_EXCP_ILLEGAL_INST:
+            signo = TARGET_SIGILL;
+            code = TARGET_ILL_ILLOPC;
+            break;
+        case RISCV_EXCP_BREAKPOINT:
+            signo = TARGET_SIGTRAP;
+            code = TARGET_TRAP_BRKPT;
+            break;
+        case EXCP_DEBUG:
+            signo = TARGET_SIGTRAP;
+            code = TARGET_TRAP_BRKPT;
+            break;
+        default:
+            fprintf(stderr, "qemu: unhandled CPU exception "
+                "0x%x - aborting\n", trapnr);
+            cpu_dump_state(cs, stderr, 0);
+            abort();
+        }
+
+        if (signo) {
+            force_sig_fault(signo, code, env->pc);
+        }
+
+        process_pending_signals(env);
+    }
+}
+
 #endif /* TARGET_ARCH_CPU_H */