diff mbox series

[kvmtool,v3] riscv: Use the count parameter of term_putc in SBI_EXT_DBCN_CONSOLE_WRITE

Message ID 20250218230936.408429-1-cyrilbur@tenstorrent.com (mailing list archive)
State New
Headers show
Series [kvmtool,v3] riscv: Use the count parameter of term_putc in SBI_EXT_DBCN_CONSOLE_WRITE | expand

Commit Message

Cyril Bur Feb. 18, 2025, 11:09 p.m. UTC
Currently each character of a string is term_putc()ed individually. This
causes a round trip into opensbi for each char. Very inefficient
especially since the interface term_putc() does accept a count.

This patch passes a count to term_putc() in the
SBI_EXT_DBCN_CONSOLE_WRITE path.

Signed-off-by: Cyril Bur <cyrilbur@tenstorrent.com>
---
 riscv/kvm-cpu.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

Comments

Joel Stanley Feb. 19, 2025, 12:41 a.m. UTC | #1
On Tue, 18 Feb 2025 at 15:09, Cyril Bur <cyrilbur@tenstorrent.com> wrote:
>
> Currently each character of a string is term_putc()ed individually. This
> causes a round trip into opensbi for each char. Very inefficient
> especially since the interface term_putc() does accept a count.
>
> This patch passes a count to term_putc() in the
> SBI_EXT_DBCN_CONSOLE_WRITE path.
>
> Signed-off-by: Cyril Bur <cyrilbur@tenstorrent.com>

Reviewed-by: Joel Stanley <joel@jms.id.au>

I added Will to cc so he can take a look and apply this for you.

> ---
>  riscv/kvm-cpu.c | 22 ++++++++++++----------
>  1 file changed, 12 insertions(+), 10 deletions(-)
>
> diff --git a/riscv/kvm-cpu.c b/riscv/kvm-cpu.c
> index 0c171da..84d35f7 100644
> --- a/riscv/kvm-cpu.c
> +++ b/riscv/kvm-cpu.c
> @@ -172,21 +172,23 @@ static bool kvm_cpu_riscv_sbi(struct kvm_cpu *vcpu)
>                         str_start = guest_flat_to_host(vcpu->kvm, addr);
>                         addr += vcpu->kvm_run->riscv_sbi.args[0] - 1;
>                         str_end = guest_flat_to_host(vcpu->kvm, addr);
> -                       if (!str_start || !str_end) {
> +                       if (!str_start || !str_end || str_start > str_end) {
>                                 vcpu->kvm_run->riscv_sbi.ret[0] =
>                                                 SBI_ERR_INVALID_PARAM;
>                                 break;
>                         }
> +                       if (vcpu->kvm_run->riscv_sbi.function_id ==
> +                           SBI_EXT_DBCN_CONSOLE_WRITE) {
> +                               int length = (str_end - str_start) + 1;
> +
> +                               length = term_putc(str_start, length, 0);
> +                               vcpu->kvm_run->riscv_sbi.ret[1] = length;
> +                               break;
> +                       }
> +                       /* This will be SBI_EXT_DBCN_CONSOLE_READ */
>                         vcpu->kvm_run->riscv_sbi.ret[1] = 0;
> -                       while (str_start <= str_end) {
> -                               if (vcpu->kvm_run->riscv_sbi.function_id ==
> -                                   SBI_EXT_DBCN_CONSOLE_WRITE) {
> -                                       term_putc(str_start, 1, 0);
> -                               } else {
> -                                       if (!term_readable(0))
> -                                               break;
> -                                       *str_start = term_getc(vcpu->kvm, 0);
> -                               }
> +                       while (str_start <= str_end && term_readable(0)) {
> +                               *str_start = term_getc(vcpu->kvm, 0);
>                                 vcpu->kvm_run->riscv_sbi.ret[1]++;
>                                 str_start++;
>                         }
> --
> 2.34.1
>
diff mbox series

Patch

diff --git a/riscv/kvm-cpu.c b/riscv/kvm-cpu.c
index 0c171da..84d35f7 100644
--- a/riscv/kvm-cpu.c
+++ b/riscv/kvm-cpu.c
@@ -172,21 +172,23 @@  static bool kvm_cpu_riscv_sbi(struct kvm_cpu *vcpu)
 			str_start = guest_flat_to_host(vcpu->kvm, addr);
 			addr += vcpu->kvm_run->riscv_sbi.args[0] - 1;
 			str_end = guest_flat_to_host(vcpu->kvm, addr);
-			if (!str_start || !str_end) {
+			if (!str_start || !str_end || str_start > str_end) {
 				vcpu->kvm_run->riscv_sbi.ret[0] =
 						SBI_ERR_INVALID_PARAM;
 				break;
 			}
+			if (vcpu->kvm_run->riscv_sbi.function_id ==
+			    SBI_EXT_DBCN_CONSOLE_WRITE) {
+				int length = (str_end - str_start) + 1;
+
+				length = term_putc(str_start, length, 0);
+				vcpu->kvm_run->riscv_sbi.ret[1] = length;
+				break;
+			}
+			/* This will be SBI_EXT_DBCN_CONSOLE_READ */
 			vcpu->kvm_run->riscv_sbi.ret[1] = 0;
-			while (str_start <= str_end) {
-				if (vcpu->kvm_run->riscv_sbi.function_id ==
-				    SBI_EXT_DBCN_CONSOLE_WRITE) {
-					term_putc(str_start, 1, 0);
-				} else {
-					if (!term_readable(0))
-						break;
-					*str_start = term_getc(vcpu->kvm, 0);
-				}
+			while (str_start <= str_end && term_readable(0)) {
+				*str_start = term_getc(vcpu->kvm, 0);
 				vcpu->kvm_run->riscv_sbi.ret[1]++;
 				str_start++;
 			}