socsvn commit: r238927 - soc2012/syuu/bhyve-bios/usr.sbin/bhyve

syuu at FreeBSD.org syuu at FreeBSD.org
Tue Jul 3 19:26:02 UTC 2012


Author: syuu
Date: Tue Jul  3 19:25:59 2012
New Revision: 238927
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=238927

Log:
  Rename VMCALL -> HYPERCALL, inital implement of INT 10h

Added:
  soc2012/syuu/bhyve-bios/usr.sbin/bhyve/bios_call.c
  soc2012/syuu/bhyve-bios/usr.sbin/bhyve/bios_call.h
  soc2012/syuu/bhyve-bios/usr.sbin/bhyve/bios_int10.c
Modified:
  soc2012/syuu/bhyve-bios/usr.sbin/bhyve/Makefile
  soc2012/syuu/bhyve-bios/usr.sbin/bhyve/fbsdrun.c

Modified: soc2012/syuu/bhyve-bios/usr.sbin/bhyve/Makefile
==============================================================================
--- soc2012/syuu/bhyve-bios/usr.sbin/bhyve/Makefile	Tue Jul  3 19:19:03 2012	(r238926)
+++ soc2012/syuu/bhyve-bios/usr.sbin/bhyve/Makefile	Tue Jul  3 19:25:59 2012	(r238927)
@@ -8,6 +8,7 @@
 SRCS+=  instruction_emul.c mevent.c
 SRCS+=	pci_emul.c pci_hostbridge.c pci_passthru.c pci_virtio_block.c
 SRCS+=	pci_virtio_net.c pci_uart.c pit_8254.c post.c rtc.c uart.c xmsr.c
+SRCS+=  bios_call.c bios_int10.c
 
 NO_MAN=
 

Added: soc2012/syuu/bhyve-bios/usr.sbin/bhyve/bios_call.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2012/syuu/bhyve-bios/usr.sbin/bhyve/bios_call.c	Tue Jul  3 19:25:59 2012	(r238927)
@@ -0,0 +1,105 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/linker_set.h>
+
+#include <stdio.h>
+#include <assert.h>
+
+#include "bios_call.h"
+
+SET_DECLARE(bios_call_set, struct bios_call);
+
+#define	MAX_INTRS	(0xff)
+
+static struct {
+	const char	*name;
+	bios_call_func_t	handler;
+} bios_call_handlers[MAX_INTRS];
+
+static int
+default_bios_call(struct vmctx *ctx, int vcpu, int intno)
+{
+        fprintf(stderr, "Not implemented BIOS call int=%x\n", intno);
+
+        return (-1);
+}
+
+int
+emulate_bios_call(struct vmctx *ctx, int vcpu, int intno)
+{
+	bios_call_func_t handler;
+
+	assert(intno < MAX_INTRS);
+
+	handler = bios_call_handlers[intno].handler;
+
+	if (handler == default_bios_call)
+		return (-1);
+
+	return ((*handler)(ctx, vcpu, intno));
+}
+
+void
+init_bios_call(void)
+{
+	struct bios_call **bcpp, *bcp;
+	int i;
+
+	/*
+	 * Set up the default handler for all intnos
+	 */
+	for (i = 0; i < MAX_INTRS; i++) {
+		bios_call_handlers[i].name = "default";
+		bios_call_handlers[i].handler = default_bios_call;
+	}
+
+	/*
+	 * Overwrite with specified handlers
+	 */
+	SET_FOREACH(bcpp, bios_call_set) {
+		bcp = *bcpp;
+		assert(bcp->intno < MAX_INTRS);
+		bios_call_handlers[bcp->intno].name = bcp->name;
+		bios_call_handlers[bcp->intno].handler = bcp->handler;
+	}
+}
+
+int
+register_bios_call(struct bios_call *bcp)
+{
+	assert(bcp->intno < MAX_INTRS);
+	bios_call_handlers[bcp->intno].name = bcp->name;
+	bios_call_handlers[bcp->intno].handler = bcp->handler;
+
+	return (0);
+}

Added: soc2012/syuu/bhyve-bios/usr.sbin/bhyve/bios_call.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2012/syuu/bhyve-bios/usr.sbin/bhyve/bios_call.h	Tue Jul  3 19:25:59 2012	(r238927)
@@ -0,0 +1,56 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _BIOS_CALL_H_
+#define	_BIOS_CALL_H_
+
+#include <sys/linker_set.h>
+
+struct vmctx;
+
+typedef int (*bios_call_func_t)(struct vmctx *ctx, int vcpu, int intno);
+
+struct bios_call {
+	const char 	*name;
+	int		intno;
+	bios_call_func_t	handler;
+};
+
+#define	BIOS_CALL(name, intno, handler)					\
+	static struct bios_call __CONCAT(__bios_call, __LINE__) = { 	\
+		#name,							\
+		(intno),						\
+		(handler),						\
+	};								\
+	DATA_SET(bios_call_set, __CONCAT(__bios_call, __LINE__))
+	
+void	init_bios_call(void);
+int	emulate_bios_call(struct vmctx *, int vcpu, int intno);
+int	register_bios_call(struct bios_call *call);
+
+#endif	/* _BIOS_CALL_H_ */

Added: soc2012/syuu/bhyve-bios/usr.sbin/bhyve/bios_int10.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ soc2012/syuu/bhyve-bios/usr.sbin/bhyve/bios_int10.c	Tue Jul  3 19:25:59 2012	(r238927)
@@ -0,0 +1,109 @@
+/*-
+ * Copyright (c) 2011 NetApp, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <termios.h>
+#include <unistd.h>
+#include <stdbool.h>
+
+#include <machine/vmm.h>
+#include <vmmapi.h>
+
+#include "bios_call.h"
+
+#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 void
+ttywrite(unsigned char wb)
+{
+	(void) write(STDOUT_FILENO, &wb, 1);
+}
+
+static int
+int10_handler(struct vmctx *ctx, int vcpu, int intno)
+{
+	static int opened;
+	uint64_t rax, rbx;
+	uint8_t al, ah, bl, bh;
+	int error;
+
+	if (!opened) {
+		ttyopen();
+		opened = 1;
+	}
+
+	if ((error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RAX, &rax)) != 0)
+		goto done;
+
+	if ((error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RBX, &rbx)) != 0)
+		goto done;
+
+	al = (uint8_t)rax;
+	ah = (uint8_t)(rax >> 8);
+	bl = (uint8_t)rbx;
+	bh = (uint8_t)(rbx >> 8);
+
+	switch (ah) {
+	case 0x0e:
+		ttywrite(al);
+		break;
+	default:
+        	fprintf(stderr, "Not implemented BIOS call int=%x ah=%x\n",
+			intno, ah);
+	}
+
+done:
+	return (error);
+
+}
+BIOS_CALL(int10, 0x10, int10_handler);

Modified: soc2012/syuu/bhyve-bios/usr.sbin/bhyve/fbsdrun.c
==============================================================================
--- soc2012/syuu/bhyve-bios/usr.sbin/bhyve/fbsdrun.c	Tue Jul  3 19:19:03 2012	(r238926)
+++ soc2012/syuu/bhyve-bios/usr.sbin/bhyve/fbsdrun.c	Tue Jul  3 19:25:59 2012	(r238927)
@@ -43,7 +43,6 @@
 #include <errno.h>
 #include <signal.h>
 #include <pthread.h>
-#include <inttypes.h>
 
 #include <machine/vmm.h>
 #include <vmmapi.h>
@@ -55,6 +54,7 @@
 #include "pci_emul.h"
 #include "xmsr.h"
 #include "instruction_emul.h"
+#include "bios_call.h"
 
 #define	DEFAULT_GUEST_HZ	100
 #define	DEFAULT_GUEST_TSLICE	200
@@ -434,39 +434,23 @@
 }
 
 static int
-vmexit_vmcall(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
+vmexit_hypercall(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
 {
-	int error;
-	uint64_t rsp, rip, rax, rbx, rcx, rdx;
-	uint64_t intr;
-
-	error = vm_get_register(ctx, *pvcpu, VM_REG_GUEST_RSP, &rsp);
-	if (!error)
-		error = vm_get_register(ctx, *pvcpu, VM_REG_GUEST_RIP, &rip);
-	if (!error)
-		error = vm_get_register(ctx, *pvcpu, VM_REG_GUEST_RAX, &rax);
-	if (!error)
-		error = vm_get_register(ctx, *pvcpu, VM_REG_GUEST_RBX, &rbx);
-	if (!error)
-		error = vm_get_register(ctx, *pvcpu, VM_REG_GUEST_RCX, &rcx);
-	if (!error)
-		error = vm_get_register(ctx, *pvcpu, VM_REG_GUEST_RDX, &rdx);
+	int intno = (vmexit->rip - 0x400) / 0x4;
 
-	if (error) {
-		printf("errno = %d\n", errno);
+	if (!bios_mode) {
+		fprintf(stderr, "Failed to handle hypercall at 0x%lx\n", 
+			vmexit->rip);
 		return (VMEXIT_ABORT);
 	}
-
-	printf("VMCALL handled\n");
-	printf("rsp=%"PRIx64" rip=%"PRIx64" rax=%"PRIx64" rbx=%"PRIx64" rcx=%"PRIx64" rdx=%"PRIx64"\n",
-		rsp, rip, rax, rbx, rcx, rdx);
-	intr = (rip - 0x400) / 0x4;
-	printf("intr=%"PRIu64"\n", intr);
-
-	if (intr == 0x14)
+		
+	if (emulate_bios_call(ctx, *pvcpu, intno) != 0) {
+		fprintf(stderr, "Failed to emulate BIOS call at 0x%lx\n", 
+			vmexit->rip);
 		return (VMEXIT_ABORT);
-	else
-		return (VMEXIT_CONTINUE);
+	}
+		
+	return (VMEXIT_CONTINUE);
 }
 
 static void
@@ -510,7 +494,7 @@
 	[VM_EXITCODE_WRMSR]  = vmexit_wrmsr,
 	[VM_EXITCODE_MTRAP]  = vmexit_mtrap,
 	[VM_EXITCODE_PAGING] = vmexit_paging,
-	[VM_EXITCODE_VMCALL] = vmexit_vmcall,
+	[VM_EXITCODE_HYPERCALL] = vmexit_hypercall,
 };
 
 static void
@@ -690,6 +674,7 @@
 
 	if (bios_mode != 0) {
 		vm_set_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, 1);
+		init_bios_call();
 	}
 
 	init_inout();


More information about the svn-soc-all mailing list