150336067SMichael Clark /* 250336067SMichael Clark * QEMU RISC-V Host Target Interface (HTIF) Emulation 350336067SMichael Clark * 450336067SMichael Clark * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 550336067SMichael Clark * Copyright (c) 2017-2018 SiFive, Inc. 650336067SMichael Clark * 750336067SMichael Clark * This provides HTIF device emulation for QEMU. At the moment this allows 850336067SMichael Clark * for identical copies of bbl/linux to run on both spike and QEMU. 950336067SMichael Clark * 1050336067SMichael Clark * This program is free software; you can redistribute it and/or modify it 1150336067SMichael Clark * under the terms and conditions of the GNU General Public License, 1250336067SMichael Clark * version 2 or later, as published by the Free Software Foundation. 1350336067SMichael Clark * 1450336067SMichael Clark * This program is distributed in the hope it will be useful, but WITHOUT 1550336067SMichael Clark * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1650336067SMichael Clark * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 1750336067SMichael Clark * more details. 1850336067SMichael Clark * 1950336067SMichael Clark * You should have received a copy of the GNU General Public License along with 2050336067SMichael Clark * this program. If not, see <http://www.gnu.org/licenses/>. 2150336067SMichael Clark */ 2250336067SMichael Clark 2350336067SMichael Clark #include "qemu/osdep.h" 2450336067SMichael Clark #include "qapi/error.h" 2550336067SMichael Clark #include "qemu/log.h" 2670eb9f9cSBin Meng #include "hw/char/riscv_htif.h" 2750336067SMichael Clark #include "hw/char/serial.h" 2850336067SMichael Clark #include "chardev/char.h" 2950336067SMichael Clark #include "chardev/char-fe.h" 3050336067SMichael Clark #include "qemu/timer.h" 3150336067SMichael Clark #include "qemu/error-report.h" 3250336067SMichael Clark 3350336067SMichael Clark #define RISCV_DEBUG_HTIF 0 3450336067SMichael Clark #define HTIF_DEBUG(fmt, ...) \ 3550336067SMichael Clark do { \ 3650336067SMichael Clark if (RISCV_DEBUG_HTIF) { \ 3750336067SMichael Clark qemu_log_mask(LOG_TRACE, "%s: " fmt "\n", __func__, ##__VA_ARGS__);\ 3850336067SMichael Clark } \ 3950336067SMichael Clark } while (0) 4050336067SMichael Clark 41753ae97aSBin Meng #define HTIF_DEV_SHIFT 56 42753ae97aSBin Meng #define HTIF_CMD_SHIFT 48 43753ae97aSBin Meng 44753ae97aSBin Meng #define HTIF_DEV_SYSTEM 0 45753ae97aSBin Meng #define HTIF_DEV_CONSOLE 1 46753ae97aSBin Meng 47753ae97aSBin Meng #define HTIF_SYSTEM_CMD_SYSCALL 0 48753ae97aSBin Meng #define HTIF_CONSOLE_CMD_GETC 0 49753ae97aSBin Meng #define HTIF_CONSOLE_CMD_PUTC 1 50753ae97aSBin Meng 51*a6e13e31SBin Meng /* PK system call number */ 52*a6e13e31SBin Meng #define PK_SYS_WRITE 64 53*a6e13e31SBin Meng 5450336067SMichael Clark static uint64_t fromhost_addr, tohost_addr; 5517b9751eSKONRAD Frederic static int address_symbol_set; 5650336067SMichael Clark 5750336067SMichael Clark void htif_symbol_callback(const char *st_name, int st_info, uint64_t st_value, 5850336067SMichael Clark uint64_t st_size) 5950336067SMichael Clark { 6050336067SMichael Clark if (strcmp("fromhost", st_name) == 0) { 6117b9751eSKONRAD Frederic address_symbol_set |= 1; 6250336067SMichael Clark fromhost_addr = st_value; 6350336067SMichael Clark if (st_size != 8) { 6450336067SMichael Clark error_report("HTIF fromhost must be 8 bytes"); 6550336067SMichael Clark exit(1); 6650336067SMichael Clark } 6750336067SMichael Clark } else if (strcmp("tohost", st_name) == 0) { 6817b9751eSKONRAD Frederic address_symbol_set |= 2; 6950336067SMichael Clark tohost_addr = st_value; 7050336067SMichael Clark if (st_size != 8) { 7150336067SMichael Clark error_report("HTIF tohost must be 8 bytes"); 7250336067SMichael Clark exit(1); 7350336067SMichael Clark } 7450336067SMichael Clark } 7550336067SMichael Clark } 7650336067SMichael Clark 7750336067SMichael Clark /* 7850336067SMichael Clark * Called by the char dev to see if HTIF is ready to accept input. 7950336067SMichael Clark */ 8050336067SMichael Clark static int htif_can_recv(void *opaque) 8150336067SMichael Clark { 8250336067SMichael Clark return 1; 8350336067SMichael Clark } 8450336067SMichael Clark 8550336067SMichael Clark /* 8650336067SMichael Clark * Called by the char dev to supply input to HTIF console. 8750336067SMichael Clark * We assume that we will receive one character at a time. 8850336067SMichael Clark */ 8950336067SMichael Clark static void htif_recv(void *opaque, const uint8_t *buf, int size) 9050336067SMichael Clark { 91dadee9e3SBin Meng HTIFState *s = opaque; 9250336067SMichael Clark 9350336067SMichael Clark if (size != 1) { 9450336067SMichael Clark return; 9550336067SMichael Clark } 9650336067SMichael Clark 97753ae97aSBin Meng /* 98753ae97aSBin Meng * TODO - we need to check whether mfromhost is zero which indicates 99753ae97aSBin Meng * the device is ready to receive. The current implementation 100753ae97aSBin Meng * will drop characters 101753ae97aSBin Meng */ 10250336067SMichael Clark 103dadee9e3SBin Meng uint64_t val_written = s->pending_read; 10450336067SMichael Clark uint64_t resp = 0x100 | *buf; 10550336067SMichael Clark 1061237c2d6SBin Meng s->fromhost = (val_written >> 48 << 48) | (resp << 16 >> 16); 10750336067SMichael Clark } 10850336067SMichael Clark 10950336067SMichael Clark /* 11050336067SMichael Clark * Called by the char dev to supply special events to the HTIF console. 11150336067SMichael Clark * Not used for HTIF. 11250336067SMichael Clark */ 113083b266fSPhilippe Mathieu-Daudé static void htif_event(void *opaque, QEMUChrEvent event) 11450336067SMichael Clark { 11550336067SMichael Clark 11650336067SMichael Clark } 11750336067SMichael Clark 11850336067SMichael Clark static int htif_be_change(void *opaque) 11950336067SMichael Clark { 12050336067SMichael Clark HTIFState *s = opaque; 12150336067SMichael Clark 12250336067SMichael Clark qemu_chr_fe_set_handlers(&s->chr, htif_can_recv, htif_recv, htif_event, 12350336067SMichael Clark htif_be_change, s, NULL, true); 12450336067SMichael Clark 12550336067SMichael Clark return 0; 12650336067SMichael Clark } 12750336067SMichael Clark 128753ae97aSBin Meng /* 129753ae97aSBin Meng * See below the tohost register format. 130753ae97aSBin Meng * 131753ae97aSBin Meng * Bits 63:56 indicate the "device". 132753ae97aSBin Meng * Bits 55:48 indicate the "command". 133753ae97aSBin Meng * 134753ae97aSBin Meng * Device 0 is the syscall device, which is used to emulate Unixy syscalls. 135753ae97aSBin Meng * It only implements command 0, which has two subfunctions: 136753ae97aSBin Meng * - If bit 0 is clear, then bits 47:0 represent a pointer to a struct 137753ae97aSBin Meng * describing the syscall. 138753ae97aSBin Meng * - If bit 1 is set, then bits 47:1 represent an exit code, with a zero 139753ae97aSBin Meng * value indicating success and other values indicating failure. 140753ae97aSBin Meng * 141753ae97aSBin Meng * Device 1 is the blocking character device. 142753ae97aSBin Meng * - Command 0 reads a character 143753ae97aSBin Meng * - Command 1 writes a character from the 8 LSBs of tohost 144753ae97aSBin Meng * 145753ae97aSBin Meng * For RV32, the tohost register is zero-extended, so only device=0 and 146753ae97aSBin Meng * command=0 (i.e. HTIF syscalls/exit codes) are supported. 147753ae97aSBin Meng */ 148dadee9e3SBin Meng static void htif_handle_tohost_write(HTIFState *s, uint64_t val_written) 14950336067SMichael Clark { 150753ae97aSBin Meng uint8_t device = val_written >> HTIF_DEV_SHIFT; 151753ae97aSBin Meng uint8_t cmd = val_written >> HTIF_CMD_SHIFT; 15250336067SMichael Clark uint64_t payload = val_written & 0xFFFFFFFFFFFFULL; 15350336067SMichael Clark int resp = 0; 15450336067SMichael Clark 15550336067SMichael Clark HTIF_DEBUG("mtohost write: device: %d cmd: %d what: %02" PRIx64 15650336067SMichael Clark " -payload: %016" PRIx64 "\n", device, cmd, payload & 0xFF, payload); 15750336067SMichael Clark 15850336067SMichael Clark /* 15950336067SMichael Clark * Currently, there is a fixed mapping of devices: 16050336067SMichael Clark * 0: riscv-tests Pass/Fail Reporting Only (no syscall proxy) 16150336067SMichael Clark * 1: Console 16250336067SMichael Clark */ 163753ae97aSBin Meng if (unlikely(device == HTIF_DEV_SYSTEM)) { 16450336067SMichael Clark /* frontend syscall handler, shutdown and exit code support */ 165753ae97aSBin Meng if (cmd == HTIF_SYSTEM_CMD_SYSCALL) { 16650336067SMichael Clark if (payload & 0x1) { 16750336067SMichael Clark /* exit code */ 16850336067SMichael Clark int exit_code = payload >> 1; 16950336067SMichael Clark exit(exit_code); 17050336067SMichael Clark } else { 171*a6e13e31SBin Meng uint64_t syscall[8]; 172*a6e13e31SBin Meng cpu_physical_memory_read(payload, syscall, sizeof(syscall)); 173*a6e13e31SBin Meng if (syscall[0] == PK_SYS_WRITE && 174*a6e13e31SBin Meng syscall[1] == HTIF_DEV_CONSOLE && 175*a6e13e31SBin Meng syscall[3] == HTIF_CONSOLE_CMD_PUTC) { 176*a6e13e31SBin Meng uint8_t ch; 177*a6e13e31SBin Meng cpu_physical_memory_read(syscall[2], &ch, 1); 178*a6e13e31SBin Meng qemu_chr_fe_write(&s->chr, &ch, 1); 179*a6e13e31SBin Meng resp = 0x100 | (uint8_t)payload; 180*a6e13e31SBin Meng } else { 181*a6e13e31SBin Meng qemu_log_mask(LOG_UNIMP, 182*a6e13e31SBin Meng "pk syscall proxy not supported\n"); 183*a6e13e31SBin Meng } 18450336067SMichael Clark } 18550336067SMichael Clark } else { 18650336067SMichael Clark qemu_log("HTIF device %d: unknown command\n", device); 18750336067SMichael Clark } 188753ae97aSBin Meng } else if (likely(device == HTIF_DEV_CONSOLE)) { 18950336067SMichael Clark /* HTIF Console */ 190753ae97aSBin Meng if (cmd == HTIF_CONSOLE_CMD_GETC) { 19150336067SMichael Clark /* this should be a queue, but not yet implemented as such */ 192dadee9e3SBin Meng s->pending_read = val_written; 1931237c2d6SBin Meng s->tohost = 0; /* clear to indicate we read */ 19450336067SMichael Clark return; 195753ae97aSBin Meng } else if (cmd == HTIF_CONSOLE_CMD_PUTC) { 196dadee9e3SBin Meng qemu_chr_fe_write(&s->chr, (uint8_t *)&payload, 1); 19750336067SMichael Clark resp = 0x100 | (uint8_t)payload; 19850336067SMichael Clark } else { 19950336067SMichael Clark qemu_log("HTIF device %d: unknown command\n", device); 20050336067SMichael Clark } 20150336067SMichael Clark } else { 20250336067SMichael Clark qemu_log("HTIF unknown device or command\n"); 20350336067SMichael Clark HTIF_DEBUG("device: %d cmd: %d what: %02" PRIx64 20450336067SMichael Clark " payload: %016" PRIx64, device, cmd, payload & 0xFF, payload); 20550336067SMichael Clark } 20650336067SMichael Clark /* 207753ae97aSBin Meng * Latest bbl does not set fromhost to 0 if there is a value in tohost. 208753ae97aSBin Meng * With this code enabled, qemu hangs waiting for fromhost to go to 0. 209753ae97aSBin Meng * With this code disabled, qemu works with bbl priv v1.9.1 and v1.10. 210753ae97aSBin Meng * HTIF needs protocol documentation and a more complete state machine. 211753ae97aSBin Meng * 212dadee9e3SBin Meng * while (!s->fromhost_inprogress && 2131237c2d6SBin Meng * s->fromhost != 0x0) { 214753ae97aSBin Meng * } 21550336067SMichael Clark */ 2161237c2d6SBin Meng s->fromhost = (val_written >> 48 << 48) | (resp << 16 >> 16); 2171237c2d6SBin Meng s->tohost = 0; /* clear to indicate we read */ 21850336067SMichael Clark } 21950336067SMichael Clark 220dadee9e3SBin Meng #define TOHOST_OFFSET1 (s->tohost_offset) 221dadee9e3SBin Meng #define TOHOST_OFFSET2 (s->tohost_offset + 4) 222dadee9e3SBin Meng #define FROMHOST_OFFSET1 (s->fromhost_offset) 223dadee9e3SBin Meng #define FROMHOST_OFFSET2 (s->fromhost_offset + 4) 22450336067SMichael Clark 22550336067SMichael Clark /* CPU wants to read an HTIF register */ 22650336067SMichael Clark static uint64_t htif_mm_read(void *opaque, hwaddr addr, unsigned size) 22750336067SMichael Clark { 228dadee9e3SBin Meng HTIFState *s = opaque; 22950336067SMichael Clark if (addr == TOHOST_OFFSET1) { 2301237c2d6SBin Meng return s->tohost & 0xFFFFFFFF; 23150336067SMichael Clark } else if (addr == TOHOST_OFFSET2) { 2321237c2d6SBin Meng return (s->tohost >> 32) & 0xFFFFFFFF; 23350336067SMichael Clark } else if (addr == FROMHOST_OFFSET1) { 2341237c2d6SBin Meng return s->fromhost & 0xFFFFFFFF; 23550336067SMichael Clark } else if (addr == FROMHOST_OFFSET2) { 2361237c2d6SBin Meng return (s->fromhost >> 32) & 0xFFFFFFFF; 23750336067SMichael Clark } else { 23850336067SMichael Clark qemu_log("Invalid htif read: address %016" PRIx64 "\n", 23950336067SMichael Clark (uint64_t)addr); 24050336067SMichael Clark return 0; 24150336067SMichael Clark } 24250336067SMichael Clark } 24350336067SMichael Clark 24450336067SMichael Clark /* CPU wrote to an HTIF register */ 24550336067SMichael Clark static void htif_mm_write(void *opaque, hwaddr addr, 24650336067SMichael Clark uint64_t value, unsigned size) 24750336067SMichael Clark { 248dadee9e3SBin Meng HTIFState *s = opaque; 24950336067SMichael Clark if (addr == TOHOST_OFFSET1) { 2501237c2d6SBin Meng if (s->tohost == 0x0) { 251dadee9e3SBin Meng s->allow_tohost = 1; 2521237c2d6SBin Meng s->tohost = value & 0xFFFFFFFF; 25350336067SMichael Clark } else { 254dadee9e3SBin Meng s->allow_tohost = 0; 25550336067SMichael Clark } 25650336067SMichael Clark } else if (addr == TOHOST_OFFSET2) { 257dadee9e3SBin Meng if (s->allow_tohost) { 2581237c2d6SBin Meng s->tohost |= value << 32; 2591237c2d6SBin Meng htif_handle_tohost_write(s, s->tohost); 26050336067SMichael Clark } 26150336067SMichael Clark } else if (addr == FROMHOST_OFFSET1) { 262dadee9e3SBin Meng s->fromhost_inprogress = 1; 2631237c2d6SBin Meng s->fromhost = value & 0xFFFFFFFF; 26450336067SMichael Clark } else if (addr == FROMHOST_OFFSET2) { 2651237c2d6SBin Meng s->fromhost |= value << 32; 266dadee9e3SBin Meng s->fromhost_inprogress = 0; 26750336067SMichael Clark } else { 26850336067SMichael Clark qemu_log("Invalid htif write: address %016" PRIx64 "\n", 26950336067SMichael Clark (uint64_t)addr); 27050336067SMichael Clark } 27150336067SMichael Clark } 27250336067SMichael Clark 27350336067SMichael Clark static const MemoryRegionOps htif_mm_ops = { 27450336067SMichael Clark .read = htif_mm_read, 27550336067SMichael Clark .write = htif_mm_write, 27650336067SMichael Clark }; 27750336067SMichael Clark 2788d8897acSAnup Patel bool htif_uses_elf_symbols(void) 27950336067SMichael Clark { 2808d8897acSAnup Patel return (address_symbol_set == 3) ? true : false; 2818d8897acSAnup Patel } 2828d8897acSAnup Patel 2831237c2d6SBin Meng HTIFState *htif_mm_init(MemoryRegion *address_space, Chardev *chr, 2841237c2d6SBin Meng uint64_t nonelf_base) 2858d8897acSAnup Patel { 2868d8897acSAnup Patel uint64_t base, size, tohost_offset, fromhost_offset; 2878d8897acSAnup Patel 2888d8897acSAnup Patel if (!htif_uses_elf_symbols()) { 2898d8897acSAnup Patel fromhost_addr = nonelf_base; 2908d8897acSAnup Patel tohost_addr = nonelf_base + 8; 2918d8897acSAnup Patel } 2928d8897acSAnup Patel 2938d8897acSAnup Patel base = MIN(tohost_addr, fromhost_addr); 2948d8897acSAnup Patel size = MAX(tohost_addr + 8, fromhost_addr + 8) - base; 2958d8897acSAnup Patel tohost_offset = tohost_addr - base; 2968d8897acSAnup Patel fromhost_offset = fromhost_addr - base; 29750336067SMichael Clark 298b21e2380SMarkus Armbruster HTIFState *s = g_new0(HTIFState, 1); 29950336067SMichael Clark s->tohost_offset = tohost_offset; 30050336067SMichael Clark s->fromhost_offset = fromhost_offset; 30150336067SMichael Clark s->pending_read = 0; 30250336067SMichael Clark s->allow_tohost = 0; 30350336067SMichael Clark s->fromhost_inprogress = 0; 30450336067SMichael Clark qemu_chr_fe_init(&s->chr, chr, &error_abort); 30550336067SMichael Clark qemu_chr_fe_set_handlers(&s->chr, htif_can_recv, htif_recv, htif_event, 30650336067SMichael Clark htif_be_change, s, NULL, true); 3078d8897acSAnup Patel 30850336067SMichael Clark memory_region_init_io(&s->mmio, NULL, &htif_mm_ops, s, 30950336067SMichael Clark TYPE_HTIF_UART, size); 3106fad7d18SKONRAD Frederic memory_region_add_subregion_overlap(address_space, base, 3116fad7d18SKONRAD Frederic &s->mmio, 1); 31250336067SMichael Clark 31350336067SMichael Clark return s; 31450336067SMichael Clark } 315