From nobody Mon Feb 9 13:59:04 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519999315383371.71068017453354; Fri, 2 Mar 2018 06:01:55 -0800 (PST) Received: from localhost ([::1]:35166 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1erlFy-0005ze-DZ for importer@patchew.org; Fri, 02 Mar 2018 09:01:54 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58538) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1erl8D-0007Yw-3D for qemu-devel@nongnu.org; Fri, 02 Mar 2018 08:53:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1erl8B-0002Bg-Bb for qemu-devel@nongnu.org; Fri, 02 Mar 2018 08:53:53 -0500 Received: from mail-pf0-x242.google.com ([2607:f8b0:400e:c00::242]:35139) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1erl8B-0002BS-3O for qemu-devel@nongnu.org; Fri, 02 Mar 2018 08:53:51 -0500 Received: by mail-pf0-x242.google.com with SMTP id y186so4034196pfb.2 for ; Fri, 02 Mar 2018 05:53:50 -0800 (PST) Received: from localhost.localdomain (125-237-39-90.jetstream.xtra.co.nz. [125.237.39.90]) by smtp.gmail.com with ESMTPSA id g16sm14046966pfd.23.2018.03.02.05.53.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 02 Mar 2018 05:53:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ocRT9HpP1VHDPUiO/Ae7/1CGyO8su3rYxqX1pgkQHGg=; b=fyqkJSJ5p9mkwKiwa6UK/zBwNOW4kE5HKBb9EJFBgktE+jviXbmPVOOIWjaxZ3MAWW Vys0VJFVgKbbSK74mlQDy3sEmpzxy2iTCqctC+Omg/SJbsz78ex7vGfNsU7u6enXBhVB a3vP+MJgbPPN+5F0vUKFKIDyrmbsUvnSYwWRIGG3gzIPS4CgdwL9e4vA6oAKWi3I6S2j kzD5A/LWS1v++WaeoSmr15Am3uiujtZBMaOTe0aeSah3ZECvsmc3PlOwcQYyF3dG2WBt oZ8PqBj3su8pmw7if9HKeWTq/kATYZAPWjxCZtlyAyEJrru2Y8AjbZPqb+XaKCxNH55V x9eQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ocRT9HpP1VHDPUiO/Ae7/1CGyO8su3rYxqX1pgkQHGg=; b=IXlJWbVNlwCGdaYlfF/h3ImSD6Fuhvb/bqWezYmnTfft3CBQ+cEXXlOm8fSbEq2E7T kydfdT4Fk7Zo7iKq9gm2jzD0NGxrwR6JDXsozvHRhhQYf/2F9n84uZNuEXA0E569D5G5 mPbM23j6OCc4komEkLnIQLfNTZPGCq8bg4M90wYdJUPX6p7B0Z33gd2QwOFBrJ+6+lGR 1Sk6kG3Vb+yGtvnNol3AM/CRxtNavAAPoKw1IObdImeX/kXOihAqMaCJ8JNZ76ZH3HaX n6C/mhu+LLVQWzCFXeFX3ZCf+CRnaDOmdJO1uQAqlGS3y5DKkDOuKqDAICNy5xCdYJZ/ JIJg== X-Gm-Message-State: APf1xPAHJJ1f+aDhBGxzYMzB5BlBdSciMiMgMjljQn7iG1MRfM7Qo0R4 oun4fEWtFCiyCq9paziJRPCRpAmAP6A= X-Google-Smtp-Source: AG47ELslG7vKve+kkTmDKD1VXGM7us7+MQ5kudab6hjcZ5ia3rXRxDDvIeMz40mch0tXOdtvA1co9w== X-Received: by 10.101.78.200 with SMTP id w8mr4656533pgq.43.1519998829868; Fri, 02 Mar 2018 05:53:49 -0800 (PST) From: Michael Clark To: qemu-devel@nongnu.org Date: Sat, 3 Mar 2018 02:51:40 +1300 Message-Id: <1519998711-73430-13-git-send-email-mjc@sifive.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1519998711-73430-1-git-send-email-mjc@sifive.com> References: <1519998711-73430-1-git-send-email-mjc@sifive.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::242 Subject: [Qemu-devel] [PATCH v8 12/23] RISC-V HTIF Console X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stefan O'Rear , Sagar Karandikar , Bastian Koppelmann , Palmer Dabbelt , Michael Clark , RISC-V Patches Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" HTIF (Host Target Interface) provides console emulation for QEMU. HTIF allows identical copies of BBL (Berkeley Boot Loader) and linux to run on both Spike and QEMU. BBL provides HTIF console access via the SBI (Supervisor Binary Interface) and the linux kernel SBI console. The HTIT chardev implements the pre qom legacy interface consistent with the 16550a UART in 'hw/char/serial.c'. Reviewed-by: Richard Henderson Signed-off-by: Sagar Karandikar Signed-off-by: Stefan O'Rear Signed-off-by: Michael Clark Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- hw/riscv/riscv_htif.c | 258 ++++++++++++++++++++++++++++++++++++++= ++++ include/hw/riscv/riscv_htif.h | 61 ++++++++++ 2 files changed, 319 insertions(+) create mode 100644 hw/riscv/riscv_htif.c create mode 100644 include/hw/riscv/riscv_htif.h diff --git a/hw/riscv/riscv_htif.c b/hw/riscv/riscv_htif.c new file mode 100644 index 0000000..3e17f30 --- /dev/null +++ b/hw/riscv/riscv_htif.c @@ -0,0 +1,258 @@ +/* + * QEMU RISC-V Host Target Interface (HTIF) Emulation + * + * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu + * Copyright (c) 2017-2018 SiFive, Inc. + * + * This provides HTIF device emulation for QEMU. At the moment this allows + * for identical copies of bbl/linux to run on both spike and QEMU. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2 or later, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program. If not, see . + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu/log.h" +#include "hw/sysbus.h" +#include "hw/char/serial.h" +#include "chardev/char.h" +#include "chardev/char-fe.h" +#include "hw/riscv/riscv_htif.h" +#include "qemu/timer.h" +#include "exec/address-spaces.h" +#include "qemu/error-report.h" + +#define RISCV_DEBUG_HTIF 0 +#define HTIF_DEBUG(fmt, ...) = \ + do { = \ + if (RISCV_DEBUG_HTIF) { = \ + qemu_log_mask(LOG_TRACE, "%s: " fmt "\n", __func__, ##__VA_ARG= S__);\ + } = \ + } while (0) + +static uint64_t fromhost_addr, tohost_addr; + +void htif_symbol_callback(const char *st_name, int st_info, uint64_t st_va= lue, + uint64_t st_size) +{ + if (strcmp("fromhost", st_name) =3D=3D 0) { + fromhost_addr =3D st_value; + if (st_size !=3D 8) { + error_report("HTIF fromhost must be 8 bytes"); + exit(1); + } + } else if (strcmp("tohost", st_name) =3D=3D 0) { + tohost_addr =3D st_value; + if (st_size !=3D 8) { + error_report("HTIF tohost must be 8 bytes"); + exit(1); + } + } +} + +/* + * Called by the char dev to see if HTIF is ready to accept input. + */ +static int htif_can_recv(void *opaque) +{ + return 1; +} + +/* + * Called by the char dev to supply input to HTIF console. + * We assume that we will receive one character at a time. + */ +static void htif_recv(void *opaque, const uint8_t *buf, int size) +{ + HTIFState *htifstate =3D opaque; + + if (size !=3D 1) { + return; + } + + /* TODO - we need to check whether mfromhost is zero which indicates + the device is ready to receive. The current implementation + will drop characters */ + + uint64_t val_written =3D htifstate->pending_read; + uint64_t resp =3D 0x100 | *buf; + + htifstate->env->mfromhost =3D (val_written >> 48 << 48) | (resp << 16 = >> 16); +} + +/* + * Called by the char dev to supply special events to the HTIF console. + * Not used for HTIF. + */ +static void htif_event(void *opaque, int event) +{ + +} + +static int htif_be_change(void *opaque) +{ + HTIFState *s =3D opaque; + + qemu_chr_fe_set_handlers(&s->chr, htif_can_recv, htif_recv, htif_event, + htif_be_change, s, NULL, true); + + return 0; +} + +static void htif_handle_tohost_write(HTIFState *htifstate, uint64_t val_wr= itten) +{ + uint8_t device =3D val_written >> 56; + uint8_t cmd =3D val_written >> 48; + uint64_t payload =3D val_written & 0xFFFFFFFFFFFFULL; + int resp =3D 0; + + HTIF_DEBUG("mtohost write: device: %d cmd: %d what: %02" PRIx64 + " -payload: %016" PRIx64 "\n", device, cmd, payload & 0xFF, payloa= d); + + /* + * Currently, there is a fixed mapping of devices: + * 0: riscv-tests Pass/Fail Reporting Only (no syscall proxy) + * 1: Console + */ + if (unlikely(device =3D=3D 0x0)) { + /* frontend syscall handler, shutdown and exit code support */ + if (cmd =3D=3D 0x0) { + if (payload & 0x1) { + /* exit code */ + int exit_code =3D payload >> 1; + exit(exit_code); + } else { + qemu_log_mask(LOG_UNIMP, "pk syscall proxy not supported\n= "); + } + } else { + qemu_log("HTIF device %d: unknown command\n", device); + } + } else if (likely(device =3D=3D 0x1)) { + /* HTIF Console */ + if (cmd =3D=3D 0x0) { + /* this should be a queue, but not yet implemented as such */ + htifstate->pending_read =3D val_written; + htifstate->env->mtohost =3D 0; /* clear to indicate we read */ + return; + } else if (cmd =3D=3D 0x1) { + qemu_chr_fe_write(&htifstate->chr, (uint8_t *)&payload, 1); + resp =3D 0x100 | (uint8_t)payload; + } else { + qemu_log("HTIF device %d: unknown command\n", device); + } + } else { + qemu_log("HTIF unknown device or command\n"); + HTIF_DEBUG("device: %d cmd: %d what: %02" PRIx64 + " payload: %016" PRIx64, device, cmd, payload & 0xFF, payload); + } + /* + * - latest bbl does not set fromhost to 0 if there is a value in toho= st + * - with this code enabled, qemu hangs waiting for fromhost to go to 0 + * - with this code disabled, qemu works with bbl priv v1.9.1 and v1.10 + * - HTIF needs protocol documentation and a more complete state machi= ne + + while (!htifstate->fromhost_inprogress && + htifstate->env->mfromhost !=3D 0x0) { + } + */ + htifstate->env->mfromhost =3D (val_written >> 48 << 48) | (resp << 16 = >> 16); + htifstate->env->mtohost =3D 0; /* clear to indicate we read */ +} + +#define TOHOST_OFFSET1 (htifstate->tohost_offset) +#define TOHOST_OFFSET2 (htifstate->tohost_offset + 4) +#define FROMHOST_OFFSET1 (htifstate->fromhost_offset) +#define FROMHOST_OFFSET2 (htifstate->fromhost_offset + 4) + +/* CPU wants to read an HTIF register */ +static uint64_t htif_mm_read(void *opaque, hwaddr addr, unsigned size) +{ + HTIFState *htifstate =3D opaque; + if (addr =3D=3D TOHOST_OFFSET1) { + return htifstate->env->mtohost & 0xFFFFFFFF; + } else if (addr =3D=3D TOHOST_OFFSET2) { + return (htifstate->env->mtohost >> 32) & 0xFFFFFFFF; + } else if (addr =3D=3D FROMHOST_OFFSET1) { + return htifstate->env->mfromhost & 0xFFFFFFFF; + } else if (addr =3D=3D FROMHOST_OFFSET2) { + return (htifstate->env->mfromhost >> 32) & 0xFFFFFFFF; + } else { + qemu_log("Invalid htif read: address %016" PRIx64 "\n", + (uint64_t)addr); + return 0; + } +} + +/* CPU wrote to an HTIF register */ +static void htif_mm_write(void *opaque, hwaddr addr, + uint64_t value, unsigned size) +{ + HTIFState *htifstate =3D opaque; + if (addr =3D=3D TOHOST_OFFSET1) { + if (htifstate->env->mtohost =3D=3D 0x0) { + htifstate->allow_tohost =3D 1; + htifstate->env->mtohost =3D value & 0xFFFFFFFF; + } else { + htifstate->allow_tohost =3D 0; + } + } else if (addr =3D=3D TOHOST_OFFSET2) { + if (htifstate->allow_tohost) { + htifstate->env->mtohost |=3D value << 32; + htif_handle_tohost_write(htifstate, htifstate->env->mtohost); + } + } else if (addr =3D=3D FROMHOST_OFFSET1) { + htifstate->fromhost_inprogress =3D 1; + htifstate->env->mfromhost =3D value & 0xFFFFFFFF; + } else if (addr =3D=3D FROMHOST_OFFSET2) { + htifstate->env->mfromhost |=3D value << 32; + htifstate->fromhost_inprogress =3D 0; + } else { + qemu_log("Invalid htif write: address %016" PRIx64 "\n", + (uint64_t)addr); + } +} + +static const MemoryRegionOps htif_mm_ops =3D { + .read =3D htif_mm_read, + .write =3D htif_mm_write, +}; + +HTIFState *htif_mm_init(MemoryRegion *address_space, MemoryRegion *main_me= m, + CPURISCVState *env, Chardev *chr) +{ + uint64_t base =3D MIN(tohost_addr, fromhost_addr); + uint64_t size =3D MAX(tohost_addr + 8, fromhost_addr + 8) - base; + uint64_t tohost_offset =3D tohost_addr - base; + uint64_t fromhost_offset =3D fromhost_addr - base; + + HTIFState *s =3D g_malloc0(sizeof(HTIFState)); + s->address_space =3D address_space; + s->main_mem =3D main_mem; + s->main_mem_ram_ptr =3D memory_region_get_ram_ptr(main_mem); + s->env =3D env; + s->tohost_offset =3D tohost_offset; + s->fromhost_offset =3D fromhost_offset; + s->pending_read =3D 0; + s->allow_tohost =3D 0; + s->fromhost_inprogress =3D 0; + qemu_chr_fe_init(&s->chr, chr, &error_abort); + qemu_chr_fe_set_handlers(&s->chr, htif_can_recv, htif_recv, htif_event, + htif_be_change, s, NULL, true); + if (base) { + memory_region_init_io(&s->mmio, NULL, &htif_mm_ops, s, + TYPE_HTIF_UART, size); + memory_region_add_subregion(address_space, base, &s->mmio); + } + + return s; +} diff --git a/include/hw/riscv/riscv_htif.h b/include/hw/riscv/riscv_htif.h new file mode 100644 index 0000000..fb5f881 --- /dev/null +++ b/include/hw/riscv/riscv_htif.h @@ -0,0 +1,61 @@ +/* + * QEMU RISCV Host Target Interface (HTIF) Emulation + * + * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu + * Copyright (c) 2017-2018 SiFive, Inc. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2 or later, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program. If not, see . + */ + +#ifndef HW_RISCV_HTIF_H +#define HW_RISCV_HTIF_H + +#include "hw/hw.h" +#include "chardev/char.h" +#include "chardev/char-fe.h" +#include "sysemu/sysemu.h" +#include "exec/memory.h" +#include "target/riscv/cpu.h" + +#define TYPE_HTIF_UART "riscv.htif.uart" + +typedef struct HTIFState { + int allow_tohost; + int fromhost_inprogress; + + hwaddr tohost_offset; + hwaddr fromhost_offset; + uint64_t tohost_size; + uint64_t fromhost_size; + MemoryRegion mmio; + MemoryRegion *address_space; + MemoryRegion *main_mem; + void *main_mem_ram_ptr; + + CPURISCVState *env; + CharBackend chr; + uint64_t pending_read; +} HTIFState; + +extern const VMStateDescription vmstate_htif; +extern const MemoryRegionOps htif_io_ops; + +/* HTIF symbol callback */ +void htif_symbol_callback(const char *st_name, int st_info, uint64_t st_va= lue, + uint64_t st_size); + +/* legacy pre qom */ +HTIFState *htif_mm_init(MemoryRegion *address_space, MemoryRegion *main_me= m, + CPURISCVState *env, Chardev *chr); + +#endif --=20 2.7.0