From owner-svn-soc-all@freebsd.org Thu Jul 23 17:56:53 2015 Return-Path: Delivered-To: svn-soc-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 8E2ED9A9E16 for ; Thu, 23 Jul 2015 17:56:53 +0000 (UTC) (envelope-from mihai@FreeBSD.org) Received: from socsvn.freebsd.org (socsvn.freebsd.org [IPv6:2001:1900:2254:206a::50:2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 64CA318FE for ; Thu, 23 Jul 2015 17:56:53 +0000 (UTC) (envelope-from mihai@FreeBSD.org) Received: from socsvn.freebsd.org ([127.0.1.124]) by socsvn.freebsd.org (8.15.2/8.15.2) with ESMTP id t6NHur5v036665 for ; Thu, 23 Jul 2015 17:56:53 GMT (envelope-from mihai@FreeBSD.org) Received: (from www@localhost) by socsvn.freebsd.org (8.15.2/8.15.2/Submit) id t6NHuqKj036663 for svn-soc-all@FreeBSD.org; Thu, 23 Jul 2015 17:56:52 GMT (envelope-from mihai@FreeBSD.org) Date: Thu, 23 Jul 2015 17:56:52 GMT Message-Id: <201507231756.t6NHuqKj036663@socsvn.freebsd.org> X-Authentication-Warning: socsvn.freebsd.org: www set sender to mihai@FreeBSD.org using -f From: mihai@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r288699 - soc2015/mihai/bhyve-on-arm-head/usr.sbin/bhyvearm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-soc-all@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: SVN commit messages for the entire Summer of Code repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 23 Jul 2015 17:56:53 -0000 Author: mihai Date: Thu Jul 23 17:56:52 2015 New Revision: 288699 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=288699 Log: soc2015: mihai: bhyve: usr.sbin: bhyvearm: consport.c: added emulation for console port [bvm] using memory Added: soc2015/mihai/bhyve-on-arm-head/usr.sbin/bhyvearm/consport.c Added: soc2015/mihai/bhyve-on-arm-head/usr.sbin/bhyvearm/consport.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2015/mihai/bhyve-on-arm-head/usr.sbin/bhyvearm/consport.c Thu Jul 23 17:56:52 2015 (r288699) @@ -0,0 +1,118 @@ +#include +#include + +#include +#include +#include +#include +#include + +#include "mem.h" + +#define BVM_CONSOLE_PORT 0x220 +#define BVM_CONS_SIG ('b' << 8 | 'v') + +static struct termios tio_orig, tio_new; + +static void +ttyclose(void) +{ + tcsetattr(STDIN_FILENO, TCSANOW, &tio_orig); +} + +static void +ttyopen(void) +{ + tcgetattr(STDIN_FILENO, &tio_orig); + + cfmakeraw(&tio_new); + tcsetattr(STDIN_FILENO, TCSANOW, &tio_new); + + atexit(ttyclose); +} + +static bool +tty_char_available(void) +{ + fd_set rfds; + struct timeval tv; + + FD_ZERO(&rfds); + FD_SET(STDIN_FILENO, &rfds); + tv.tv_sec = 0; + tv.tv_usec = 0; + if (select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tv) > 0) { + return (true); + } else { + return (false); + } +} + +static int +ttyread(void) +{ + char rb; + + if (tty_char_available()) { + read(STDIN_FILENO, &rb, 1); + return (rb & 0xff); + } else { + return (-1); + } +} + +static void +ttywrite(unsigned char wb) +{ + (void) write(STDOUT_FILENO, &wb, 1); +} + +static int +console_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr, int size, uint64_t *val, void *arg1, long arg2) +{ + static int opened; + + if (size == 2 && dir == MEM_F_READ) { + *val = BVM_CONS_SIG; + return (0); + } + + /* + * Guests might probe this port to look for old ISA devices + * using single-byte reads. Return 0xff for those. + */ + if (size == 1 && dir == MEM_F_READ) { + *val = 0xff; + return (0); + } + + if (size != 4) + return (-1); + + if (!opened) { + ttyopen(); + opened = 1; + } + + if (dir == MEM_F_READ) + *val = ttyread(); + else + ttywrite(*val); + return (0); +} + +struct mem_range consport ={ + "bvmcons", + MEM_F_RW, + console_handler, + NULL, + 0, + BVM_CONSOLE_PORT, + sizeof(int) +}; + +void +init_bvmcons(void) +{ + register_mem(&consport); +}