2018-03-02 15:31:12 +03:00
|
|
|
/*
|
|
|
|
* 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 for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qapi/error.h"
|
|
|
|
#include "qemu/log.h"
|
2020-09-03 13:40:18 +03:00
|
|
|
#include "hw/char/riscv_htif.h"
|
2018-03-02 15:31:12 +03:00
|
|
|
#include "hw/char/serial.h"
|
|
|
|
#include "chardev/char.h"
|
|
|
|
#include "chardev/char-fe.h"
|
|
|
|
#include "qemu/timer.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_ARGS__);\
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2022-12-29 12:18:17 +03:00
|
|
|
#define HTIF_DEV_SHIFT 56
|
|
|
|
#define HTIF_CMD_SHIFT 48
|
|
|
|
|
|
|
|
#define HTIF_DEV_SYSTEM 0
|
|
|
|
#define HTIF_DEV_CONSOLE 1
|
|
|
|
|
|
|
|
#define HTIF_SYSTEM_CMD_SYSCALL 0
|
|
|
|
#define HTIF_CONSOLE_CMD_GETC 0
|
|
|
|
#define HTIF_CONSOLE_CMD_PUTC 1
|
|
|
|
|
2022-12-29 12:18:23 +03:00
|
|
|
/* PK system call number */
|
|
|
|
#define PK_SYS_WRITE 64
|
|
|
|
|
2018-03-02 15:31:12 +03:00
|
|
|
static uint64_t fromhost_addr, tohost_addr;
|
|
|
|
|
|
|
|
void htif_symbol_callback(const char *st_name, int st_info, uint64_t st_value,
|
2018-05-03 18:17:14 +03:00
|
|
|
uint64_t st_size)
|
2018-03-02 15:31:12 +03:00
|
|
|
{
|
|
|
|
if (strcmp("fromhost", st_name) == 0) {
|
|
|
|
fromhost_addr = st_value;
|
|
|
|
if (st_size != 8) {
|
|
|
|
error_report("HTIF fromhost must be 8 bytes");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else if (strcmp("tohost", st_name) == 0) {
|
|
|
|
tohost_addr = st_value;
|
|
|
|
if (st_size != 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)
|
|
|
|
{
|
2022-12-29 12:18:20 +03:00
|
|
|
HTIFState *s = opaque;
|
2018-03-02 15:31:12 +03:00
|
|
|
|
|
|
|
if (size != 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-29 12:18:17 +03:00
|
|
|
/*
|
|
|
|
* TODO - we need to check whether mfromhost is zero which indicates
|
|
|
|
* the device is ready to receive. The current implementation
|
|
|
|
* will drop characters
|
|
|
|
*/
|
2018-03-02 15:31:12 +03:00
|
|
|
|
2022-12-29 12:18:20 +03:00
|
|
|
uint64_t val_written = s->pending_read;
|
2018-03-02 15:31:12 +03:00
|
|
|
uint64_t resp = 0x100 | *buf;
|
|
|
|
|
2022-12-29 12:18:21 +03:00
|
|
|
s->fromhost = (val_written >> 48 << 48) | (resp << 16 >> 16);
|
2018-03-02 15:31:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the char dev to supply special events to the HTIF console.
|
|
|
|
* Not used for HTIF.
|
|
|
|
*/
|
chardev: Use QEMUChrEvent enum in IOEventHandler typedef
The Chardev events are listed in the QEMUChrEvent enum.
By using the enum in the IOEventHandler typedef we:
- make the IOEventHandler type more explicit (this handler
process out-of-band information, while the IOReadHandler
is in-band),
- help static code analyzers.
This patch was produced with the following spatch script:
@match@
expression backend, opaque, context, set_open;
identifier fd_can_read, fd_read, fd_event, be_change;
@@
qemu_chr_fe_set_handlers(backend, fd_can_read, fd_read, fd_event,
be_change, opaque, context, set_open);
@depends on match@
identifier opaque, event;
identifier match.fd_event;
@@
static
-void fd_event(void *opaque, int event)
+void fd_event(void *opaque, QEMUChrEvent event)
{
...
}
Then the typedef was modified manually in
include/chardev/char-fe.h.
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Acked-by: Corey Minyard <cminyard@mvista.com>
Acked-by: Cornelia Huck <cohuck@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20191218172009.8868-15-philmd@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2019-12-18 20:20:09 +03:00
|
|
|
static void htif_event(void *opaque, QEMUChrEvent event)
|
2018-03-02 15:31:12 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int htif_be_change(void *opaque)
|
|
|
|
{
|
|
|
|
HTIFState *s = opaque;
|
|
|
|
|
|
|
|
qemu_chr_fe_set_handlers(&s->chr, htif_can_recv, htif_recv, htif_event,
|
|
|
|
htif_be_change, s, NULL, true);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-29 12:18:17 +03:00
|
|
|
/*
|
|
|
|
* See below the tohost register format.
|
|
|
|
*
|
|
|
|
* Bits 63:56 indicate the "device".
|
|
|
|
* Bits 55:48 indicate the "command".
|
|
|
|
*
|
|
|
|
* Device 0 is the syscall device, which is used to emulate Unixy syscalls.
|
|
|
|
* It only implements command 0, which has two subfunctions:
|
|
|
|
* - If bit 0 is clear, then bits 47:0 represent a pointer to a struct
|
|
|
|
* describing the syscall.
|
|
|
|
* - If bit 1 is set, then bits 47:1 represent an exit code, with a zero
|
|
|
|
* value indicating success and other values indicating failure.
|
|
|
|
*
|
|
|
|
* Device 1 is the blocking character device.
|
|
|
|
* - Command 0 reads a character
|
|
|
|
* - Command 1 writes a character from the 8 LSBs of tohost
|
|
|
|
*
|
|
|
|
* For RV32, the tohost register is zero-extended, so only device=0 and
|
|
|
|
* command=0 (i.e. HTIF syscalls/exit codes) are supported.
|
|
|
|
*/
|
2022-12-29 12:18:20 +03:00
|
|
|
static void htif_handle_tohost_write(HTIFState *s, uint64_t val_written)
|
2018-03-02 15:31:12 +03:00
|
|
|
{
|
2022-12-29 12:18:17 +03:00
|
|
|
uint8_t device = val_written >> HTIF_DEV_SHIFT;
|
|
|
|
uint8_t cmd = val_written >> HTIF_CMD_SHIFT;
|
2018-03-02 15:31:12 +03:00
|
|
|
uint64_t payload = val_written & 0xFFFFFFFFFFFFULL;
|
|
|
|
int resp = 0;
|
|
|
|
|
|
|
|
HTIF_DEBUG("mtohost write: device: %d cmd: %d what: %02" PRIx64
|
|
|
|
" -payload: %016" PRIx64 "\n", device, cmd, payload & 0xFF, payload);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Currently, there is a fixed mapping of devices:
|
|
|
|
* 0: riscv-tests Pass/Fail Reporting Only (no syscall proxy)
|
|
|
|
* 1: Console
|
|
|
|
*/
|
2022-12-29 12:18:17 +03:00
|
|
|
if (unlikely(device == HTIF_DEV_SYSTEM)) {
|
2018-03-02 15:31:12 +03:00
|
|
|
/* frontend syscall handler, shutdown and exit code support */
|
2022-12-29 12:18:17 +03:00
|
|
|
if (cmd == HTIF_SYSTEM_CMD_SYSCALL) {
|
2018-03-02 15:31:12 +03:00
|
|
|
if (payload & 0x1) {
|
|
|
|
/* exit code */
|
|
|
|
int exit_code = payload >> 1;
|
|
|
|
exit(exit_code);
|
|
|
|
} else {
|
2022-12-29 12:18:23 +03:00
|
|
|
uint64_t syscall[8];
|
|
|
|
cpu_physical_memory_read(payload, syscall, sizeof(syscall));
|
|
|
|
if (syscall[0] == PK_SYS_WRITE &&
|
|
|
|
syscall[1] == HTIF_DEV_CONSOLE &&
|
|
|
|
syscall[3] == HTIF_CONSOLE_CMD_PUTC) {
|
|
|
|
uint8_t ch;
|
|
|
|
cpu_physical_memory_read(syscall[2], &ch, 1);
|
|
|
|
qemu_chr_fe_write(&s->chr, &ch, 1);
|
|
|
|
resp = 0x100 | (uint8_t)payload;
|
|
|
|
} else {
|
|
|
|
qemu_log_mask(LOG_UNIMP,
|
|
|
|
"pk syscall proxy not supported\n");
|
|
|
|
}
|
2018-03-02 15:31:12 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qemu_log("HTIF device %d: unknown command\n", device);
|
|
|
|
}
|
2022-12-29 12:18:17 +03:00
|
|
|
} else if (likely(device == HTIF_DEV_CONSOLE)) {
|
2018-03-02 15:31:12 +03:00
|
|
|
/* HTIF Console */
|
2022-12-29 12:18:17 +03:00
|
|
|
if (cmd == HTIF_CONSOLE_CMD_GETC) {
|
2018-03-02 15:31:12 +03:00
|
|
|
/* this should be a queue, but not yet implemented as such */
|
2022-12-29 12:18:20 +03:00
|
|
|
s->pending_read = val_written;
|
2022-12-29 12:18:21 +03:00
|
|
|
s->tohost = 0; /* clear to indicate we read */
|
2018-03-02 15:31:12 +03:00
|
|
|
return;
|
2022-12-29 12:18:17 +03:00
|
|
|
} else if (cmd == HTIF_CONSOLE_CMD_PUTC) {
|
2022-12-29 12:18:20 +03:00
|
|
|
qemu_chr_fe_write(&s->chr, (uint8_t *)&payload, 1);
|
2018-03-02 15:31:12 +03:00
|
|
|
resp = 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);
|
|
|
|
}
|
|
|
|
/*
|
2022-12-29 12:18:17 +03:00
|
|
|
* Latest bbl does not set fromhost to 0 if there is a value in tohost.
|
|
|
|
* 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 machine.
|
|
|
|
*
|
2022-12-29 12:18:20 +03:00
|
|
|
* while (!s->fromhost_inprogress &&
|
2022-12-29 12:18:21 +03:00
|
|
|
* s->fromhost != 0x0) {
|
2022-12-29 12:18:17 +03:00
|
|
|
* }
|
|
|
|
*/
|
2022-12-29 12:18:21 +03:00
|
|
|
s->fromhost = (val_written >> 48 << 48) | (resp << 16 >> 16);
|
|
|
|
s->tohost = 0; /* clear to indicate we read */
|
2018-03-02 15:31:12 +03:00
|
|
|
}
|
|
|
|
|
2022-12-29 12:18:20 +03:00
|
|
|
#define TOHOST_OFFSET1 (s->tohost_offset)
|
|
|
|
#define TOHOST_OFFSET2 (s->tohost_offset + 4)
|
|
|
|
#define FROMHOST_OFFSET1 (s->fromhost_offset)
|
|
|
|
#define FROMHOST_OFFSET2 (s->fromhost_offset + 4)
|
2018-03-02 15:31:12 +03:00
|
|
|
|
|
|
|
/* CPU wants to read an HTIF register */
|
|
|
|
static uint64_t htif_mm_read(void *opaque, hwaddr addr, unsigned size)
|
|
|
|
{
|
2022-12-29 12:18:20 +03:00
|
|
|
HTIFState *s = opaque;
|
2018-03-02 15:31:12 +03:00
|
|
|
if (addr == TOHOST_OFFSET1) {
|
2022-12-29 12:18:21 +03:00
|
|
|
return s->tohost & 0xFFFFFFFF;
|
2018-03-02 15:31:12 +03:00
|
|
|
} else if (addr == TOHOST_OFFSET2) {
|
2022-12-29 12:18:21 +03:00
|
|
|
return (s->tohost >> 32) & 0xFFFFFFFF;
|
2018-03-02 15:31:12 +03:00
|
|
|
} else if (addr == FROMHOST_OFFSET1) {
|
2022-12-29 12:18:21 +03:00
|
|
|
return s->fromhost & 0xFFFFFFFF;
|
2018-03-02 15:31:12 +03:00
|
|
|
} else if (addr == FROMHOST_OFFSET2) {
|
2022-12-29 12:18:21 +03:00
|
|
|
return (s->fromhost >> 32) & 0xFFFFFFFF;
|
2018-03-02 15:31:12 +03:00
|
|
|
} 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,
|
2022-12-29 12:18:17 +03:00
|
|
|
uint64_t value, unsigned size)
|
2018-03-02 15:31:12 +03:00
|
|
|
{
|
2022-12-29 12:18:20 +03:00
|
|
|
HTIFState *s = opaque;
|
2018-03-02 15:31:12 +03:00
|
|
|
if (addr == TOHOST_OFFSET1) {
|
2022-12-29 12:18:21 +03:00
|
|
|
if (s->tohost == 0x0) {
|
2022-12-29 12:18:20 +03:00
|
|
|
s->allow_tohost = 1;
|
2022-12-29 12:18:21 +03:00
|
|
|
s->tohost = value & 0xFFFFFFFF;
|
2018-03-02 15:31:12 +03:00
|
|
|
} else {
|
2022-12-29 12:18:20 +03:00
|
|
|
s->allow_tohost = 0;
|
2018-03-02 15:31:12 +03:00
|
|
|
}
|
|
|
|
} else if (addr == TOHOST_OFFSET2) {
|
2022-12-29 12:18:20 +03:00
|
|
|
if (s->allow_tohost) {
|
2022-12-29 12:18:21 +03:00
|
|
|
s->tohost |= value << 32;
|
|
|
|
htif_handle_tohost_write(s, s->tohost);
|
2018-03-02 15:31:12 +03:00
|
|
|
}
|
|
|
|
} else if (addr == FROMHOST_OFFSET1) {
|
2022-12-29 12:18:20 +03:00
|
|
|
s->fromhost_inprogress = 1;
|
2022-12-29 12:18:21 +03:00
|
|
|
s->fromhost = value & 0xFFFFFFFF;
|
2018-03-02 15:31:12 +03:00
|
|
|
} else if (addr == FROMHOST_OFFSET2) {
|
2022-12-29 12:18:21 +03:00
|
|
|
s->fromhost |= value << 32;
|
2022-12-29 12:18:20 +03:00
|
|
|
s->fromhost_inprogress = 0;
|
2018-03-02 15:31:12 +03:00
|
|
|
} else {
|
|
|
|
qemu_log("Invalid htif write: address %016" PRIx64 "\n",
|
|
|
|
(uint64_t)addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps htif_mm_ops = {
|
|
|
|
.read = htif_mm_read,
|
|
|
|
.write = htif_mm_write,
|
|
|
|
};
|
|
|
|
|
2022-12-29 12:18:21 +03:00
|
|
|
HTIFState *htif_mm_init(MemoryRegion *address_space, Chardev *chr,
|
2022-12-29 13:31:23 +03:00
|
|
|
uint64_t nonelf_base, bool custom_base)
|
2018-03-02 15:31:12 +03:00
|
|
|
{
|
2022-01-13 17:50:39 +03:00
|
|
|
uint64_t base, size, tohost_offset, fromhost_offset;
|
|
|
|
|
2022-12-29 13:31:23 +03:00
|
|
|
if (custom_base) {
|
2022-01-13 17:50:39 +03:00
|
|
|
fromhost_addr = nonelf_base;
|
|
|
|
tohost_addr = nonelf_base + 8;
|
2022-12-29 13:31:23 +03:00
|
|
|
} else {
|
|
|
|
if (!fromhost_addr || !tohost_addr) {
|
|
|
|
error_report("Invalid HTIF fromhost or tohost address");
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-13 17:50:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
base = MIN(tohost_addr, fromhost_addr);
|
|
|
|
size = MAX(tohost_addr + 8, fromhost_addr + 8) - base;
|
|
|
|
tohost_offset = tohost_addr - base;
|
|
|
|
fromhost_offset = fromhost_addr - base;
|
2018-03-02 15:31:12 +03:00
|
|
|
|
2022-03-15 17:41:56 +03:00
|
|
|
HTIFState *s = g_new0(HTIFState, 1);
|
2018-03-02 15:31:12 +03:00
|
|
|
s->tohost_offset = tohost_offset;
|
|
|
|
s->fromhost_offset = fromhost_offset;
|
|
|
|
s->pending_read = 0;
|
|
|
|
s->allow_tohost = 0;
|
|
|
|
s->fromhost_inprogress = 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);
|
2022-01-13 17:50:39 +03:00
|
|
|
|
|
|
|
memory_region_init_io(&s->mmio, NULL, &htif_mm_ops, s,
|
|
|
|
TYPE_HTIF_UART, size);
|
|
|
|
memory_region_add_subregion_overlap(address_space, base,
|
|
|
|
&s->mmio, 1);
|
2018-03-02 15:31:12 +03:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|