Skip to contentSkip to conversation

modify lguest console to support multiple hvc's

PATCH In review Eric Van Hensbergen 3 participants 3 messages Aug 30, 2007 – Aug 31, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 modify lguest console to support multiple hvc's

From: Eric Van Hensbergen <[email protected]>

This was a quick modification I did of lguest to be able to support multiple HVC channels for some experiements I was doing. I'm not sure if this is more generally useful, so I'm posting it to the list in case someone else has a need for it.

---
 Documentation/lguest/lguest.c |  161 ++++++++++++++++++++++++-----------------
 drivers/char/hvc_lguest.c     |   57 +++++++++------
 2 files changed, 129 insertions(+), 89 deletions(-)
Documentation/lguest/lguest.c +94 -67
index f791840..c6a3e4d 100644
@@ -690,12 +690,14 @@ static void restore_term(void)
 }
 
 /* We associate some data with the console for our exit hack. */
-struct console_abort
+struct console_priv
 {
+	/* which console we are */
+	int index;
 	/* How many times have they hit ^C? */
-	int count;
+	int a_count;
 	/* When did they start? */
-	struct timeval start;
+	struct timeval a_start;
 };
 
 /* This is the routine which handles console input (ie. stdin). */
@@ -705,11 +707,12 @@ static bool handle_console_input(int fd, struct device *dev)
 	int len;
 	unsigned int num;
 	struct iovec iov[LGUEST_MAX_DMA_SECTIONS];
-	struct console_abort *abort = dev->priv;
+	struct console_priv *cons = dev->priv;
 
 	/* First we get the console buffer from the Guest.  The key is dev->mem
-	 * which was set to 0 in setup_console(). */
-	lenp = get_dma_buffer(fd, dev->mem, iov, &num, &irq);
+	 * plus the console index adjusted to be a multiple of 4 because lguest
+	 * wants keys to be a multiple of 4 */
+	lenp = get_dma_buffer(fd, dev->mem+(cons->index*4), iov, &num, &irq);
 	if (!lenp) {
 		/* If it's not ready for input, warn and set up to discard. */
 		warn("console: no dma buffer!");
@@ -734,39 +737,44 @@ static bool handle_console_input(int fd, struct device *dev)
 		trigger_irq(fd, irq);
 	}
 
-	/* Three ^C within one second?  Exit.
-	 *
-	 * This is such a hack, but works surprisingly well.  Each ^C has to be
-	 * in a buffer by itself, so they can't be too fast.  But we check that
-	 * we get three within about a second, so they can't be too slow. */
-	if (len == 1 && ((char *)iov[0].iov_base)[0] == 3) {
-		if (!abort->count++)
-			gettimeofday(&abort->start, NULL);
-		else if (abort->count == 3) {
-			struct timeval now;
-			gettimeofday(&now, NULL);
-			if (now.tv_sec <= abort->start.tv_sec+1) {
-				u32 args[] = { LHREQ_BREAK, 0 };
-				/* Close the fd so Waker will know it has to
-				 * exit. */
-				close(waker_fd);
-				/* Just in case waker is blocked in BREAK, send
-				 * unbreak now. */
-				write(fd, args, sizeof(args));
-				exit(2);
+	/* Only do interrupt hack and restore_term() on initial console */
+	if (cons->index == 0) {
+		/* Three ^C within one second?  Exit.
+		 *
+		 * This is such a hack, but works surprisingly well.  Each ^C
+		 * has to be in a buffer by itself, so they can't be too fast.
+		 * But we check that we get three within about a second, so
+		 * they can't be too slow. */
+		if (len == 1 && ((char *)iov[0].iov_base)[0] == 3) {
+			if (!cons->a_count++)
+				gettimeofday(&cons->a_start, NULL);
+			else if (cons->a_count == 3) {
+				struct timeval now;
+				gettimeofday(&now, NULL);
+				if (now.tv_sec <= cons->a_start.tv_sec+1) {
+					u32 args[] = { LHREQ_BREAK, 0 };
+					/* Close the fd so Waker will know it
+					 * has to exit. */
+					close(waker_fd);
+					/* Just in case waker is blocked in
+					 * BREAK, send unbreak now. */
+					write(fd, args, sizeof(args));
+					exit(2);
+				}
+				cons->a_count = 0;
 			}
-			abort->count = 0;
+		} else
+			/* Any other key resets the abort counter. */
+			cons->a_count = 0;
+
+		/* Now, if we didn't read anything, put the input terminal
+		 * back and return failure (meaning, don't call us again). */
+		if (!len) {
+			restore_term();
+			return false;
 		}
-	} else
-		/* Any other key resets the abort counter. */
-		abort->count = 0;
-
-	/* Now, if we didn't read anything, put the input terminal back and
-	 * return failure (meaning, don't call us again). */
-	if (!len) {
-		restore_term();
-		return false;
 	}
+
 	/* Everything went OK! */
 	return true;
 }
@@ -777,7 +785,7 @@ static u32 handle_console_output(int fd, const struct iovec *iov,
 {
 	/* Whatever the Guest sends, write it to standard output.  Return the
 	 * number of bytes written. */
-	return writev(STDOUT_FILENO, iov, num);
+	return writev(dev->fd, iov, num);
 }
 
 /* Guest->Host network output is also pretty easy. */
@@ -1044,34 +1052,56 @@ static struct device *new_device(struct device_list *devices,
 	return dev;
 }
 
+static u32 str2ip(const char *ipaddr)
+{
+	unsigned int byte[4];
+
+	sscanf(ipaddr, "%u.%u.%u.%u", &byte[0], &byte[1], &byte[2], &byte[3]);
+	return (byte[0] << 24) | (byte[1] << 16) | (byte[2] << 8) | byte[3];
+}
+
 /* Our first setup routine is the console.  It's a fairly simple device, but
  * UNIX tty handling makes it uglier than it could be. */
-static void setup_console(struct device_list *devices)
+static void setup_console(char *arg, struct device_list *devices)
 {
 	struct device *dev;
-
-	/* If we can save the initial standard input settings... */
-	if (tcgetattr(STDIN_FILENO, &orig_term) == 0) {
-		struct termios term = orig_term;
-		/* Then we turn off echo, line buffering and ^C etc.  We want a
-		 * raw input stream to the Guest. */
-		term.c_lflag &= ~(ISIG|ICANON|ECHO);
-		tcsetattr(STDIN_FILENO, TCSANOW, &term);
-		/* If we exit gracefully, the original settings will be
-		 * restored so the user can see what they're typing. */
-		atexit(restore_term);
+	int fd;
+	static int hvcs_index;
+	struct console_priv *cons;
+
+	if (arg == NULL) {	/* default console - do the tty stuff */
+		/* If we can save the initial standard input settings... */
+		if (tcgetattr(STDIN_FILENO, &orig_term) == 0) {
+			struct termios term = orig_term;
+			/* Then we turn off echo, line buffering and ^C etc.
+			 * We want a raw input stream to the Guest. */
+			term.c_lflag &= ~(ISIG|ICANON|ECHO);
+			tcsetattr(STDIN_FILENO, TCSANOW, &term);
+			/* If we exit gracefully, the original settings will be
+			 * restored so the user can see what they're typing. */
+			atexit(restore_term);
+		}
+		fd = STDIN_FILENO;
+	} else {
+		/* create or open an exisitng fifo */
+		mkfifo(arg, S_IWUSR | S_IRUSR);
+		fd = open_or_die(arg, O_RDWR);
 	}
 
 	/* We don't currently require any memory for the console, so we ask for
 	 * 0 pages. */
 	dev = new_device(devices, LGUEST_DEVICE_T_CONSOLE, 0, 0,
-			 STDIN_FILENO, handle_console_input,
-			 LGUEST_CONSOLE_DMA_KEY, handle_console_output);
-	/* We store the console state in dev->priv, and initialize it. */
-	dev->priv = malloc(sizeof(struct console_abort));
-	((struct console_abort *)dev->priv)->count = 0;
-	verbose("device %p: console\n",
-		(void *)(dev->desc->pfn * getpagesize()));
+			 fd, handle_console_input,
+			 LGUEST_CONSOLE_DMA_KEY+(hvcs_index*4),
+			 handle_console_output);
+	dev->fd = fd;
+	/* We store the console state in dev->priv, and initialize */
+	dev->priv = malloc(sizeof(struct console_priv));
+	cons = dev->priv;
+	cons->a_count = 0;
+	cons->index = hvcs_index++;
+	verbose("device %p: console %d\n",
+		(void *)(dev->desc->pfn * getpagesize()), cons->index);
 }
 
 /* Setting up a block file is also fairly straightforward. */
@@ -1184,14 +1214,6 @@ static void setup_net_file(const char *filename,
 }
 /*:*/
 
-static u32 str2ip(const char *ipaddr)
-{
-	unsigned int byte[4];
-
-	sscanf(ipaddr, "%u.%u.%u.%u", &byte[0], &byte[1], &byte[2], &byte[3]);
-	return (byte[0] << 24) | (byte[1] << 16) | (byte[2] << 8) | byte[3];
-}
-
 /* This code is "adapted" from libbridge: it attaches the Host end of the
  * network device to the bridge device specified by the command line.
  *
@@ -1369,12 +1391,14 @@ static struct option opts[] = {
 	{ "tunnet", 1, NULL, 't' },
 	{ "block", 1, NULL, 'b' },
 	{ "initrd", 1, NULL, 'i' },
+	{ "console", 1, NULL, 'c' },
 	{ NULL },
 };
 static void usage(void)
 {
 	errx(1, "Usage: lguest [--verbose] "
 	     "[--sharenet=<filename>|--tunnet=(<ipaddr>|bridge:<bridgename>)\n"
+	     "|--console=<fifopath>...\n"
 	     "|--block=<filename>|--initrd=<filename>]...\n"
 	     "<mem-in-mb> vmlinux [args...]");
 }
@@ -1431,6 +1455,9 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	/* We always have at least one console device */
+	setup_console(NULL, &device_list);
+
 	/* The options are fairly straight-forward */
 	while ((c = getopt_long(argc, argv, "v", opts, NULL)) != EOF) {
 		switch (c) {
@@ -1446,6 +1473,9 @@ int main(int argc, char *argv[])
 		case 'b':
 			setup_block_file(optarg, &device_list);
 			break;
+		case 'c':
+			setup_console(optarg, &device_list);
+			break;
 		case 'i':
 			initrd_name = optarg;
 			break;
@@ -1459,9 +1489,6 @@ int main(int argc, char *argv[])
 	if (optind + 2 > argc)
 		usage();
 
-	/* We always have a console device */
-	setup_console(&device_list);
-
 	/* We start by mapping anonymous pages over all of guest-physical
 	 * memory range.  This fills it with 0, and ensures that the Guest
 	 * won't be killed when it tries to access it. */
drivers/char/hvc_lguest.c +35 -22

112 lines not on this page · raw message

Signature · 1 line
1.5.0.gddff-dirty
#1 lore raw
Quoted 8 lines · Eric Van Hensbergen

From: Eric Van Hensbergen <[email protected]>

This was a quick modification I did of lguest to be able to support multiple HVC channels for some experiements I was doing. I'm not sure if this is more generally useful, so I'm posting it to the list in case someone else has a need for it.

Hi Eric,

This is cool! Great that it's useful for you. What do the other
consoles look like from inside the guest?

Cheers,
Rusty.

#2 lore raw
Quoted 8 lines · Rusty Russell
Quoted 7 lines · Eric Van Hensbergen

From: Eric Van Hensbergen <[email protected]>

This was a quick modification I did of lguest to be able to support multiple HVC channels for some experiments I was doing. I'm not sure if this is more generally useful, so I'm posting it to the list in case someone else has a need for it.

Quoted 5 lines
Quoted 1 line

This is cool! Great that it's useful for you. What do the other
consoles look like from inside the guest?

They just show up on other hvc device minor numbers. I was running 9p over them, but I wanted a tighter coupling for v9fs so I could tune performance and incrementally optimize.

-eric