Skip to contentSkip to conversation

Paravirt framebuffer frontend kernel support [1/5]

PATCH In review Jeremy Katz 6 participants 23 messages Sep 2, 2006 – Nov 3, 2006

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

PATCH 1/1 Paravirt framebuffer frontend kernel support [1/5]

Initial patch from Anthony Liguori, ported to xenstore and current Xen by Markus Armbruster. Further improvements since the last submission
* Don't set up on dom0 or if our domain isn't setup for a graphics console (katzj)
* Compute framebuffer size from screen dimension (armbru)
* Implement XENFB_TYPE_SET_EVENTS (armbru)
* Future-proof layout of shared page (armbru)
* Memory barriers (armbru)
* Coaelsce notifies: just one batch of events instead of one per event (armbru)
* Error handling fixes (armbru)

Cc Markus Armbruster <[email protected]>
Cc Anthony Liguori <[email protected]>
linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c +4 -0
diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c
@@ -1866,8 +1866,12 @@ void __init setup_arch(char **cmdline_p)
 #endif
 #endif
 	} else {
+#if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
+		conswitchp = &dummy_con;
+#else
 		extern int console_use_vt;
 		console_use_vt = 0;
+#endif
 	}
 }
 
linux-2.6-xen-sparse/drivers/xen/Kconfig +23 -0
diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/Kconfig
@@ -172,6 +172,29 @@ config XEN_NETDEV_FRONTEND
 	  dedicated device-driver domain, or your master control domain
 	  (domain 0), then you almost certainly want to say Y here.
 
+config XEN_FRAMEBUFFER
+	tristate "Framebuffer-device frontend driver"
+	depends on XEN && FB
+	select FB_CFB_FILLRECT
+	select FB_CFB_COPYAREA
+	select FB_CFB_IMAGEBLIT
+	default y
+	help
+	  The framebuffer-device frontend drivers allows the kernel to create a
+	  virtual framebuffer.  This framebuffer can be viewed in another
+	  domain.  Unless this domain has access to a real video card, you
+	  probably want to say Y here.
+
+config XEN_KEYBOARD
+	tristate "Keyboard-device frontend driver"
+	depends on XEN
+	default y
+	help
+	  The keyboard-device frontend driver allows the kernel to create a
+	  virtual keyboard.  This keyboard can then be driven by another
+	  domain.  If you've said Y to CONFIG_XEN_FRAMEBUFFER, you probably
+	  want to say Y here.
+
 config XEN_SCRUB_PAGES
 	bool "Scrub memory before freeing it to Xen"
 	default y
linux-2.6-xen-sparse/drivers/xen/Makefile +2 -0
diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/Makefile
@@ -15,3 +15,5 @@ obj-$(CONFIG_XEN_NETDEV_FRONTEND)	+= net
 obj-$(CONFIG_XEN_NETDEV_FRONTEND)	+= netfront/
 obj-$(CONFIG_XEN_PCIDEV_BACKEND)	+= pciback/
 obj-$(CONFIG_XEN_PCIDEV_FRONTEND)	+= pcifront/
+obj-$(CONFIG_XEN_FRAMEBUFFER)		+= xenfb/
+obj-$(CONFIG_XEN_KEYBOARD)		+= xenkbd/
linux-2.6-xen-sparse/mm/memory.c +1 -0
diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/mm/memory.c
@@ -881,6 +881,7 @@ unsigned long zap_page_range(struct vm_a
 		tlb_finish_mmu(tlb, address, end);
 	return end;
 }
+EXPORT_SYMBOL(zap_page_range);
 
 /*
  * Do a quick page-table lookup for a single page.
linux-2.6-xen-sparse/drivers/xen/xenfb/Makefile new file +1 -0
diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/xenfb/Makefile
@@ -0,0 +1,1 @@
+obj-$(CONFIG_XEN_FRAMEBUFFER)	:= xenfb.o
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +571 -0

571 lines not on this page · raw message

linux-2.6-xen-sparse/drivers/xen/xenkbd/Makefile new file +1 -0
diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/xenkbd/Makefile
@@ -0,0 +1,1 @@
+obj-$(CONFIG_XEN_KEYBOARD)	+= xenkbd.o
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +178 -0

178 lines not on this page · raw message

linux-2.6-xen-sparse/include/linux/xenfb.h new file +108 -0

108 lines not on this page · raw message

linux-2.6-xen-sparse/include/linux/xenkbd.h new file +92 -0

92 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw

Okay, this fixes a lot of the things I complained about last time, and most of the new problems should be fairly easy to fix up.

There are still a couple of things from the last round which are unfixed. This is mostly my fault for going on holiday at an inconvenient time; sorry about that.

The big problem with this is that you don't support shadow translate mode guests, because the frontend exposes mfns directly to the backend. The correct way of fixing this is to use grant tables, but, as Anthony pointed out, that's quite hard due to the amount of memory you need to grant access to.

The easy way of doing this would be to just increase the size of the grant tables. Alternatively, you could add support for granting access to ranges of memory, but that sounds like a whole lot of work.

You could also put in a quick hack of just translating the mfns in the backend if the frontend is in shadow translate mode, but that's not really very nice.

It'd also be nice if the protocol supported putting the backend in an unprivileged domain, which grant tables would give you for free, but that's a lower priority.

Aside from that, the lack of support for multiple framebuffers in one guest is kind of annoying. It's not necessary to have this working in a first cut of the patch, but it'd be good if the protocol included some way of adding it in later, which the current one doesn't.

Hard coding the resolution is also kind of icky, but is again acceptable for a first cut. It'd be good to have at least a plan for how this is supposed to work before the thing gets merged, including how to change the resolution of a live framebuffer.

Also, the spec you pointed me at last time appears to be out of date:
it still thinks you get the pgdir mfn from start info, which certainly isn't acceptable for new drivers.

Quoted 21 lines

Initial patch from Anthony Liguori, ported to xenstore and current Xen by Markus Armbruster. Further improvements since the last submission
* Don't set up on dom0 or if our domain isn't setup for a graphics console (katzj)
* Compute framebuffer size from screen dimension (armbru)
* Implement XENFB_TYPE_SET_EVENTS (armbru)
* Future-proof layout of shared page (armbru)
* Memory barriers (armbru)
* Coaelsce notifies: just one batch of events instead of one per event (armbru)
* Error handling fixes (armbru)

Cc Markus Armbruster <[email protected]>
Cc Anthony Liguori <[email protected]>

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c

linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c +0 -0
@@ -1866,8 +1866,12 @@ void __init setup_arch(char **cmdline_p)
 #endif

linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c void __init setup_arch(char **cmdline_p) · line 1876

Quoted 10 lines
linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c +4 -0
 #endif
 	} else {
+#if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
+		conswitchp = &dummy_con;
+#else
 		extern int console_use_vt;
 		console_use_vt = 0;
+#endif
 	}
 }

Not quite sure I understand what this is trying to achieve, or why it's related to the PV framebuffer stuff.

Quoted 20 lines

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/Kconfig

linux-2.6-xen-sparse/drivers/xen/Kconfig +13 -0
@@ -172,6 +172,29 @@ config XEN_NETDEV_FRONTEND
 	  dedicated device-driver domain, or your master control domain
 	  (domain 0), then you almost certainly want to say Y here.
 
+config XEN_FRAMEBUFFER
+	tristate "Framebuffer-device frontend driver"
+	depends on XEN && FB
+	select FB_CFB_FILLRECT
+	select FB_CFB_COPYAREA
+	select FB_CFB_IMAGEBLIT
+	default y
+	help
+	  The framebuffer-device frontend drivers allows the kernel to create a
+	  virtual framebuffer.  This framebuffer can be viewed in another
+	  domain.  Unless this domain has access to a real video card, you
+	  probably want to say Y here.
+

linux-2.6-xen-sparse/drivers/xen/Kconfig config XEN_NETDEV_FRONTEND · line 197

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/Kconfig +10 -0
+config XEN_KEYBOARD
+	tristate "Keyboard-device frontend driver"
+	depends on XEN
+	default y
+	help
+	  The keyboard-device frontend driver allows the kernel to create a
+	  virtual keyboard.  This keyboard can then be driven by another
+	  domain.  If you've said Y to CONFIG_XEN_FRAMEBUFFER, you probably
+	  want to say Y here.
+

From last time:

Quoted 5 lines
Quoted 4 lines
Quoted 1 line

What happens if you configure XEN_KEYBOARD but not XEN_FRAMEBUFFER?

The backend refuses to connect, i.e. you can't use it. Anthony, can you shed some light on why you created two separate configs? you shed some light on why you created two separate configs?

They are two separate devices. Seemed like a natural separation to me.

Perhaps XEN_KEYBOARD should depend on XEN_FRAMEBUFFER, given that it doesn't work without it.

Quoted 16 lines

config XEN_SCRUB_PAGES

linux-2.6-xen-sparse/drivers/xen/Kconfig +0 -0
 	bool "Scrub memory before freeing it to Xen"
 	default y

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/Makefile

linux-2.6-xen-sparse/drivers/xen/Makefile +2 -0
@@ -15,3 +15,5 @@ obj-$(CONFIG_XEN_NETDEV_FRONTEND)	+= net
 obj-$(CONFIG_XEN_NETDEV_FRONTEND)	+= netfront/
 obj-$(CONFIG_XEN_PCIDEV_BACKEND)	+= pciback/
 obj-$(CONFIG_XEN_PCIDEV_FRONTEND)	+= pcifront/
+obj-$(CONFIG_XEN_FRAMEBUFFER)		+= xenfb/
+obj-$(CONFIG_XEN_KEYBOARD)		+= xenkbd/

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/mm/memory.c

linux-2.6-xen-sparse/mm/memory.c unsigned long zap_page_range(struct vm_a · line 884

Quoted 4 lines
linux-2.6-xen-sparse/mm/memory.c +1 -0
@@ -881,6 +881,7 @@ unsigned long zap_page_range(struct vm_a
 		tlb_finish_mmu(tlb, address, end);
 	return end;
 }
+EXPORT_SYMBOL(zap_page_range);

It's unfortunate that we have to modify an upstream file here, but I can't really see any alternative.

Quoted 38 lines
linux-2.6-xen-sparse/mm/memory.c +0 -0
 /*
  * Do a quick page-table lookup for a single page.

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/xenfb/Makefile

linux-2.6-xen-sparse/drivers/xen/xenfb/Makefile new file +1 -0
@@ -0,0 +1,1 @@
+obj-$(CONFIG_XEN_FRAMEBUFFER)	:= xenfb.o

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +26 -0
@@ -0,0 +1,571 @@
+/*
+ * linux/drivers/video/xenfb.c -- Xen para-virtual frame buffer device
+ *
+ * Copyright (C) 2005-2006
+ *
+ *      Anthony Liguori <[email protected]>
+ *
+ *  Based on linux/drivers/video/q40fb.c
+ *
+ *  This file is subject to the terms and conditions of the GNU General Public
+ *  License. See the file COPYING in the main directory of this archive for
+ *  more details.
+ */
+
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/fb.h>
+#include <linux/module.h>
+#include <linux/vmalloc.h>
+#include <linux/mm.h>
+#include <asm/hypervisor.h>
+#include <xen/evtchn.h>
+#include <xen/xenbus.h>
+#include <linux/xenfb.h>
+#include <linux/kthread.h>
+

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 36

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+#define XENFB_WIDTH 800
+#define XENFB_HEIGHT 600
+#define XENFB_DEPTH 32
+
+static int xenfb_fps = 20;
+static unsigned long xenfb_mem_len = XENFB_WIDTH * XENFB_HEIGHT * XENFB_DEPTH / 8;
+
+struct xenfb_mapping
+{
+	struct list_head	next;

Perhaps not the best name for a list head?

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 48

Quoted 12 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +12 -0
+	struct vm_area_struct	*vma;
+	atomic_t		map_refs;
+	int			faults;
+	struct xenfb_info	*info;
+};
+
+struct xenfb_info
+{
+	struct task_struct		*kthread;
+	wait_queue_head_t		wq;
+
+	unsigned char			*fb;

Why unsigned char?

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 50

Quoted 2 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +2 -0
+	struct fb_fix_screeninfo	*fix;
+	struct fb_var_screeninfo	*var;

Not quite sure what these are for. Why not just get at them through the fb_info pointer?

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 55

Quoted 6 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+	struct fb_info			*fb_info;
+	struct timer_list		refresh;
+	int				dirty;
 
+	int				y1, y2;
+	int				x1, x2;

These could perhaps do with slightly more descriptive names?

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 2 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +2 -0
+
+	struct semaphore		mm_lock;

That's going to confuse people.

(Me, for a start)

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 66

Quoted 9 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +9 -0
+	int				nr_pages;
+	struct page			**pages;
+	struct list_head		mappings;
+
+	unsigned			evtchn;
+	int				irq;
+	struct xenfb_page		*page;
+	unsigned long 			*mfns;
+	u32				flags;

Maybe a comment to say what sort of flags go in here?

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+};
+
+static void xenfb_do_update(struct xenfb_info *info,
+			    int x, int y, int w, int h)
+{
+	union xenfb_out_event event;
+	__u32 prod;

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 83

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+
+	event.type = XENFB_TYPE_UPDATE;
+	event.update.x = x;
+	event.update.y = y;
+	event.update.width = w;
+	event.update.height = h;
+
+	prod = info->page->out_prod;
+	if (prod - info->page->out_cons == XENFB_OUT_RING_LEN)
+		return;		/* ring buffer full, event lost */

It seems to me like if we lose the event, we probably shouldn't mark the framebuffer as clean and throw away the update region. Perhaps the caller should instead reschedule the timer?

Also, is there any reason the test couldn't use xenfb_queue_full?

Quoted 26 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +26 -0
+	mb();			/* ensure ring space available */
+	XENFB_OUT_RING_REF(info->page, prod) = event;
+	wmb();			/* ensure ring contents visible */
+	info->page->out_prod = prod + 1;
+
+	notify_remote_via_evtchn(info->evtchn);
+}
+
+static int xenfb_queue_full(struct xenfb_info *info)
+{
+	__u32 cons, prod;
+
+	prod = info->page->out_prod;
+	cons = info->page->out_cons;
+	return prod - cons == XENFB_OUT_RING_LEN;
+}
+
+static void xenfb_update_screen(struct xenfb_info *info)
+{
+	int y1, y2, x1, x2;
+	struct list_head *item;
+	struct xenfb_mapping *map;
+
+	if (!(info->flags & XENFB_FLAG_UPDATE))
+		return;
+	if (xenfb_queue_full(info))

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 119

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+		return;
+
+	y1 = info->y1;
+	y2 = info->y2;
+	x1 = info->x1;
+	x2 = info->x2;
+	info->y1 = info->y2 = info->x1 = info->x2 = 0;
+	down(&info->mm_lock);
+	list_for_each(item, &info->mappings) {
+		map = list_entry(item, struct xenfb_mapping, next);

list_for_each_entry, perhaps?

Quoted 42 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +42 -0
+		if (!map->faults)
+			continue;
+		zap_page_range(map->vma, map->vma->vm_start,
+			       map->vma->vm_end - map->vma->vm_start, NULL);
+		map->faults = 0;
+	}
+	up(&info->mm_lock);
+
+	xenfb_do_update(info, x1, y1, x2 - x1, y2 - y1);
+}
+
+static int xenfb_thread(void *data)
+{
+	struct xenfb_info *info = data;
+
+	for (;;) {
+		if (kthread_should_stop())
+			break;
+		if (info->dirty) {
+			info->dirty = 0;
+			xenfb_update_screen(info);
+		}
+		wait_event_interruptible(info->wq,
+			kthread_should_stop() || info->dirty);
+	}
+	return 0;
+}
+
+static int xenfb_setcolreg(unsigned regno, unsigned red, unsigned green,
+			   unsigned blue, unsigned transp,
+			   struct fb_info *info)
+{
+	u32 v;
+
+	if (regno > info->cmap.len)
+		return 1;
+
+	red   >>= (16 - info->var.red.length);
+	green >>= (16 - info->var.green.length);
+	blue  >>= (16 - info->var.blue.length);
+
+	v = (red << info->var.red.offset) |

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 171

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+	    (green << info->var.green.offset) |
+	    (blue << info->var.blue.offset);
+
+	switch (info->var.bits_per_pixel) {
+	case 16:
+	case 24:
+	case 32:
+		((u32 *)info->pseudo_palette)[regno] = v;
+		break;
+	}

That looks exciting. What is this code trying to achieve?

There are comments in xxxfb_setcolreg in skeletonfb to the effect that pseudo_palette should only get played with if fix.visual is TRUECOLOR or DIRECTCOLOR, and a specific warning against trying to do stuff with bits_per_pixel here.

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 181

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+	
+	return 0;
+}
+
+static void xenfb_timer(unsigned long data)
+{
+	struct xenfb_info *info = (struct xenfb_info *)data;
+	info->dirty = 1;
+	wake_up(&info->wq);
+}

From last time:

Quoted 6 lines
Quoted 3 lines

Could this end up running at the same time as update_screen and race to update dirty? I suppose it doesn't actually matter too much if it does.

xenfb_timer() runs from the timer interrupt, sets dirty, then kicks the wait queue.

Quoted 8 lines

A separate kernel thread running xenfb_thread() sleeps on the wait queue. When it wakes up, and dirty is set, it clears dirty and updates the screen. Then goes back to sleep.

Obviously, dirty is always set when the thread wakes up. If xenfb_timer() runs before the thread goes to sleep again, the wakeup is lost, and the value of dirty doesn't matter. I believe that's okay.

It looks to me like we can lose updates to the screen in that case:

xenfb_timer runs, sets dirty to 1, kicks the thread
xenfb_thread wakes up xenfb_thread reads the update region
another update happens, extends the update region, reschedules xenfb_timer xenfb_timer runs again, sets dirty to 2, kicks the thread again
xenfb_thread sets dirty to 0 xenfb_thread sends an update using
the old region

I suppose the race is sufficiently unlikely and sufficiently mild that it can probably be ignored for all realistic purposes.

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 184

Quoted 3 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +3 -0
+
+static void xenfb_refresh(struct xenfb_info *info,
+			  int x1, int y1, int w, int h)

Is there any reason why there can't be multiple calls to this function live at the same time? In particular, if vm_nopage and xenfb_copyarea happen at the same time it looks like we can lose part of the update area.

Also, vm_nopage holds the mm_lock when it calls this, whereas the other callers appear not to. Was this deliberate?

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 197

Quoted 13 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +13 -0
+{
+	int y2, x2;
+
+	y2 = y1 + h;
+	x2 = x1 + w;
+	if (info->y2 == 0) {
+		info->y1 = y1;
+		info->y2 = y2;
+	}
+	if (info->x2 == 0) {
+		info->x1 = x1;
+		info->x2 = x2;
+	}

Presumably, these are just looking to see if there's a pre-existing region, and if not copying the new region across verbatim? Could you test dirty instead in that case?

Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +4 -0
+
+	if (info->y1 > y1)
+		info->y1 = y1;
+	if (info->y2 < y2)

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 211

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+		info->y2 = y2;
+	if (info->x1 > x1)
+		info->x1 = x1;
+	if (info->x2 < x2)
+		info->x2 = x2;
+
+	if (timer_pending(&info->refresh))
+		return;
+
+	mod_timer(&info->refresh, jiffies + HZ/xenfb_fps);

Would it be sensible to have a fallback so that an update is always sent to the backend e.g. 100ms after any change to the framebuffer, so that the user still sees something when the display is being updated very frequently?

Quoted 97 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +97 -0
+}
+
+static void xenfb_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
+{
+	struct xenfb_info *info = p->par;
+
+	cfb_fillrect(p, rect);
+	xenfb_refresh(info, rect->dx, rect->dy, rect->width, rect->height);
+}
+
+static void xenfb_imageblit(struct fb_info *p, const struct fb_image *image)
+{
+	struct xenfb_info *info = p->par;
+
+	cfb_imageblit(p, image);
+	xenfb_refresh(info, image->dx, image->dy, image->width, image->height);
+}
+
+static void xenfb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
+{
+	struct xenfb_info *info = p->par;
+
+	cfb_copyarea(p, area);
+	xenfb_refresh(info, area->dx, area->dy, area->width, area->height);
+}
+
+static void xenfb_vm_open(struct vm_area_struct *vma)
+{
+	struct xenfb_mapping *map = vma->vm_private_data;
+	atomic_inc(&map->map_refs);
+}
+
+static void xenfb_vm_close(struct vm_area_struct *vma)
+{
+	struct xenfb_mapping *map = vma->vm_private_data;
+	struct xenfb_info *info = map->info;
+
+	down(&info->mm_lock);
+	if (atomic_dec_and_test(&map->map_refs)) {
+		list_del(&map->next);
+		kfree(map);
+	}
+	up(&info->mm_lock);
+}
+
+static struct page *xenfb_vm_nopage(struct vm_area_struct *vma,
+				    unsigned long vaddr, int *type)
+{
+	struct xenfb_mapping *map = vma->vm_private_data;
+	struct xenfb_info *info = map->info;
+	int pgnr = (vaddr - vma->vm_start) >> PAGE_SHIFT;
+	struct page *page;
+	int y1, y2;
+
+	if (pgnr >= info->nr_pages)
+		return NOPAGE_SIGBUS;
+
+	down(&info->mm_lock);
+	page = info->pages[pgnr];
+	get_page(page);
+	map->faults++;
+
+	y1 = pgnr * PAGE_SIZE / info->fix->line_length;
+	y2 = (pgnr * PAGE_SIZE + PAGE_SIZE - 1) / info->fix->line_length;
+	if (y2 > info->var->yres)
+		y2 = info->var->yres;
+	xenfb_refresh(info, 0, y1, info->var->xres, y2 - y1);
+	up(&info->mm_lock);
+
+	if (type)
+		*type = VM_FAULT_MINOR;
+
+	return page;
+}
+
+static struct vm_operations_struct xenfb_vm_ops = {
+	.open	= xenfb_vm_open,
+	.close	= xenfb_vm_close,
+	.nopage	= xenfb_vm_nopage,
+};
+
+static int xenfb_mmap(struct fb_info *fb_info, struct vm_area_struct *vma)
+{
+	struct xenfb_info *info = fb_info->par;
+	struct xenfb_mapping *map;
+	int ret;
+	int map_pages;
+
+	down(&info->mm_lock);
+
+	ret = -EINVAL;
+	if (!(vma->vm_flags & VM_WRITE))
+		goto out;
+	if (!(vma->vm_flags & VM_SHARED))
+		goto out;
+	if (vma->vm_pgoff != 0)
+		goto out;

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 318

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+
+	map_pages = (vma->vm_end - vma->vm_start + PAGE_SIZE-1) >> PAGE_SHIFT;
+	if (map_pages > info->nr_pages)
+		goto out;
+
+	ret = -ENOMEM;
+	map = kmalloc(sizeof(*map), GFP_KERNEL);
+	if (map == NULL)
+		goto out;
+	memset(map, 0, sizeof(*map));

kzalloc, maybe?

Quoted 43 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +43 -0
+
+	map->vma = vma;
+	map->faults = 0;
+	map->info = info;
+	atomic_set(&map->map_refs, 1);
+	list_add(&map->next, &info->mappings);
+	vma->vm_ops = &xenfb_vm_ops;
+	vma->vm_flags |= (VM_DONTEXPAND | VM_RESERVED);
+	vma->vm_private_data = map;
+	ret = 0;
+
+ out:
+	up(&info->mm_lock);
+	return ret;
+}
+
+static struct fb_ops xenfb_fb_ops = {
+	.owner		= THIS_MODULE,
+	.fb_setcolreg	= xenfb_setcolreg,
+	.fb_fillrect	= xenfb_fillrect,
+	.fb_copyarea	= xenfb_copyarea,
+	.fb_imageblit	= xenfb_imageblit,
+	.fb_mmap	= xenfb_mmap,
+};
+
+static irqreturn_t xenfb_event_handler(int rq, void *dev_id,
+				       struct pt_regs *regs)
+{
+	struct xenfb_info *info = dev_id;
+	__u32 cons, prod;
+
+	prod = info->page->in_prod;
+	rmb();			/* ensure we see ring contents up to prod */
+	for (cons = info->page->in_cons; cons != prod; cons++) {
+		union xenfb_in_event *event;
+		event = &XENFB_IN_RING_REF(info->page, cons);
+
+		switch (event->type) {
+		case XENFB_TYPE_SET_EVENTS:
+			info->flags = event->set_events.flags;
+			break;
+		}
+	}

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 371

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+	mb();			/* ensure we're done with ring contents */
+	info->page->in_cons = cons;
+	notify_remote_via_evtchn(info->evtchn);
+
+	return IRQ_HANDLED;
+}
+
+static unsigned long vmalloc_to_mfn(void *address)
+{
+	// FIXME was return pfn_to_mfn(vmalloc_to_pfn(address));

Not quite sure why this comment is here.

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 377

Quoted 6 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +6 -0
+	return arbitrary_virt_to_machine(address) >> PAGE_SHIFT;
+}
+
+static struct xenfb_info *xenfb_info;
+
+static int __init xenfb_probe(void)

I'd find it easier to be confident of the error handling here if the function were split up a little.

Quoted 8 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +8 -0
+{
+	struct xenfb_info *info;
+	int i, ret;
+	struct fb_info *fb_info;
+	struct evtchn_alloc_unbound alloc_unbound;
+	struct evtchn_close close;
+	struct xenbus_transaction xbt;
+

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 395

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+        /* Nothing to do if running in dom0. */
+        if (is_initial_xendomain())
+		return -ENODEV;
+#if 0
+        /* if we're not set up to use graphics mode, then don't initialize */
+	if (xenbus_scanf(XBT_NIL, "console", "use_graphics", "%d", &ret) < 0)
+		return -ENODEV;
+	if (ret == 0)
+		return -ENODEV;
+#endif

That's interesting. What is this here for?

Quoted 11 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +11 -0
+
+	info = kmalloc(sizeof(*info), GFP_KERNEL);
+	if (info == NULL)
+		return -ENOMEM;
+	memset(info, 0, sizeof(*info));
+
+	INIT_LIST_HEAD(&info->mappings);
+
+	info->fb = vmalloc(xenfb_mem_len);
+	if (info->fb == NULL)
+		goto error;

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 416

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+	memset(info->fb, 0, xenfb_mem_len);
+	info->nr_pages = (xenfb_mem_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
+	info->pages = kmalloc(sizeof(struct page*)*info->nr_pages, GFP_KERNEL);
+	if (info->pages == NULL)
+		goto error_vfree;
+	for (i = 0; i < info->nr_pages; i++)
+		info->pages[i] = vmalloc_to_page(info->fb + i * PAGE_SIZE);
+
+	fb_info = framebuffer_alloc(sizeof(u32) * 256, NULL);
+	// FIXME sizeof(struct xenfb_info)

Where did sizeof(u32) * 256 come from? What does the comment mean?

fb_info isn't released from the error path.

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 420

Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +4 -0
+	if (fb_info == NULL)
+		goto error_kfree;
+
+	info->mfns = vmalloc(sizeof(unsigned long) * info->nr_pages);

What happens if this fails? Also, you never seem to vfree this if we hit an error later on.

Quoted 6 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +6 -0
+	/* set up shared page */
+	info->page = (void *)__get_free_page(GFP_KERNEL);
+	if (!info->page)
+		goto error_kfree;
+	/* set up event channel */
+	alloc_unbound.dom = DOMID_SELF;

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 436

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+	alloc_unbound.remote_dom = 0;
+	ret = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
+					  &alloc_unbound);
+	if (ret)
+		goto error_freep;
+	info->evtchn = alloc_unbound.port;
+
+	for (i = 0; i < info->nr_pages; i++)
+		info->mfns[i] = vmalloc_to_mfn(info->fb + i * PAGE_SIZE);
+	info->page->pd[0] = vmalloc_to_mfn(info->mfns);

What happens if this domain is running in shadow translate mode? It looks to me like the backend will end up mapping completely the wrong frame.

Quoted 16 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +16 -0
+	info->page->pd[1] = 0;
+	info->page->width = XENFB_WIDTH;
+	info->page->height = XENFB_HEIGHT;
+	info->page->depth = XENFB_DEPTH;
+	info->page->line_length = (info->page->depth / 8) * info->page->width;
+	info->page->mem_length = xenfb_mem_len;
+	info->page->in_cons = info->page->in_prod = 0;
+	info->page->out_cons = info->page->out_prod = 0;
+
+	ret = bind_evtchn_to_irqhandler(info->evtchn, xenfb_event_handler,
+					0, "xenfb", info);
+	if (ret < 0) {
+		close.port = info->evtchn;
+		HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
+		goto error_freep;
+	}

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 462

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+
+	info->irq = ret;
+	xenfb_info = info;
+
+	fb_info->pseudo_palette = fb_info->par;
+	fb_info->par = info;
+	fb_info->screen_base = info->fb;
+
+	memset(&fb_info->var, 0, sizeof(fb_info->var));
+	memset(&fb_info->fix, 0, sizeof(fb_info->fix));

I think these are already guaranteed to be zero, aren't they?

Quoted 16 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +16 -0
+
+	fb_info->fbops = &xenfb_fb_ops;
+	fb_info->var.xres_virtual = fb_info->var.xres = info->page->width;
+	fb_info->var.yres_virtual = fb_info->var.yres = info->page->height;
+	fb_info->var.bits_per_pixel = info->page->depth;
+
+	fb_info->var.red = (struct fb_bitfield){16, 8, 0};
+	fb_info->var.green = (struct fb_bitfield){8, 8, 0};
+	fb_info->var.blue = (struct fb_bitfield){0, 8, 0};
+
+	fb_info->var.activate = FB_ACTIVATE_NOW;
+	fb_info->var.height = -1;
+	fb_info->var.width = -1;
+	fb_info->var.vmode = FB_VMODE_NONINTERLACED;
+
+	fb_info->fix.visual = FB_VISUAL_TRUECOLOR;

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 488

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+	fb_info->fix.line_length = info->page->line_length;
+	fb_info->fix.smem_start = 0;
+	fb_info->fix.smem_len = xenfb_mem_len;
+	strcpy(fb_info->fix.id, "xen");
+	fb_info->fix.type = FB_TYPE_PACKED_PIXELS;
+	fb_info->fix.accel = FB_ACCEL_NONE;
+
+	fb_info->flags = FBINFO_FLAG_DEFAULT;
+
+	fb_alloc_cmap(&fb_info->cmap, 256, 0);

This can fail, and the error paths don't bother to deallocate it.

Quoted 16 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +16 -0
+
+	info->fb_info = fb_info;
+	info->fix = &fb_info->fix;
+	info->var = &fb_info->var;
+
+	init_MUTEX(&info->mm_lock);
+	init_waitqueue_head(&info->wq);
+	init_timer(&info->refresh);
+	info->refresh.function = xenfb_timer;
+	info->refresh.data = (unsigned long)info;
+
+	info->kthread = kthread_run(xenfb_thread, info, "xenfb thread");
+	if (IS_ERR(info->kthread))
+		goto error_unbind;
+
+	ret = register_framebuffer(fb_info);

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 514

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+	if (ret)
+		goto error_unbind;
+
+ again:
+	ret = xenbus_transaction_start(&xbt);
+	if (ret)
+		goto error_unreg;
+	ret = xenbus_printf(xbt, "vfb", "page-ref", "%lu",
+			    virt_to_mfn(info->page));
+	// FIXME grant tables?

Again, what happens in shadow translate mode?

Also, it's kind of annoying that there's no support for multiple framebuffers in a domU. That's very much a wishlist thing, though.

Quoted 12 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +12 -0
+	if (ret)
+		goto error_xenbus;
+	ret = xenbus_printf(xbt, "vfb", "event-channel", "%u",
+			    info->evtchn);
+	if (ret)
+		goto error_xenbus;
+	ret = xenbus_transaction_end(xbt, 0);
+	if (ret) {
+		if (ret == -EAGAIN)
+			goto again;
+		goto error_unreg;
+	}

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 536

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+
+	return 0;
+
+ error_xenbus:
+	xenbus_transaction_end(xbt, 1);
+ error_unreg:
+	unregister_framebuffer(fb_info);
+ error_unbind:
+	unbind_from_irqhandler(info->irq, info);
+	// FIXME do we have to stop info->kthread?

If you've started it you certainly need to stop it.

Quoted 6 lines

+ error_freep:

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+	free_page((unsigned long)info->page);
+ error_kfree:
+	kfree(info->pages);
+ error_vfree:
+	vfree(info->fb);

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 552

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+ error:
+	kfree(info);
+	xenfb_info = NULL;
+
+	return -ENODEV;
+}
+
+static int __init xenfb_init(void)
+{
+	return xenfb_probe();

From last time:

Quoted 3 lines
Quoted 1 line

You might want to consider using xenbus_device and friends here.

I considered that, but it wasn't obvious to me how to make it fit cleanly to a user-space backend. Care to advise?

I think if the frontend needs to know whether the backend is in kernel or userspace then we've messed up somewhere. Why do you expect this to be difficult?

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 565

Quoted 13 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +13 -0
+}
+
+static void __exit xenfb_cleanup(void)
+{
+	struct xenfb_info *info = xenfb_info;
+
+	unregister_framebuffer(info->fb_info);
+	unbind_from_irqhandler(info->irq, info);
+	free_page((unsigned long)info->page);
+	kfree(info->pages);
+	vfree(info->fb);
+	kfree(info);
+	xenfb_info = NULL;

I don't think this releases all of the memory you allocate in xenfb_probe.

Quoted 94 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +6 -0
+}
+
+module_init(xenfb_init);
+module_exit(xenfb_cleanup);
+
+MODULE_LICENSE("GPL");

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/xenkbd/Makefile

linux-2.6-xen-sparse/drivers/xen/xenkbd/Makefile new file +1 -0
@@ -0,0 +1,1 @@
+obj-$(CONFIG_XEN_KEYBOARD)	+= xenkbd.o

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c

linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +79 -0
@@ -0,0 +1,178 @@
+/*
+ * linux/drivers/input/keyboard/xenkbd.c -- Xen para-virtual input device
+ *
+ * Copyright (C) 2005
+ *
+ *      Anthony Liguori <[email protected]>
+ *
+ *  Based on linux/drivers/input/mouse/sermouse.c
+ *
+ *  This file is subject to the terms and conditions of the GNU General Public
+ *  License. See the file COPYING in the main directory of this archive for
+ *  more details.
+ */
+
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/module.h>
+#include <linux/input.h>
+#include <asm/hypervisor.h>
+#include <xen/evtchn.h>
+#include <xen/xenbus.h>
+#include <linux/xenkbd.h>
+
+struct xenkbd_device
+{
+	struct input_dev *dev;
+	struct xenkbd_info *info;
+	unsigned evtchn;
+	int irq;
+};
+
+static irqreturn_t input_handler(int rq, void *dev_id, struct pt_regs *regs)
+{
+	struct xenkbd_device *dev = dev_id;
+	struct xenkbd_info *info = dev ? dev->info : 0;
+	static int button_map[3] = { BTN_RIGHT, BTN_MIDDLE, BTN_LEFT };
+	__u32 cons, prod;
+
+	prod = info->in_prod;
+	rmb();			/* ensure we see ring contents up to prod */
+	for (cons = info->in_cons; cons != prod; cons++) {
+		union xenkbd_in_event *event;
+		event = &XENKBD_IN_RING_REF(info, cons);
+	
+		switch (event->type) {
+		case XENKBD_TYPE_MOTION:
+			input_report_rel(dev->dev, REL_X, event->motion.rel_x);
+			input_report_rel(dev->dev, REL_Y, event->motion.rel_y);
+			break;
+		case XENKBD_TYPE_BUTTON:
+			if (event->button.button < 3)
+				input_report_key(dev->dev,
+						 button_map[event->button.button],
+						 event->button.pressed);
+			break;
+		case XENKBD_TYPE_KEY:
+			input_report_key(dev->dev, event->key.keycode, event->key.pressed);
+			break;
+		}
+	}
+	input_sync(dev->dev);
+	mb();			/* ensure we got ring contents */
+	info->in_cons = cons;
+	notify_remote_via_evtchn(dev->evtchn);
+
+	return IRQ_HANDLED;
+}
+
+static struct xenkbd_device *xenkbd_dev;
+
+int __init xenkbd_init(void)
+{
+	int ret = 0;
+	int i;
+	struct xenkbd_device *dev;
+	struct input_dev *input_dev;
+	struct evtchn_alloc_unbound alloc_unbound;
+	struct xenbus_transaction xbt;
+

linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c line 89

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +10 -0
+        /* Nothing to do if running in dom0. */
+        if (is_initial_xendomain())
+	  return -ENODEV;
+#if 0
+        /* if we're not set up to use graphics mode, then don't initialize */
+	if (xenbus_scanf(XBT_NIL, "console", "use_graphics", "%d", &ret) < 0)
+	  return -ENODEV;
+	if (ret == 0)
+	  return -ENODEV;
+#endif

Not sure about this.

linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c line 94

Quoted 5 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +5 -0
+
+	dev = kmalloc(sizeof(*dev), GFP_KERNEL);
+	input_dev = input_allocate_device();
+	if (!dev || !input_dev)
+		return -ENOMEM;

You potentially leak memory if one of these fails and the other succeeds.

Also, the error paths don't release input_dev. In fact, nothing ever releases input_dev, unless I'm missing something.

Quoted 9 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +9 -0
+
+	dev->dev = input_dev;
+	dev->info = (void *)__get_free_page(GFP_KERNEL);
+	if (!dev->info) {
+		ret = -ENOMEM;
+		goto error;
+	}
+
+	alloc_unbound.dom = DOMID_SELF;

linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c line 113

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +10 -0
+	alloc_unbound.remote_dom = 0;
+	ret = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
+					  &alloc_unbound);
+	if (ret)
+		goto error_freep;
+	dev->evtchn = alloc_unbound.port;
+	ret = bind_evtchn_to_irqhandler(dev->evtchn, input_handler, 0,
+					"xenkbd", dev);
+	if (ret < 0)
+		goto error_freep;

You might want to close the event channel here.

linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c line 122

Quoted 9 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +9 -0
+	dev->irq = ret;
+
+	input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
+	input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT);
+	input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
+
+	/* FIXME not sure this is quite right */
+	for (i = 0; i < 256; i++)
+		set_bit(i, input_dev->keybit);

It looks reasonable, but perhaps memset would be a better choice.

linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c line 126

Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +4 -0
+
+	input_dev->name = "Xen Virtual Keyboard/Mouse";
+
+	input_register_device(input_dev);

This can fail.

linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c line 133

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +7 -0
+
+ again:
+	ret = xenbus_transaction_start(&xbt);
+	if (ret)
+		goto error_unreg;
+	ret = xenbus_printf(xbt, "vkbd", "page-ref", "%lu",
+			    virt_to_mfn(dev->info));

There's no reason that I can see not to use grant tables here, and that'd avoid all of the potential issues with shadow translate mode.

linux-2.6-xen-sparse/include/linux/xenfb.h

Quoted 48 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +45 -0
+	if (ret)
+		goto error_xenbus;
+	ret = xenbus_printf(xbt, "vkbd", "event-channel", "%u",
+			    dev->evtchn);
+	if (ret)
+		goto error_xenbus;
+	ret = xenbus_transaction_end(xbt, 0);
+	if (ret) {
+		if (ret == -EAGAIN)
+			goto again;
+		goto error_unreg;
+	}
+
+	dev->info->in_cons = dev->info->in_prod = 0;
+	dev->info->out_cons = dev->info->out_prod = 0;
+	xenkbd_dev = dev;
+
+	return ret;
+
+	
+ error_xenbus:
+	xenbus_transaction_end(xbt, 1);
+ error_unreg:
+	input_unregister_device(input_dev);
+	unbind_from_irqhandler(dev->irq, dev);
+ error_freep:
+	free_page((unsigned long)dev->info);
+ error:
+	kfree(dev);
+	return ret;
+}
+
+static void __exit xenkbd_cleanup(void)
+{
+	input_unregister_device(xenkbd_dev->dev);
+	unbind_from_irqhandler(xenkbd_dev->irq, xenkbd_dev);
+	free_page((unsigned long)xenkbd_dev->info);
+	kfree(xenkbd_dev);
+	xenkbd_dev = NULL;
+}
+
+module_init(xenkbd_init);
+module_exit(xenkbd_cleanup);
+
+MODULE_LICENSE("GPL");

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/include/linux/xenfb.h

linux-2.6-xen-sparse/include/linux/xenfb.h new file +0 -0

This is defining an io protocol, so belongs in xen/include/public/io

Quoted 15 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +14 -0
@@ -0,0 +1,108 @@
+/*
+ * linux/include/linux/xenfb.h -- Xen virtual frame buffer device
+ *
+ * Copyright (C) 2005
+ *
+ *      Anthony Liguori <[email protected]>
+ *
+ *  This file is subject to the terms and conditions of the GNU General Public
+ *  License. See the file COPYING in the main directory of this archive for
+ *  more details.
+ */
+
+#ifndef _LINUX_XENFB_H
+#define _LINUX_XENFB_H

linux-2.6-xen-sparse/include/linux/xenfb.h line 24

Quoted 10 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +10 -0
+
+#include <asm/types.h>
+
+/* out events */
+
+#define XENFB_OUT_EVENT_SIZE 40
+
+#define XENFB_TYPE_MOTION 1
+#define XENFB_TYPE_UPDATE 2
+

The other tagged unions in Xen look more like this:

struct {
	u8 type;
	union {
		struct {
			u32 field1;
			u32 field2;
		} type1;
		struct {
			u16 field1;
		} type2;
	} u;
};

I'd find this a lot easier to read than the current approach of putting the type into the individual event type structures. Is there any reason why this couldn't be used here?

linux-2.6-xen-sparse/include/linux/xenfb.h line 30

Quoted 6 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +6 -0
+struct xenfb_motion	    /* currently unused */
+{
+	__u8 type;          /* XENFB_TYPE_MOTION */
+	__u16 x;            /* The new x coordinate */
+	__u16 y;            /* The new y coordinate */
+};

I think if the comment said what was moving this'd be a lot clearer.

Quoted 6 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +6 -0
+
+struct xenfb_update
+{
+	__u8 type;          /* XENFB_TYPE_UPDATE */
+	__u16 x;            /* source x */
+	__u16 y;            /* source y */

linux-2.6-xen-sparse/include/linux/xenfb.h line 46

Quoted 10 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +10 -0
+	__u16 width;        /* rect width */
+	__u16 height;       /* rect height */
+};
+
+union xenfb_out_event
+{
+	__u8 type;
+	struct xenfb_motion motion;
+	struct xenfb_update update;
+	char _[XENFB_OUT_EVENT_SIZE];

Perhaps ``pad'' would be clearer than ``_''?

Quoted 44 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +44 -0
+};
+
+/* in events */
+
+#define XENFB_IN_EVENT_SIZE 40
+
+#define XENFB_TYPE_SET_EVENTS 1
+
+#define XENFB_FLAG_MOTION 1
+#define XENFB_FLAG_UPDATE 2
+#define XENFB_FLAG_COPY 4
+#define XENFB_FLAG_FILL 8
+
+struct xenfb_set_events
+{
+	__u8 type;          /* XENFB_TYPE_SET_EVENTS */
+	__u32 flags;        /* combination of XENFB_FLAG_* */
+};
+
+union xenfb_in_event
+{
+	__u8 type;
+	struct xenfb_set_events set_events;
+	char _[XENFB_OUT_EVENT_SIZE];
+};
+
+/* shared page */
+
+#define XENFB_IN_RING_SIZE 1024
+#define XENFB_IN_RING_LEN (XENFB_IN_RING_SIZE / XENFB_IN_EVENT_SIZE)
+#define XENFB_IN_RING_OFFS 1024
+#define XENFB_IN_RING(page) \
+    ((union xenfb_in_event *)((char *)(page) + XENFB_IN_RING_OFFS))
+#define XENFB_IN_RING_REF(page, idx) \
+    (XENFB_IN_RING((page))[(idx) % XENFB_IN_RING_LEN])
+
+#define XENFB_OUT_RING_SIZE 2048
+#define XENFB_OUT_RING_LEN (XENFB_OUT_RING_SIZE / XENFB_OUT_EVENT_SIZE)
+#define XENFB_OUT_RING_OFFS (XENFB_IN_RING_OFFS + XENFB_IN_RING_SIZE)
+#define XENFB_OUT_RING(page) \
+    ((union xenfb_out_event *)((char *)(page) + XENFB_OUT_RING_OFFS))
+#define XENFB_OUT_RING_REF(page, idx) \
+    (XENFB_OUT_RING((page))[(idx) % XENFB_OUT_RING_LEN])
+

linux-2.6-xen-sparse/include/linux/xenfb.h line 100

Quoted 10 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +10 -0
+struct xenfb_page
+{
+	__u16 width;         /* the width of the framebuffer (in pixels) */
+	__u16 height;        /* the height of the framebuffer (in pixels) */
+	__u32 line_length;   /* the length of a row of pixels (in bytes) */
+	__u32 mem_length;    /* the length of the framebuffer (in bytes) */
+	__u8 depth;          /* the depth of a pixel (in bits) */
+
+	unsigned long pd[2];	/* FIXME rename to pgdir? */
+	/* FIXME pd[1] unused at this time, shrink? */

I think it'd be enough to just give some indication of where the magic number 2 came from.

From last time:

Quoted 7 lines
Quoted 3 lines

Is it really a good idea for these to be on the shared page? What happens if the {front,back}end is malicious and modifies them while the framebuffer is operating normally?

Obviously, both frontend and backend should be robust against bad shared page contents. The shared page is writable by both, because both need to step the rings.

Quoted 7 lines

The members above are for information flowing from frontend to backend. For safety, they should be write-only in the frontend, and the backend should copy the info to a safer place, checking its consistency.

I can improve safety as sketched. Feel free to suggest a better way to transmit the same information.

We're going to eventually need some way for the frontend to tell the backend to change the display resolution and/or depth. Could this be used to select the initial resolution? I'm not sure what the plan is here, but I would guess something like this:

1) Framebuffer is operating normally, with the frontend modifying the buffer in memory and generating update events in the usual way.

2) Frontend decides it wants a resolution change. Linux is made to stop updating the buffer. Not quite sure how to do this, but it seems like something which most real framebuffers would need to do during a resolution change. The frontend sends a start_change_res message to the backend with the desired new resolution.

3) The backend finishes off any pending updates, and picks up the start_change_res message. It can then unmap the buffer and send back a change_res_goahead message.

4) The frontend picks up the change_res_goahead message. It can now release the buffer area and allocate a new one of the correct size, and send a change_res_complete message to the correct size, and send a change_res_complete message to the backend. Once that's sent the frontend can go back to normal operation at the new resolution.

5) The backend picks up the change_res_complete message, remaps the buffer, and returns to normal operation.

change_res_complete is actually redundant, but will simplify the backend enough that it's probably worthwhile.

If you use a protocol like this, then you could have the frontend send a start_change_res as the first thing after it starts up, and communicate the information that way. This also gives you some scope for negotiating a lower res if the backend can't cope with the requested one for some reason.

The other way would be to do this through xenbus, but synchronising with the data path would be a bit of a pain.

linux-2.6-xen-sparse/include/linux/xenfb.h line 106

Quoted 6 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +6 -0
+
+	__u32 in_cons, in_prod;
+	__u32 out_cons, out_prod;
+};
+
+void xenfb_resume(void);

No longer exists.

linux-2.6-xen-sparse/include/linux/xenkbd.h

Quoted 5 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +2 -0
+
+#endif

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/include/linux/xenkbd.h

linux-2.6-xen-sparse/include/linux/xenkbd.h new file +0 -0

Again, this belongs in xen/include/public/io.

Quoted 22 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +21 -0
@@ -0,0 +1,92 @@
+/*
+ * linux/include/linux/xenkbd.h -- Xen virtual keyboard/mouse
+ *
+ * Copyright (C) 2005
+ *
+ *      Anthony Liguori <[email protected]>
+ *
+ *  This file is subject to the terms and conditions of the GNU General Public
+ *  License. See the file COPYING in the main directory of this archive for
+ *  more details.
+ */
+
+#ifndef _LINUX_XENKBD_H
+#define _LINUX_XENKBD_H
+
+#include <asm/types.h>
+
+/* in events */
+
+#define XENKBD_IN_EVENT_SIZE 40
+

linux-2.6-xen-sparse/include/linux/xenkbd.h line 31

Quoted 10 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +10 -0
+#define XENKBD_TYPE_MOTION  1     /* mouse movement event */
+#define XENKBD_TYPE_BUTTON  2     /* mouse button event */
+#define XENKBD_TYPE_KEY     3     /* keyboard event */
+
+struct xenkbd_motion
+{
+	__u8 type;         /* XENKBD_TYPE_MOTION */
+	__s16 rel_x;       /* relative X motion */
+	__s16 rel_y;       /* relative Y motion */
+};

Again, this is a slightly ugly way of doing tagged unions.

Quoted 15 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +15 -0
+
+struct xenkbd_button
+{
+	__u8 type;         /* XENKBD_TYPE_BUTTON */
+	__u8 pressed;      /* 1 if pressed; 0 otherwise */
+	__u8 button;       /* the button (0, 1, 2 is right, middle, left) */
+};
+
+struct xenkbd_key
+{
+	__u8 type;         /* XENKBD_TYPE_KEY */
+	__u8 pressed;      /* 1 if pressed; 0 otherwise */
+	__u16 keycode;     /* KEY_* from linux/input.h */
+};
+

linux-2.6-xen-sparse/include/linux/xenkbd.h line 56

Quoted 10 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +10 -0
+union xenkbd_in_event
+{
+	__u8 type;
+	struct xenkbd_motion motion;
+	struct xenkbd_button button;
+	struct xenkbd_key key;
+	char _[XENKBD_IN_EVENT_SIZE];
+};
+
+/* out events */

Maybe a comment that none are currently defined?

Quoted 24 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +24 -0
+
+#define XENKBD_OUT_EVENT_SIZE 40
+
+union xenkbd_out_event
+{
+	__u8 type;
+	char _[XENKBD_OUT_EVENT_SIZE];
+};
+
+/* shared page */
+
+#define XENKBD_IN_RING_SIZE 2048
+#define XENKBD_IN_RING_LEN (XENKBD_IN_RING_SIZE / XENKBD_IN_EVENT_SIZE)
+#define XENKBD_IN_RING_OFFS 1024
+#define XENKBD_IN_RING(page) \
+    ((union xenkbd_in_event *)((char *)(page) + XENKBD_IN_RING_OFFS))
+#define XENKBD_IN_RING_REF(page, idx) \
+    (XENKBD_IN_RING((page))[(idx) % XENKBD_IN_RING_LEN])
+
+#define XENKBD_OUT_RING_SIZE 1024
+#define XENKBD_OUT_RING_LEN (XENKBD_OUT_RING_SIZE / XENKBD_OUT_EVENT_SIZE)
+#define XENKBD_OUT_RING_OFFS (XENKBD_IN_RING_OFFS + XENKBD_IN_RING_SIZE)
+#define XENKBD_OUT_RING(page) \
+    ((union xenkbd_out_event *)((char *)(page) + XENKBD_OUT_RING_OFFS))

linux-2.6-xen-sparse/include/linux/xenkbd.h line 90

Quoted 10 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +10 -0
+#define XENKBD_OUT_RING_REF(page, idx) \
+    (XENKBD_OUT_RING((page))[(idx) % XENKBD_OUT_RING_LEN])
+
+struct xenkbd_info
+{
+	__u32 in_cons, in_prod;
+	__u32 out_cons, out_prod;
+};
+
+void xenkbd_resume(void);

No longer exists.

linux-2.6-xen-sparse/include/linux/xenkbd.h line 92

Quoted 2 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +2 -0
+
+#endif

Steven.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#2 lore raw
Quoted 3 lines · Steven Smith

You could also put in a quick hack of just translating the mfns in the backend if the frontend is in shadow translate mode, but that's not really very nice.

My aim is to have all translation done in Xen if a guest is auto-translated, even if the mappings are created by a foreign domain (e.g., dom0). Maybe this is the showstopper that should drive us to make this change. As noted already, this would also be a simplification and robustification for the control libraries (which kludge their own P2M translation with xc_get_pfn_list).

-- Keir

#3 lore raw
In reply to Steven Smith
Quoted 3 lines · Steven Smith

There are still a couple of things from the last round which are unfixed. This is mostly my fault for going on holiday at an inconvenient time; sorry about that.

We won't begrudge you a holiday -- hope it was a good one :-) Leaving some of the stuff for Markus to reply to

Quoted 6 lines

The big problem with this is that you don't support shadow translate mode guests, because the frontend exposes mfns directly to the backend. The correct way of fixing this is to use grant tables, but, as Anthony pointed out, that's quite hard due to the amount of memory you need to grant access to.

Quoted 7 lines

The easy way of doing this would be to just increase the size of the grant tables. Alternatively, you could add support for granting access to ranges of memory, but that sounds like a whole lot of work.

You could also put in a quick hack of just translating the mfns in the backend if the frontend is in shadow translate mode, but that's not really very nice.

I wouldn't complain if we go the route Keir suggested and have Xen do it ;-) But if not, I think that given the size of the memory in question + grant tables, the quick hack is probably going to be the "right" thing for this case.

Quoted 9 lines
Quoted 9 lines

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c

linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c +2 -0
@@ -1866,8 +1866,12 @@ void __init setup_arch(char **cmdline_p)
 #endif
 #endif
 	} else {
+#if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
+		conswitchp = &dummy_con;
Quoted 8 lines

linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c void __init setup_arch(char **cmdline_p) · line 1876

Quoted 6 lines
linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c +2 -0
+#else
 		extern int console_use_vt;
 		console_use_vt = 0;
+#endif
 	}
 }

Not quite sure I understand what this is trying to achieve, or why it's related to the PV framebuffer stuff.

It makes it so that console switching is achieved by the dummy console and then when the fbcon driver is loaded, it takes over.

[snip]

Quoted 2 lines

Perhaps XEN_KEYBOARD should depend on XEN_FRAMEBUFFER, given that it doesn't work without it.

Yeah, seems reasonable enough -- I've added to my tree.

Quoted 14 lines
Quoted 14 lines
linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c +0 -0
 config XEN_SCRUB_PAGES
linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c +0 -0
 	bool "Scrub memory before freeing it to Xen"
 	default y

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/Makefile

linux-2.6-xen-sparse/drivers/xen/Makefile +2 -0
@@ -15,3 +15,5 @@ obj-$(CONFIG_XEN_NETDEV_FRONTEND)	+= net
 obj-$(CONFIG_XEN_NETDEV_FRONTEND)	+= netfront/
 obj-$(CONFIG_XEN_PCIDEV_BACKEND)	+= pciback/
 obj-$(CONFIG_XEN_PCIDEV_FRONTEND)	+= pcifront/
+obj-$(CONFIG_XEN_FRAMEBUFFER)		+= xenfb/
+obj-$(CONFIG_XEN_KEYBOARD)		+= xenkbd/

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/mm/memory.c
--- a/linux-2.6-xen-sparse/mm/memory.c Sat Sep 02 12:11:54 2006 +0100

Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/Makefile unsigned long zap_page_range(struct vm_a · line 884

Quoted 6 lines

+++ b/linux-2.6-xen-sparse/mm/memory.c Sat Sep 02 15:11:17 2006 -0400

linux-2.6-xen-sparse/drivers/xen/Makefile +1 -0
@@ -881,6 +881,7 @@ unsigned long zap_page_range(struct vm_a
 		tlb_finish_mmu(tlb, address, end);
 	return end;
 }
+EXPORT_SYMBOL(zap_page_range);

It's unfortunate that we have to modify an upstream file here, but I can't really see any alternative.

Well, if we don't allow the framebuffer to be modular then it doesn't have to be exported. But that also seems like the wrong answer :-)

Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/Makefile unsigned long zap_page_range(struct vm_a

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/Makefile +7 -0
+#if 0
+        /* if we're not set up to use graphics mode, then don't initialize */
+	if (xenbus_scanf(XBT_NIL, "console", "use_graphics", "%d", &ret) < 0)
+		return -ENODEV;
+	if (ret == 0)
+		return -ENODEV;
+#endif

That's interesting. What is this here for?

It looks like it got accidentally #if 0'd while merging patches. Whoops. Basically, if the guest isn't set up to use a graphical console, then we shouldn't initialize the devices.

Jeremy

#4 lore raw
Quoted 4 lines
Quoted 3 lines

There are still a couple of things from the last round which are unfixed. This is mostly my fault for going on holiday at an inconvenient time; sorry about that.

We won't begrudge you a holiday -- hope it was a good one :-)

Yes, thank you.

Quoted 10 lines
Quoted 10 lines

The big problem with this is that you don't support shadow translate mode guests, because the frontend exposes mfns directly to the backend. The correct way of fixing this is to use grant tables, but, as Anthony pointed out, that's quite hard due to the amount of memory you need to grant access to.

The easy way of doing this would be to just increase the size of the grant tables. Alternatively, you could add support for granting access to ranges of memory, but that sounds like a whole lot of work.

Quoted 7 lines
Quoted 3 lines

You could also put in a quick hack of just translating the mfns in the backend if the frontend is in shadow translate mode, but that's not really very nice.

I wouldn't complain if we go the route Keir suggested and have Xen do it ;-) But if not, I think that given the size of the memory in question + grant tables, the quick hack is probably going to be the "right" thing for this case.

Okay. Adding translations to the hypervisor isn't going to happen until after 3.0.3, so we should probably just leave this for a little while.

Quoted 17 lines
Quoted 9 lines
Quoted 9 lines

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c

linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c +2 -0
@@ -1866,8 +1866,12 @@ void __init setup_arch(char **cmdline_p)
 #endif
 #endif
 	} else {
+#if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
+		conswitchp = &dummy_con;
Quoted 8 lines

linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c void __init setup_arch(char **cmdline_p) · line 1876

Quoted 6 lines
linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c +2 -0
+#else
 		extern int console_use_vt;
 		console_use_vt = 0;
+#endif
 	}
 }

Not quite sure I understand what this is trying to achieve, or why it's related to the PV framebuffer stuff.

Quoted 2 lines

It makes it so that console switching is achieved by the dummy console and then when the fbcon driver is loaded, it takes over.

Okay.

Quoted 5 lines

[snip]

Quoted 2 lines

Perhaps XEN_KEYBOARD should depend on XEN_FRAMEBUFFER, given that it doesn't work without it.

Yeah, seems reasonable enough -- I've added to my tree.

Thanks.

Quoted 22 lines
Quoted 14 lines
Quoted 14 lines

config XEN_SCRUB_PAGES

linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c +0 -0
 	bool "Scrub memory before freeing it to Xen"
 	default y

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/Makefile

linux-2.6-xen-sparse/drivers/xen/Makefile +2 -0
@@ -15,3 +15,5 @@ obj-$(CONFIG_XEN_NETDEV_FRONTEND)	+= net
 obj-$(CONFIG_XEN_NETDEV_FRONTEND)	+= netfront/
 obj-$(CONFIG_XEN_PCIDEV_BACKEND)	+= pciback/
 obj-$(CONFIG_XEN_PCIDEV_FRONTEND)	+= pcifront/
+obj-$(CONFIG_XEN_FRAMEBUFFER)		+= xenfb/
+obj-$(CONFIG_XEN_KEYBOARD)		+= xenkbd/

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/mm/memory.c
--- a/linux-2.6-xen-sparse/mm/memory.c Sat Sep 02 12:11:54 2006 +0100

Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/Makefile unsigned long zap_page_range(struct vm_a · line 884

Quoted 6 lines

+++ b/linux-2.6-xen-sparse/mm/memory.c Sat Sep 02 15:11:17 2006 -0400

linux-2.6-xen-sparse/drivers/xen/Makefile +1 -0
@@ -881,6 +881,7 @@ unsigned long zap_page_range(struct vm_a
 		tlb_finish_mmu(tlb, address, end);
 	return end;
 }
+EXPORT_SYMBOL(zap_page_range);

It's unfortunate that we have to modify an upstream file here, but I can't really see any alternative.

Quoted 2 lines

Well, if we don't allow the framebuffer to be modular then it doesn't have to be exported. But that also seems like the wrong answer :-)

Yeah, adding the EXPORT_SYMBOL is the right thing to do here, it's just kind of annoying. Oh well.

Quoted 9 lines
Quoted 9 lines

linux-2.6-xen-sparse/drivers/xen/Makefile unsigned long zap_page_range(struct vm_a

Quoted 8 lines
linux-2.6-xen-sparse/drivers/xen/Makefile +7 -0
+#if 0
+        /* if we're not set up to use graphics mode, then don't initialize */
+	if (xenbus_scanf(XBT_NIL, "console", "use_graphics", "%d", &ret) < 0)
+		return -ENODEV;
+	if (ret == 0)
+		return -ENODEV;
+#endif

That's interesting. What is this here for?

Quoted 3 lines

It looks like it got accidentally #if 0'd while merging patches. Whoops. Basically, if the guest isn't set up to use a graphical console, then we shouldn't initialize the devices.

That seems like a good idea. Please put this back in.

Steven.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#5 lore raw
Quoted 7 lines · Steven Smith
Quoted 4 lines

I wouldn't complain if we go the route Keir suggested and have Xen do it ;-) But if not, I think that given the size of the memory in question + grant tables, the quick hack is probably going to be the "right" thing for this case.

Okay. Adding translations to the hypervisor isn't going to happen until after 3.0.3, so we should probably just leave this for a little while.

Another option after 3.0.3 is to allow grant tables to support these large mappings. I have some ideas how to do this fairly efficiently, given that it'll be okay to track mappings to all the pages as an aggregate. It's not going to happen for now though -- it'll just be a shame if we add grant-table support later that it'll change the setup protocol. However, we can probably maintain backward compatibility if we think about it a bit.

-- Keir

#6 lore raw
Quoted 6 lines · Keir Fraser
Quoted 4 lines · Steven Smith
Quoted 4 lines

I wouldn't complain if we go the route Keir suggested and have Xen do it ;-) But if not, I think that given the size of the memory in question + grant tables, the quick hack is probably going to be the "right" thing for this case.

The quick hack being translating mfns in the backend.

Quoted 4 lines
Quoted 3 lines

Okay. Adding translations to the hypervisor isn't going to happen until after 3.0.3, so we should probably just leave this for a little while.

Quoted 8 lines

Another option after 3.0.3 is to allow grant tables to support these large mappings. I have some ideas how to do this fairly efficiently, given that it'll be okay to track mappings to all the pages as an aggregate. It's not going to happen for now though -- it'll just be a shame if we add grant-table support later that it'll change the setup protocol. However, we can probably maintain backward compatibility if we think about it a bit.

-- Keir

Is there anything we can do now to help with maintaining backward compatibility later?

Evolving interfaces are a fact of life. What about versioning? Frontend puts its interface version in xenstore, bump it when we change stuff (which should happen very rarely, of course), backend queries the version and does the right thing.

#7 lore raw
Quoted 7 lines · Markus Armbruster

Is there anything we can do now to help with maintaining backward compatibility later?

Evolving interfaces are a fact of life. What about versioning? Frontend puts its interface version in xenstore, bump it when we change stuff (which should happen very rarely, of course), backend queries the version and does the right thing.

We don't need to think too hard about this right now. The way we do this for netfront/netback is to add feature nodes to xenstore. Absence of a feature node (e.g., because backend is too old to support the feature) means the feature is unavailable to the frontend and it will fall back to old methods. In some cases feature negotiation is a handshake (backend writes a feature node to indicate it supports a feature; frontend writes a request node to indicate it wants to use a supported feature).

-- Keir

#8 lore raw
In reply to Markus Armbruster
Quoted 6 lines
Quoted 6 lines

Another option after 3.0.3 is to allow grant tables to support these large mappings. I have some ideas how to do this fairly efficiently, given that it'll be okay to track mappings to all the pages as an aggregate. It's not going to happen for now though -- it'll just be a shame if we add grant-table support later that it'll change the setup protocol. However, we can probably maintain backward compatibility if we think about it a bit.

Quoted 7 lines

Is there anything we can do now to help with maintaining backward compatibility later?

Evolving interfaces are a fact of life. What about versioning? Frontend puts its interface version in xenstore, bump it when we change stuff (which should happen very rarely, of course), backend queries the version and does the right thing.

If we're going to do this, it'd probably be worth exporting a backend version number as well.

Even better would be having a bunch of separate feature flags, like we do now for the different netif protocols. You might, for instance, have backends which can use grant tables export feature-large-grants to their area of xenbus, and then have frontends notice that and set request-large-grants in their area if they support it. If the relevant nodes aren't present, you conclude that the other end either doesn't support the feature or doesn't want to use it for some reason.

This has a couple of advantages over a pure version number scheme:

-- If either end doesn't like the new protocol, it's trivial to make
it fall back to the old one. We might want to kill the old protocol eventually, but this at least makes the transition a bit easier.
-- You don't need to write any code now.
-- I think it's a bit cleaner than potentially artificially tying
features together.

Thoughts?

Steven.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#9 lore raw
In reply to Steven Smith
Quoted 6 lines · Steven Smith

Okay, this fixes a lot of the things I complained about last time, and most of the new problems should be fairly easy to fix up.

There are still a couple of things from the last round which are unfixed. This is mostly my fault for going on holiday at an inconvenient time; sorry about that.

I'm about to commit the same crime :) Expect me back on the 19th.

Quoted 9 lines

The big problem with this is that you don't support shadow translate mode guests, because the frontend exposes mfns directly to the backend. The correct way of fixing this is to use grant tables, but, as Anthony pointed out, that's quite hard due to the amount of memory you need to grant access to.

The easy way of doing this would be to just increase the size of the grant tables. Alternatively, you could add support for granting access to ranges of memory, but that sounds like a whole lot of work.

Yes, it would be great if Xen provided the means to do it right, but as Keir wrote that's post 3.0.3.

Quoted 3 lines

You could also put in a quick hack of just translating the mfns in the backend if the frontend is in shadow translate mode, but that's not really very nice.

As far as I can see, this is our only option right now. I'm ignorant of `shadow translate mode'; can somebody please educate me on how to detect and translate shadow translate mode frontend mfns in the backend?

Quoted 3 lines

It'd also be nice if the protocol supported putting the backend in an unprivileged domain, which grant tables would give you for free, but that's a lower priority.

I guess that has to wait until we can do the framebuffer mapping nicely.

Quoted 4 lines

Aside from that, the lack of support for multiple framebuffers in one guest is kind of annoying. It's not necessary to have this working in a first cut of the patch, but it'd be good if the protocol included some way of adding it in later, which the current one doesn't.

Well, the only place that limits us to a single framebuffer is our use of xenstore, isn't it?. All we need for additional framebuffers then is new xenstore directories, say vfbN and vkbdN, where N is 0, 1, ...

If you feel it's useful, I can rename the xenstore directories to vfb0 and vkbd0 now.

Quoted 4 lines

Hard coding the resolution is also kind of icky, but is again acceptable for a first cut. It'd be good to have at least a plan for how this is supposed to work before the thing gets merged, including how to change the resolution of a live framebuffer.

It's always good to have a plan for future work. Note, however, that the protocol is easily extensible, and therefore we don't have to fear coding ourselves into a box now that we have to break later, just for lack of foresight.

Quoted 3 lines

Also, the spec you pointed me at last time appears to be out of date:
it still thinks you get the pgdir mfn from start info, which certainly isn't acceptable for new drivers.

Yes, it's out of date. Let's nail down things here, and if we then feel the spec is useful to have, we update it.

Quoted 25 lines
Quoted 25 lines

Initial patch from Anthony Liguori, ported to xenstore and current Xen by Markus Armbruster. Further improvements since the last submission
* Don't set up on dom0 or if our domain isn't setup for a graphics console (katzj)
* Compute framebuffer size from screen dimension (armbru)
* Implement XENFB_TYPE_SET_EVENTS (armbru)
* Future-proof layout of shared page (armbru)
* Memory barriers (armbru)
* Coaelsce notifies: just one batch of events instead of one per event (armbru)
* Error handling fixes (armbru)

Cc Markus Armbruster <[email protected]>
Cc Anthony Liguori <[email protected]>

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c

linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c +2 -0
@@ -1866,8 +1866,12 @@ void __init setup_arch(char **cmdline_p)
 #endif
 #endif
 	} else {
+#if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
+		conswitchp = &dummy_con;
Quoted 8 lines

linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c void __init setup_arch(char **cmdline_p) · line 1876

Quoted 6 lines
linux-2.6-xen-sparse/arch/i386/kernel/setup-xen.c +2 -0
+#else
 		extern int console_use_vt;
 		console_use_vt = 0;
+#endif
 	}
 }

Not quite sure I understand what this is trying to achieve, or why it's related to the PV framebuffer stuff.

Jeremy answered this one.

Quoted 33 lines
Quoted 20 lines

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/Kconfig

linux-2.6-xen-sparse/drivers/xen/Kconfig +13 -0
@@ -172,6 +172,29 @@ config XEN_NETDEV_FRONTEND
 	  dedicated device-driver domain, or your master control domain
 	  (domain 0), then you almost certainly want to say Y here.
 
+config XEN_FRAMEBUFFER
+	tristate "Framebuffer-device frontend driver"
+	depends on XEN && FB
+	select FB_CFB_FILLRECT
+	select FB_CFB_COPYAREA
+	select FB_CFB_IMAGEBLIT
+	default y
+	help
+	  The framebuffer-device frontend drivers allows the kernel to create a
+	  virtual framebuffer.  This framebuffer can be viewed in another
+	  domain.  Unless this domain has access to a real video card, you
+	  probably want to say Y here.
+

linux-2.6-xen-sparse/drivers/xen/Kconfig config XEN_NETDEV_FRONTEND · line 197

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/Kconfig +10 -0
+config XEN_KEYBOARD
+	tristate "Keyboard-device frontend driver"
+	depends on XEN
+	default y
+	help
+	  The keyboard-device frontend driver allows the kernel to create a
+	  virtual keyboard.  This keyboard can then be driven by another
+	  domain.  If you've said Y to CONFIG_XEN_FRAMEBUFFER, you probably
+	  want to say Y here.
+

From last time:

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Quoted 1 line

What happens if you configure XEN_KEYBOARD but not XEN_FRAMEBUFFER?

The backend refuses to connect, i.e. you can't use it. Anthony, can you shed some light on why you created two separate configs? you shed some light on why you created two separate configs?

They are two separate devices. Seemed like a natural separation to me.

Perhaps XEN_KEYBOARD should depend on XEN_FRAMEBUFFER, given that it doesn't work without it.

Good point. Jeremy took care of it.

Quoted 14 lines
Quoted 14 lines

config XEN_SCRUB_PAGES

linux-2.6-xen-sparse/drivers/xen/Kconfig +0 -0
 	bool "Scrub memory before freeing it to Xen"
 	default y

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/Makefile

linux-2.6-xen-sparse/drivers/xen/Makefile +2 -0
@@ -15,3 +15,5 @@ obj-$(CONFIG_XEN_NETDEV_FRONTEND)	+= net
 obj-$(CONFIG_XEN_NETDEV_FRONTEND)	+= netfront/
 obj-$(CONFIG_XEN_PCIDEV_BACKEND)	+= pciback/
 obj-$(CONFIG_XEN_PCIDEV_FRONTEND)	+= pcifront/
+obj-$(CONFIG_XEN_FRAMEBUFFER)		+= xenfb/
+obj-$(CONFIG_XEN_KEYBOARD)		+= xenkbd/

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/mm/memory.c
--- a/linux-2.6-xen-sparse/mm/memory.c Sat Sep 02 12:11:54 2006 +0100

Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/Makefile unsigned long zap_page_range(struct vm_a · line 884

Quoted 6 lines

+++ b/linux-2.6-xen-sparse/mm/memory.c Sat Sep 02 15:11:17 2006 -0400

linux-2.6-xen-sparse/drivers/xen/Makefile +1 -0
@@ -881,6 +881,7 @@ unsigned long zap_page_range(struct vm_a
 		tlb_finish_mmu(tlb, address, end);
 	return end;
 }
+EXPORT_SYMBOL(zap_page_range);

It's unfortunate that we have to modify an upstream file here, but I can't really see any alternative.

Neither can I, nor the folks I asked.

Quoted 40 lines
Quoted 40 lines
linux-2.6-xen-sparse/drivers/xen/Makefile +0 -0
 /*
  * Do a quick page-table lookup for a single page.

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/xenfb/Makefile

linux-2.6-xen-sparse/drivers/xen/xenfb/Makefile new file +1 -0
@@ -0,0 +1,1 @@
+obj-$(CONFIG_XEN_FRAMEBUFFER)	:= xenfb.o

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +29 -0
@@ -0,0 +1,571 @@
+/*
+ * linux/drivers/video/xenfb.c -- Xen para-virtual frame buffer device
+ *
+ * Copyright (C) 2005-2006
+ *
+ *      Anthony Liguori <[email protected]>
+ *
+ *  Based on linux/drivers/video/q40fb.c
+ *
+ *  This file is subject to the terms and conditions of the GNU General Public
+ *  License. See the file COPYING in the main directory of this archive for
+ *  more details.
+ */
+
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/fb.h>
+#include <linux/module.h>
+#include <linux/vmalloc.h>
+#include <linux/mm.h>
+#include <asm/hypervisor.h>
+#include <xen/evtchn.h>
+#include <xen/xenbus.h>
+#include <linux/xenfb.h>
+#include <linux/kthread.h>
+
+#define XENFB_WIDTH 800
+#define XENFB_HEIGHT 600
+#define XENFB_DEPTH 32
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 36

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+
+static int xenfb_fps = 20;
+static unsigned long xenfb_mem_len = XENFB_WIDTH * XENFB_HEIGHT * XENFB_DEPTH / 8;
+
+struct xenfb_mapping
+{
+	struct list_head	next;

Perhaps not the best name for a list head?

The mappings are kept in a list of struct xenfb_mapping, chained through member next. I've seen worse names. Feel free to suggest a better one.

Quoted 5 lines
Quoted 5 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+	struct vm_area_struct	*vma;
+	atomic_t		map_refs;
+	int			faults;
+	struct xenfb_info	*info;
+};
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 48

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+
+struct xenfb_info
+{
+	struct task_struct		*kthread;
+	wait_queue_head_t		wq;
+
+	unsigned char			*fb;

Why unsigned char?

I think this is a matter of style. Plain char suggests characters, while unsigned char suggests bytes. fb points to an array of bytes.

Quoted 4 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 50

Quoted 2 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +2 -0
+	struct fb_fix_screeninfo	*fix;
+	struct fb_var_screeninfo	*var;

Not quite sure what these are for. Why not just get at them through the fb_info pointer?

Fine with me.

Quoted 7 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 55

Quoted 6 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+	struct fb_info			*fb_info;
+	struct timer_list		refresh;
+	int				dirty;
 
+	int				y1, y2;
+	int				x1, x2;

These could perhaps do with slightly more descriptive names?

Would you settle for a descriptive comment instead?

Quoted 5 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 2 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +2 -0
+
+	struct semaphore		mm_lock;

That's going to confuse people.

(Me, for a start)

What exactly confuses you? The name? Could rename to mm_sem.

Quoted 10 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 66

Quoted 9 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +9 -0
+	int				nr_pages;
+	struct page			**pages;
+	struct list_head		mappings;
+
+	unsigned			evtchn;
+	int				irq;
+	struct xenfb_page		*page;
+	unsigned long 			*mfns;
+	u32				flags;

Maybe a comment to say what sort of flags go in here?

Sure.

Quoted 12 lines
Quoted 12 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +12 -0
+};
+
+static void xenfb_do_update(struct xenfb_info *info,
+			    int x, int y, int w, int h)
+{
+	union xenfb_out_event event;
+	__u32 prod;
+
+	event.type = XENFB_TYPE_UPDATE;
+	event.update.x = x;
+	event.update.y = y;
+	event.update.width = w;
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 83

Quoted 5 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+	event.update.height = h;
+
+	prod = info->page->out_prod;
+	if (prod - info->page->out_cons == XENFB_OUT_RING_LEN)
+		return;		/* ring buffer full, event lost */

It seems to me like if we lose the event, we probably shouldn't mark the framebuffer as clean and throw away the update region. Perhaps the caller should instead reschedule the timer?

Actually, this can't happen. xenfb_update_screen() ensures there's space in the ring before it proceeds to walk the mappings. That space can't go away, because no other place puts stuff into the ring, and only one instance of xenfb_do_update() can run (in the kernel thread).

Quoted 1 line

Also, is there any reason the test couldn't use xenfb_queue_full?

I removed the redundant test instead.

Quoted 29 lines
Quoted 29 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +29 -0
+	mb();			/* ensure ring space available */
+	XENFB_OUT_RING_REF(info->page, prod) = event;
+	wmb();			/* ensure ring contents visible */
+	info->page->out_prod = prod + 1;
+
+	notify_remote_via_evtchn(info->evtchn);
+}
+
+static int xenfb_queue_full(struct xenfb_info *info)
+{
+	__u32 cons, prod;
+
+	prod = info->page->out_prod;
+	cons = info->page->out_cons;
+	return prod - cons == XENFB_OUT_RING_LEN;
+}
+
+static void xenfb_update_screen(struct xenfb_info *info)
+{
+	int y1, y2, x1, x2;
+	struct list_head *item;
+	struct xenfb_mapping *map;
+
+	if (!(info->flags & XENFB_FLAG_UPDATE))
+		return;
+	if (xenfb_queue_full(info))
+		return;
+
+	y1 = info->y1;
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 119

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+	y2 = info->y2;
+	x1 = info->x1;
+	x2 = info->x2;
+	info->y1 = info->y2 = info->x1 = info->x2 = 0;
+	down(&info->mm_lock);
+	list_for_each(item, &info->mappings) {
+		map = list_entry(item, struct xenfb_mapping, next);

list_for_each_entry, perhaps?

Going to try.

Quoted 45 lines
Quoted 45 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +45 -0
+		if (!map->faults)
+			continue;
+		zap_page_range(map->vma, map->vma->vm_start,
+			       map->vma->vm_end - map->vma->vm_start, NULL);
+		map->faults = 0;
+	}
+	up(&info->mm_lock);
+
+	xenfb_do_update(info, x1, y1, x2 - x1, y2 - y1);
+}
+
+static int xenfb_thread(void *data)
+{
+	struct xenfb_info *info = data;
+
+	for (;;) {
+		if (kthread_should_stop())
+			break;
+		if (info->dirty) {
+			info->dirty = 0;
+			xenfb_update_screen(info);
+		}
+		wait_event_interruptible(info->wq,
+			kthread_should_stop() || info->dirty);
+	}
+	return 0;
+}
+
+static int xenfb_setcolreg(unsigned regno, unsigned red, unsigned green,
+			   unsigned blue, unsigned transp,
+			   struct fb_info *info)
+{
+	u32 v;
+
+	if (regno > info->cmap.len)
+		return 1;
+
+	red   >>= (16 - info->var.red.length);
+	green >>= (16 - info->var.green.length);
+	blue  >>= (16 - info->var.blue.length);
+
+	v = (red << info->var.red.offset) |
+	    (green << info->var.green.offset) |
+	    (blue << info->var.blue.offset);
+
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 171

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+	switch (info->var.bits_per_pixel) {
+	case 16:
+	case 24:
+	case 32:
+		((u32 *)info->pseudo_palette)[regno] = v;
+		break;
+	}

That looks exciting. What is this code trying to achieve?

Beats me. Anthony?

Quoted 44 lines

There are comments in xxxfb_setcolreg in skeletonfb to the effect that pseudo_palette should only get played with if fix.visual is TRUECOLOR or DIRECTCOLOR, and a specific warning against trying to do stuff with bits_per_pixel here.

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 181

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+	
+	return 0;
+}
+
+static void xenfb_timer(unsigned long data)
+{
+	struct xenfb_info *info = (struct xenfb_info *)data;
+	info->dirty = 1;
+	wake_up(&info->wq);
+}

From last time:

Quoted 6 lines
Quoted 3 lines

Could this end up running at the same time as update_screen and race to update dirty? I suppose it doesn't actually matter too much if it does.

xenfb_timer() runs from the timer interrupt, sets dirty, then kicks the wait queue.

Quoted 8 lines

A separate kernel thread running xenfb_thread() sleeps on the wait queue. When it wakes up, and dirty is set, it clears dirty and updates the screen. Then goes back to sleep.

Obviously, dirty is always set when the thread wakes up. If xenfb_timer() runs before the thread goes to sleep again, the wakeup is lost, and the value of dirty doesn't matter. I believe that's okay.

It looks to me like we can lose updates to the screen in that case:

xenfb_timer runs, sets dirty to 1, kicks the thread
xenfb_thread wakes up xenfb_thread reads the update region
another update happens, extends the update region, reschedules xenfb_timer xenfb_timer runs again, sets dirty to 2, kicks the thread again
xenfb_thread sets dirty to 0 xenfb_thread sends an update using
the old region

Quoted 2 lines

I suppose the race is sufficiently unlikely and sufficiently mild that it can probably be ignored for all realistic purposes.

This can't happen, because both use and update of the mappings is protected by mm_lock:

xenfb_timer runs, sets dirty to 1, kicks the thread
xenfb_thread wakes up xenfb_thread sets dirty to 0

xenfb_thread down mm_lock xenfb_thread reads the update region xenfb_thread up mm_lock

xenfb_thread sends an update using
the old region

Possibly concurrent:

another update happens:
down mm_lock
extends the update region, reschedules xenfb_timer up mm_lock

xenfb_timer runs again, sets dirty to 1, kicks the thread again

Harmless race: dirty can conceivably become 1 when there's no work to do. Then xenfb_thread wakes up, finds nothing to do and goes back to sleep.

Looks safe to me.

Quoted 10 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 184

Quoted 3 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +3 -0
+
+static void xenfb_refresh(struct xenfb_info *info,
+			  int x1, int y1, int w, int h)

Is there any reason why there can't be multiple calls to this function live at the same time? In particular, if vm_nopage and xenfb_copyarea happen at the same time it looks like we can lose part of the update area.

Also, vm_nopage holds the mm_lock when it calls this, whereas the other callers appear not to. Was this deliberate?

The question is what mm_lock protects. If it protects info->mappings, then xenfb_refresh() doesn't care about it, as it doesn't access it.

If it should protect x1, x2, y1, y2 as well, then locking looks flawed.

x1, x2, y1, y2 need protection if they can be updated concurrently. Users are xenfb_update_screen() and xenfb_refresh(). The latter runs from fb_ops methods and vm_operations_struct method nopage. Looks like they need protection, doesn't it? Anthony?

Quoted 8 lines
Quoted 8 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +8 -0
+{
+	int y2, x2;
+
+	y2 = y1 + h;
+	x2 = x1 + w;
+	if (info->y2 == 0) {
+		info->y1 = y1;
+		info->y2 = y2;
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 197

Quoted 5 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+	}
+	if (info->x2 == 0) {
+		info->x1 = x1;
+		info->x2 = x2;
+	}

Presumably, these are just looking to see if there's a pre-existing region, and if not copying the new region across verbatim? Could you test dirty instead in that case?

What about initializing x1, y1 with an impossibly small value, and x2, y2 with an impossibly large value?

Quoted 10 lines
Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+
+	if (info->y1 > y1)
+		info->y1 = y1;
+	if (info->y2 < y2)
+		info->y2 = y2;
+	if (info->x1 > x1)
+		info->x1 = x1;
+	if (info->x2 < x2)
+		info->x2 = x2;
+
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 211

Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +4 -0
+	if (timer_pending(&info->refresh))
+		return;
+
+	mod_timer(&info->refresh, jiffies + HZ/xenfb_fps);

Would it be sensible to have a fallback so that an update is always sent to the backend e.g. 100ms after any change to the framebuffer, so that the user still sees something when the display is being updated very frequently?

Could this be done in a later revision? Happy to add the idea in a comment now.

Quoted 100 lines
Quoted 100 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +100 -0
+}
+
+static void xenfb_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
+{
+	struct xenfb_info *info = p->par;
+
+	cfb_fillrect(p, rect);
+	xenfb_refresh(info, rect->dx, rect->dy, rect->width, rect->height);
+}
+
+static void xenfb_imageblit(struct fb_info *p, const struct fb_image *image)
+{
+	struct xenfb_info *info = p->par;
+
+	cfb_imageblit(p, image);
+	xenfb_refresh(info, image->dx, image->dy, image->width, image->height);
+}
+
+static void xenfb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
+{
+	struct xenfb_info *info = p->par;
+
+	cfb_copyarea(p, area);
+	xenfb_refresh(info, area->dx, area->dy, area->width, area->height);
+}
+
+static void xenfb_vm_open(struct vm_area_struct *vma)
+{
+	struct xenfb_mapping *map = vma->vm_private_data;
+	atomic_inc(&map->map_refs);
+}
+
+static void xenfb_vm_close(struct vm_area_struct *vma)
+{
+	struct xenfb_mapping *map = vma->vm_private_data;
+	struct xenfb_info *info = map->info;
+
+	down(&info->mm_lock);
+	if (atomic_dec_and_test(&map->map_refs)) {
+		list_del(&map->next);
+		kfree(map);
+	}
+	up(&info->mm_lock);
+}
+
+static struct page *xenfb_vm_nopage(struct vm_area_struct *vma,
+				    unsigned long vaddr, int *type)
+{
+	struct xenfb_mapping *map = vma->vm_private_data;
+	struct xenfb_info *info = map->info;
+	int pgnr = (vaddr - vma->vm_start) >> PAGE_SHIFT;
+	struct page *page;
+	int y1, y2;
+
+	if (pgnr >= info->nr_pages)
+		return NOPAGE_SIGBUS;
+
+	down(&info->mm_lock);
+	page = info->pages[pgnr];
+	get_page(page);
+	map->faults++;
+
+	y1 = pgnr * PAGE_SIZE / info->fix->line_length;
+	y2 = (pgnr * PAGE_SIZE + PAGE_SIZE - 1) / info->fix->line_length;
+	if (y2 > info->var->yres)
+		y2 = info->var->yres;
+	xenfb_refresh(info, 0, y1, info->var->xres, y2 - y1);
+	up(&info->mm_lock);
+
+	if (type)
+		*type = VM_FAULT_MINOR;
+
+	return page;
+}
+
+static struct vm_operations_struct xenfb_vm_ops = {
+	.open	= xenfb_vm_open,
+	.close	= xenfb_vm_close,
+	.nopage	= xenfb_vm_nopage,
+};
+
+static int xenfb_mmap(struct fb_info *fb_info, struct vm_area_struct *vma)
+{
+	struct xenfb_info *info = fb_info->par;
+	struct xenfb_mapping *map;
+	int ret;
+	int map_pages;
+
+	down(&info->mm_lock);
+
+	ret = -EINVAL;
+	if (!(vma->vm_flags & VM_WRITE))
+		goto out;
+	if (!(vma->vm_flags & VM_SHARED))
+		goto out;
+	if (vma->vm_pgoff != 0)
+		goto out;
+
+	map_pages = (vma->vm_end - vma->vm_start + PAGE_SIZE-1) >> PAGE_SHIFT;
+	if (map_pages > info->nr_pages)
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 318

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+		goto out;
+
+	ret = -ENOMEM;
+	map = kmalloc(sizeof(*map), GFP_KERNEL);
+	if (map == NULL)
+		goto out;
+	memset(map, 0, sizeof(*map));

kzalloc, maybe?

Sure.

Quoted 46 lines
Quoted 46 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +46 -0
+
+	map->vma = vma;
+	map->faults = 0;
+	map->info = info;
+	atomic_set(&map->map_refs, 1);
+	list_add(&map->next, &info->mappings);
+	vma->vm_ops = &xenfb_vm_ops;
+	vma->vm_flags |= (VM_DONTEXPAND | VM_RESERVED);
+	vma->vm_private_data = map;
+	ret = 0;
+
+ out:
+	up(&info->mm_lock);
+	return ret;
+}
+
+static struct fb_ops xenfb_fb_ops = {
+	.owner		= THIS_MODULE,
+	.fb_setcolreg	= xenfb_setcolreg,
+	.fb_fillrect	= xenfb_fillrect,
+	.fb_copyarea	= xenfb_copyarea,
+	.fb_imageblit	= xenfb_imageblit,
+	.fb_mmap	= xenfb_mmap,
+};
+
+static irqreturn_t xenfb_event_handler(int rq, void *dev_id,
+				       struct pt_regs *regs)
+{
+	struct xenfb_info *info = dev_id;
+	__u32 cons, prod;
+
+	prod = info->page->in_prod;
+	rmb();			/* ensure we see ring contents up to prod */
+	for (cons = info->page->in_cons; cons != prod; cons++) {
+		union xenfb_in_event *event;
+		event = &XENFB_IN_RING_REF(info->page, cons);
+
+		switch (event->type) {
+		case XENFB_TYPE_SET_EVENTS:
+			info->flags = event->set_events.flags;
+			break;
+		}
+	}
+	mb();			/* ensure we're done with ring contents */
+	info->page->in_cons = cons;
+	notify_remote_via_evtchn(info->evtchn);
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 371

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+
+	return IRQ_HANDLED;
+}
+
+static unsigned long vmalloc_to_mfn(void *address)
+{
+	// FIXME was return pfn_to_mfn(vmalloc_to_pfn(address));

Not quite sure why this comment is here.

Accident. However, arbitrary_virt_to_machine() doesn't appear to exist on ia64, so I went back to the old code.

Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 377

Quoted 6 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +6 -0
+	return arbitrary_virt_to_machine(address) >> PAGE_SHIFT;
+}
+
+static struct xenfb_info *xenfb_info;
+
+static int __init xenfb_probe(void)

I'd find it easier to be confident of the error handling here if the function were split up a little.

It's just the way I found it. My own idea of cleanup would be to make xenfb_cleanup() work for the error case.

Quoted 11 lines
Quoted 11 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +11 -0
+{
+	struct xenfb_info *info;
+	int i, ret;
+	struct fb_info *fb_info;
+	struct evtchn_alloc_unbound alloc_unbound;
+	struct evtchn_close close;
+	struct xenbus_transaction xbt;
+
+        /* Nothing to do if running in dom0. */
+        if (is_initial_xendomain())
+		return -ENODEV;
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 395

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+#if 0
+        /* if we're not set up to use graphics mode, then don't initialize */
+	if (xenbus_scanf(XBT_NIL, "console", "use_graphics", "%d", &ret) < 0)
+		return -ENODEV;
+	if (ret == 0)
+		return -ENODEV;
+#endif

That's interesting. What is this here for?

Jeremy explained this one already. The #if is an accident.

Quoted 14 lines
Quoted 14 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +14 -0
+
+	info = kmalloc(sizeof(*info), GFP_KERNEL);
+	if (info == NULL)
+		return -ENOMEM;
+	memset(info, 0, sizeof(*info));
+
+	INIT_LIST_HEAD(&info->mappings);
+
+	info->fb = vmalloc(xenfb_mem_len);
+	if (info->fb == NULL)
+		goto error;
+	memset(info->fb, 0, xenfb_mem_len);
+	info->nr_pages = (xenfb_mem_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
+	info->pages = kmalloc(sizeof(struct page*)*info->nr_pages, GFP_KERNEL);
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 416

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+	if (info->pages == NULL)
+		goto error_vfree;
+	for (i = 0; i < info->nr_pages; i++)
+		info->pages[i] = vmalloc_to_page(info->fb + i * PAGE_SIZE);
+
+	fb_info = framebuffer_alloc(sizeof(u32) * 256, NULL);
+	// FIXME sizeof(struct xenfb_info)

Where did sizeof(u32) * 256 come from? What does the comment mean?

I *suspect* that the correct argument is sizeof(struct xenfb_info), but can't yet exclude subtle, evil hackery going on here, so I put in a comment for now. Anthony, do you remember hackery here?

Quoted 1 line

fb_info isn't released from the error path.

Fixing...

Quoted 6 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 420

Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +4 -0
+	if (fb_info == NULL)
+		goto error_kfree;
+
+	info->mfns = vmalloc(sizeof(unsigned long) * info->nr_pages);

What happens if this fails? Also, you never seem to vfree this if we hit an error later on.

Fixing...

Quoted 11 lines
Quoted 11 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +11 -0
+	/* set up shared page */
+	info->page = (void *)__get_free_page(GFP_KERNEL);
+	if (!info->page)
+		goto error_kfree;
+	/* set up event channel */
+	alloc_unbound.dom = DOMID_SELF;
+	alloc_unbound.remote_dom = 0;
+	ret = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
+					  &alloc_unbound);
+	if (ret)
+		goto error_freep;
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 436

Quoted 5 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+	info->evtchn = alloc_unbound.port;
+
+	for (i = 0; i < info->nr_pages; i++)
+		info->mfns[i] = vmalloc_to_mfn(info->fb + i * PAGE_SIZE);
+	info->page->pd[0] = vmalloc_to_mfn(info->mfns);

What happens if this domain is running in shadow translate mode? It looks to me like the backend will end up mapping completely the wrong frame.

Discussion above seems to suggest that our only option is to have the backend detect and translate shadow translate mode frontends, so no action is required here. Correct?

Quoted 19 lines
Quoted 19 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +19 -0
+	info->page->pd[1] = 0;
+	info->page->width = XENFB_WIDTH;
+	info->page->height = XENFB_HEIGHT;
+	info->page->depth = XENFB_DEPTH;
+	info->page->line_length = (info->page->depth / 8) * info->page->width;
+	info->page->mem_length = xenfb_mem_len;
+	info->page->in_cons = info->page->in_prod = 0;
+	info->page->out_cons = info->page->out_prod = 0;
+
+	ret = bind_evtchn_to_irqhandler(info->evtchn, xenfb_event_handler,
+					0, "xenfb", info);
+	if (ret < 0) {
+		close.port = info->evtchn;
+		HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
+		goto error_freep;
+	}
+
+	info->irq = ret;
+	xenfb_info = info;
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 462

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+
+	fb_info->pseudo_palette = fb_info->par;
+	fb_info->par = info;
+	fb_info->screen_base = info->fb;
+
+	memset(&fb_info->var, 0, sizeof(fb_info->var));
+	memset(&fb_info->fix, 0, sizeof(fb_info->fix));

I think these are already guaranteed to be zero, aren't they?

Yes, framebuffer_alloc() clears *fb_info. Happy to rely on that?

Quoted 19 lines
Quoted 19 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +19 -0
+
+	fb_info->fbops = &xenfb_fb_ops;
+	fb_info->var.xres_virtual = fb_info->var.xres = info->page->width;
+	fb_info->var.yres_virtual = fb_info->var.yres = info->page->height;
+	fb_info->var.bits_per_pixel = info->page->depth;
+
+	fb_info->var.red = (struct fb_bitfield){16, 8, 0};
+	fb_info->var.green = (struct fb_bitfield){8, 8, 0};
+	fb_info->var.blue = (struct fb_bitfield){0, 8, 0};
+
+	fb_info->var.activate = FB_ACTIVATE_NOW;
+	fb_info->var.height = -1;
+	fb_info->var.width = -1;
+	fb_info->var.vmode = FB_VMODE_NONINTERLACED;
+
+	fb_info->fix.visual = FB_VISUAL_TRUECOLOR;
+	fb_info->fix.line_length = info->page->line_length;
+	fb_info->fix.smem_start = 0;
+	fb_info->fix.smem_len = xenfb_mem_len;
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 488

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+	strcpy(fb_info->fix.id, "xen");
+	fb_info->fix.type = FB_TYPE_PACKED_PIXELS;
+	fb_info->fix.accel = FB_ACCEL_NONE;
+
+	fb_info->flags = FBINFO_FLAG_DEFAULT;
+
+	fb_alloc_cmap(&fb_info->cmap, 256, 0);

This can fail, and the error paths don't bother to deallocate it.

Fixing...

Quoted 28 lines
Quoted 16 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +16 -0
+
+	info->fb_info = fb_info;
+	info->fix = &fb_info->fix;
+	info->var = &fb_info->var;
+
+	init_MUTEX(&info->mm_lock);
+	init_waitqueue_head(&info->wq);
+	init_timer(&info->refresh);
+	info->refresh.function = xenfb_timer;
+	info->refresh.data = (unsigned long)info;
+
+	info->kthread = kthread_run(xenfb_thread, info, "xenfb thread");
+	if (IS_ERR(info->kthread))
+		goto error_unbind;
+
+	ret = register_framebuffer(fb_info);

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 514

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+	if (ret)
+		goto error_unbind;
+
+ again:
+	ret = xenbus_transaction_start(&xbt);
+	if (ret)
+		goto error_unreg;
+	ret = xenbus_printf(xbt, "vfb", "page-ref", "%lu",
+			    virt_to_mfn(info->page));
+	// FIXME grant tables?

Again, what happens in shadow translate mode?

Quoted 2 lines

Also, it's kind of annoying that there's no support for multiple framebuffers in a domU. That's very much a wishlist thing, though.

See above.

Quoted 15 lines
Quoted 15 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +15 -0
+	if (ret)
+		goto error_xenbus;
+	ret = xenbus_printf(xbt, "vfb", "event-channel", "%u",
+			    info->evtchn);
+	if (ret)
+		goto error_xenbus;
+	ret = xenbus_transaction_end(xbt, 0);
+	if (ret) {
+		if (ret == -EAGAIN)
+			goto again;
+		goto error_unreg;
+	}
+
+	return 0;
+
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 536

Quoted 7 lines

+ error_xenbus:

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +6 -0
+	xenbus_transaction_end(xbt, 1);
+ error_unreg:
+	unregister_framebuffer(fb_info);
+ error_unbind:
+	unbind_from_irqhandler(info->irq, info);
+	// FIXME do we have to stop info->kthread?

If you've started it you certainly need to stop it.

Fixing...

Quoted 19 lines
Quoted 6 lines

+ error_freep:

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+	free_page((unsigned long)info->page);
+ error_kfree:
+	kfree(info->pages);
+ error_vfree:
+	vfree(info->fb);

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 552

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+ error:
+	kfree(info);
+	xenfb_info = NULL;
+
+	return -ENODEV;
+}
+
+static int __init xenfb_init(void)
+{
+	return xenfb_probe();

From last time:

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

You might want to consider using xenbus_device and friends here.

I considered that, but it wasn't obvious to me how to make it fit cleanly to a user-space backend. Care to advise?

I think if the frontend needs to know whether the backend is in kernel or userspace then we've messed up somewhere. Why do you expect this to be difficult?

I didn't *expect* it to be difficult, I just couldn't see how to fit Anthony's code to xenbus_device without rewriting more than I like, and I still can't see it. That's why I asked for advice.

Anyway, I'm reluctant to throw out working code while there's so much code that needs fixing. I may be less reluctant once we've drained the swamp some more.

Quoted 7 lines
Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+}
+
+static void __exit xenfb_cleanup(void)
+{
+	struct xenfb_info *info = xenfb_info;
+
+	unregister_framebuffer(info->fb_info);
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 545

Quoted 6 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +6 -0
+	unbind_from_irqhandler(info->irq, info);
+	free_page((unsigned long)info->page);
+	kfree(info->pages);
+	vfree(info->fb);
+	kfree(info);
+	xenfb_info = NULL;

I don't think this releases all of the memory you allocate in xenfb_probe.

Fixing...

Quoted 97 lines
Quoted 97 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +6 -0
+}
+
+module_init(xenfb_init);
+module_exit(xenfb_cleanup);
+
+MODULE_LICENSE("GPL");

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/xenkbd/Makefile

linux-2.6-xen-sparse/drivers/xen/xenkbd/Makefile new file +1 -0
@@ -0,0 +1,1 @@
+obj-$(CONFIG_XEN_KEYBOARD)	+= xenkbd.o

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c

linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +82 -0
@@ -0,0 +1,178 @@
+/*
+ * linux/drivers/input/keyboard/xenkbd.c -- Xen para-virtual input device
+ *
+ * Copyright (C) 2005
+ *
+ *      Anthony Liguori <[email protected]>
+ *
+ *  Based on linux/drivers/input/mouse/sermouse.c
+ *
+ *  This file is subject to the terms and conditions of the GNU General Public
+ *  License. See the file COPYING in the main directory of this archive for
+ *  more details.
+ */
+
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/module.h>
+#include <linux/input.h>
+#include <asm/hypervisor.h>
+#include <xen/evtchn.h>
+#include <xen/xenbus.h>
+#include <linux/xenkbd.h>
+
+struct xenkbd_device
+{
+	struct input_dev *dev;
+	struct xenkbd_info *info;
+	unsigned evtchn;
+	int irq;
+};
+
+static irqreturn_t input_handler(int rq, void *dev_id, struct pt_regs *regs)
+{
+	struct xenkbd_device *dev = dev_id;
+	struct xenkbd_info *info = dev ? dev->info : 0;
+	static int button_map[3] = { BTN_RIGHT, BTN_MIDDLE, BTN_LEFT };
+	__u32 cons, prod;
+
+	prod = info->in_prod;
+	rmb();			/* ensure we see ring contents up to prod */
+	for (cons = info->in_cons; cons != prod; cons++) {
+		union xenkbd_in_event *event;
+		event = &XENKBD_IN_RING_REF(info, cons);
+	
+		switch (event->type) {
+		case XENKBD_TYPE_MOTION:
+			input_report_rel(dev->dev, REL_X, event->motion.rel_x);
+			input_report_rel(dev->dev, REL_Y, event->motion.rel_y);
+			break;
+		case XENKBD_TYPE_BUTTON:
+			if (event->button.button < 3)
+				input_report_key(dev->dev,
+						 button_map[event->button.button],
+						 event->button.pressed);
+			break;
+		case XENKBD_TYPE_KEY:
+			input_report_key(dev->dev, event->key.keycode, event->key.pressed);
+			break;
+		}
+	}
+	input_sync(dev->dev);
+	mb();			/* ensure we got ring contents */
+	info->in_cons = cons;
+	notify_remote_via_evtchn(dev->evtchn);
+
+	return IRQ_HANDLED;
+}
+
+static struct xenkbd_device *xenkbd_dev;
+
+int __init xenkbd_init(void)
+{
+	int ret = 0;
+	int i;
+	struct xenkbd_device *dev;
+	struct input_dev *input_dev;
+	struct evtchn_alloc_unbound alloc_unbound;
+	struct xenbus_transaction xbt;
+
+        /* Nothing to do if running in dom0. */
+        if (is_initial_xendomain())
+	  return -ENODEV;
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c line 89

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +7 -0
+#if 0
+        /* if we're not set up to use graphics mode, then don't initialize */
+	if (xenbus_scanf(XBT_NIL, "console", "use_graphics", "%d", &ret) < 0)
+	  return -ENODEV;
+	if (ret == 0)
+	  return -ENODEV;
+#endif

Not sure about this.

Same as for xenfb.c. The #if is an accident.

Quoted 9 lines

linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c line 94

Quoted 5 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +5 -0
+
+	dev = kmalloc(sizeof(*dev), GFP_KERNEL);
+	input_dev = input_allocate_device();
+	if (!dev || !input_dev)
+		return -ENOMEM;

You potentially leak memory if one of these fails and the other succeeds.

Also, the error paths don't release input_dev. In fact, nothing ever releases input_dev, unless I'm missing something.

Fixing...

Quoted 12 lines
Quoted 12 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +12 -0
+
+	dev->dev = input_dev;
+	dev->info = (void *)__get_free_page(GFP_KERNEL);
+	if (!dev->info) {
+		ret = -ENOMEM;
+		goto error;
+	}
+
+	alloc_unbound.dom = DOMID_SELF;
+	alloc_unbound.remote_dom = 0;
+	ret = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
+					  &alloc_unbound);
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c line 113

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +7 -0
+	if (ret)
+		goto error_freep;
+	dev->evtchn = alloc_unbound.port;
+	ret = bind_evtchn_to_irqhandler(dev->evtchn, input_handler, 0,
+					"xenkbd", dev);
+	if (ret < 0)
+		goto error_freep;

You might want to close the event channel here.

Fixing...

Quoted 10 lines

linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c line 122

Quoted 9 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +9 -0
+	dev->irq = ret;
+
+	input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
+	input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT);
+	input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
+
+	/* FIXME not sure this is quite right */
+	for (i = 0; i < 256; i++)
+		set_bit(i, input_dev->keybit);

It looks reasonable, but perhaps memset would be a better choice.

Dunno. Anthony?

Quoted 5 lines

linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c line 126

Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +4 -0
+
+	input_dev->name = "Xen Virtual Keyboard/Mouse";
+
+	input_register_device(input_dev);

This can fail.

Most uses elsewhere in the kernel don't care. Fixing anyway.

Quoted 9 lines

linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c line 133

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +7 -0
+
+ again:
+	ret = xenbus_transaction_start(&xbt);
+	if (ret)
+		goto error_unreg;
+	ret = xenbus_printf(xbt, "vkbd", "page-ref", "%lu",
+			    virt_to_mfn(dev->info));

There's no reason that I can see not to use grant tables here, and that'd avoid all of the potential issues with shadow translate mode.

Since we have to fool around with shadow translate mode anyway for sharing the framebuffer, I don't quite see why we need to address this right now. This code works, and there's no shortage of code that needs fixing.

Same for vfb/page-ref.

Quoted 42 lines
Quoted 42 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +42 -0
+	if (ret)
+		goto error_xenbus;
+	ret = xenbus_printf(xbt, "vkbd", "event-channel", "%u",
+			    dev->evtchn);
+	if (ret)
+		goto error_xenbus;
+	ret = xenbus_transaction_end(xbt, 0);
+	if (ret) {
+		if (ret == -EAGAIN)
+			goto again;
+		goto error_unreg;
+	}
+
+	dev->info->in_cons = dev->info->in_prod = 0;
+	dev->info->out_cons = dev->info->out_prod = 0;
+	xenkbd_dev = dev;
+
+	return ret;
+
+	
+ error_xenbus:
+	xenbus_transaction_end(xbt, 1);
+ error_unreg:
+	input_unregister_device(input_dev);
+	unbind_from_irqhandler(dev->irq, dev);
+ error_freep:
+	free_page((unsigned long)dev->info);
+ error:
+	kfree(dev);
+	return ret;
+}
+
+static void __exit xenkbd_cleanup(void)
+{
+	input_unregister_device(xenkbd_dev->dev);
+	unbind_from_irqhandler(xenkbd_dev->irq, xenkbd_dev);
+	free_page((unsigned long)xenkbd_dev->info);
+	kfree(xenkbd_dev);
+	xenkbd_dev = NULL;
+}
+
+module_init(xenkbd_init);
Quoted 8 lines

linux-2.6-xen-sparse/include/linux/xenfb.h

Quoted 6 lines
linux-2.6-xen-sparse/drivers/xen/xenkbd/xenkbd.c new file +3 -0
+module_exit(xenkbd_cleanup);
+
+MODULE_LICENSE("GPL");

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/include/linux/xenfb.h

linux-2.6-xen-sparse/include/linux/xenfb.h new file +0 -0

This is defining an io protocol, so belongs in xen/include/public/io

Okay.

Quoted 41 lines
Quoted 15 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +14 -0
@@ -0,0 +1,108 @@
+/*
+ * linux/include/linux/xenfb.h -- Xen virtual frame buffer device
+ *
+ * Copyright (C) 2005
+ *
+ *      Anthony Liguori <[email protected]>
+ *
+ *  This file is subject to the terms and conditions of the GNU General Public
+ *  License. See the file COPYING in the main directory of this archive for
+ *  more details.
+ */
+
+#ifndef _LINUX_XENFB_H
+#define _LINUX_XENFB_H

linux-2.6-xen-sparse/include/linux/xenfb.h line 24

Quoted 10 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +10 -0
+
+#include <asm/types.h>
+
+/* out events */
+
+#define XENFB_OUT_EVENT_SIZE 40
+
+#define XENFB_TYPE_MOTION 1
+#define XENFB_TYPE_UPDATE 2
+

The other tagged unions in Xen look more like this:

struct {
	u8 type;
	union {
		struct {
			u32 field1;
			u32 field2;
		} type1;
		struct {
			u16 field1;
		} type2;
	} u;
};
Quoted 3 lines

I'd find this a lot easier to read than the current approach of putting the type into the individual event type structures. Is there any reason why this couldn't be used here?

Matter of taste. Will change it if you insist.

Quoted 7 lines

linux-2.6-xen-sparse/include/linux/xenfb.h line 30

Quoted 6 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +6 -0
+struct xenfb_motion	    /* currently unused */
+{
+	__u8 type;          /* XENFB_TYPE_MOTION */
+	__u16 x;            /* The new x coordinate */
+	__u16 y;            /* The new y coordinate */
+};

I think if the comment said what was moving this'd be a lot clearer.

I'm not into documenting unused code I didn't write and don't understand, so I'm going to remove this.

Quoted 9 lines
Quoted 9 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +9 -0
+
+struct xenfb_update
+{
+	__u8 type;          /* XENFB_TYPE_UPDATE */
+	__u16 x;            /* source x */
+	__u16 y;            /* source y */
+	__u16 width;        /* rect width */
+	__u16 height;       /* rect height */
+};
Quoted 8 lines

linux-2.6-xen-sparse/include/linux/xenfb.h line 46

Quoted 7 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +7 -0
+
+union xenfb_out_event
+{
+	__u8 type;
+	struct xenfb_motion motion;
+	struct xenfb_update update;
+	char _[XENFB_OUT_EVENT_SIZE];

Perhaps ``pad'' would be clearer than ``_''?

Works for me.

Quoted 48 lines
Quoted 48 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +48 -0
+};
+
+/* in events */
+
+#define XENFB_IN_EVENT_SIZE 40
+
+#define XENFB_TYPE_SET_EVENTS 1
+
+#define XENFB_FLAG_MOTION 1
+#define XENFB_FLAG_UPDATE 2
+#define XENFB_FLAG_COPY 4
+#define XENFB_FLAG_FILL 8
+
+struct xenfb_set_events
+{
+	__u8 type;          /* XENFB_TYPE_SET_EVENTS */
+	__u32 flags;        /* combination of XENFB_FLAG_* */
+};
+
+union xenfb_in_event
+{
+	__u8 type;
+	struct xenfb_set_events set_events;
+	char _[XENFB_OUT_EVENT_SIZE];
+};
+
+/* shared page */
+
+#define XENFB_IN_RING_SIZE 1024
+#define XENFB_IN_RING_LEN (XENFB_IN_RING_SIZE / XENFB_IN_EVENT_SIZE)
+#define XENFB_IN_RING_OFFS 1024
+#define XENFB_IN_RING(page) \
+    ((union xenfb_in_event *)((char *)(page) + XENFB_IN_RING_OFFS))
+#define XENFB_IN_RING_REF(page, idx) \
+    (XENFB_IN_RING((page))[(idx) % XENFB_IN_RING_LEN])
+
+#define XENFB_OUT_RING_SIZE 2048
+#define XENFB_OUT_RING_LEN (XENFB_OUT_RING_SIZE / XENFB_OUT_EVENT_SIZE)
+#define XENFB_OUT_RING_OFFS (XENFB_IN_RING_OFFS + XENFB_IN_RING_SIZE)
+#define XENFB_OUT_RING(page) \
+    ((union xenfb_out_event *)((char *)(page) + XENFB_OUT_RING_OFFS))
+#define XENFB_OUT_RING_REF(page, idx) \
+    (XENFB_OUT_RING((page))[(idx) % XENFB_OUT_RING_LEN])
+
+struct xenfb_page
+{
+	__u16 width;         /* the width of the framebuffer (in pixels) */
+	__u16 height;        /* the height of the framebuffer (in pixels) */
Quoted 8 lines

linux-2.6-xen-sparse/include/linux/xenfb.h line 100

Quoted 6 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +6 -0
+	__u32 line_length;   /* the length of a row of pixels (in bytes) */
+	__u32 mem_length;    /* the length of the framebuffer (in bytes) */
+	__u8 depth;          /* the depth of a pixel (in bits) */
+
+	unsigned long pd[2];	/* FIXME rename to pgdir? */
+	/* FIXME pd[1] unused at this time, shrink? */

I think it'd be enough to just give some indication of where the magic number 2 came from.

Okay.

Quoted 54 lines

From last time:

Quoted 7 lines
Quoted 3 lines

Is it really a good idea for these to be on the shared page? What happens if the {front,back}end is malicious and modifies them while the framebuffer is operating normally?

Obviously, both frontend and backend should be robust against bad shared page contents. The shared page is writable by both, because both need to step the rings.

Quoted 7 lines

The members above are for information flowing from frontend to backend. For safety, they should be write-only in the frontend, and the backend should copy the info to a safer place, checking its consistency.

I can improve safety as sketched. Feel free to suggest a better way to transmit the same information.

We're going to eventually need some way for the frontend to tell the backend to change the display resolution and/or depth. Could this be used to select the initial resolution? I'm not sure what the plan is here, but I would guess something like this:

1) Framebuffer is operating normally, with the frontend modifying the buffer in memory and generating update events in the usual way.

2) Frontend decides it wants a resolution change. Linux is made to stop updating the buffer. Not quite sure how to do this, but it seems like something which most real framebuffers would need to do during a resolution change. The frontend sends a start_change_res message to the backend with the desired new resolution.

3) The backend finishes off any pending updates, and picks up the start_change_res message. It can then unmap the buffer and send back a change_res_goahead message.

4) The frontend picks up the change_res_goahead message. It can now release the buffer area and allocate a new one of the correct size, and send a change_res_complete message to the correct size, and send a change_res_complete message to the backend. Once that's sent the frontend can go back to normal operation at the new resolution.

5) The backend picks up the change_res_complete message, remaps the buffer, and returns to normal operation.

change_res_complete is actually redundant, but will simplify the backend enough that it's probably worthwhile.

If you use a protocol like this, then you could have the frontend send a start_change_res as the first thing after it starts up, and communicate the information that way. This also gives you some scope for negotiating a lower res if the backend can't cope with the requested one for some reason.

Quoted 2 lines

The other way would be to do this through xenbus, but synchronising with the data path would be a bit of a pain.

Anthony, would you like to comment on this?

Quoted 7 lines

linux-2.6-xen-sparse/include/linux/xenfb.h line 106

Quoted 6 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +6 -0
+
+	__u32 in_cons, in_prod;
+	__u32 out_cons, out_prod;
+};
+
+void xenfb_resume(void);

No longer exists.

Removed.

Quoted 6 lines

linux-2.6-xen-sparse/include/linux/xenkbd.h

Quoted 5 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +2 -0
+
+#endif

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/include/linux/xenkbd.h

linux-2.6-xen-sparse/include/linux/xenkbd.h new file +0 -0

Again, this belongs in xen/include/public/io.

Okay.

Quoted 25 lines
Quoted 25 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +24 -0
@@ -0,0 +1,92 @@
+/*
+ * linux/include/linux/xenkbd.h -- Xen virtual keyboard/mouse
+ *
+ * Copyright (C) 2005
+ *
+ *      Anthony Liguori <[email protected]>
+ *
+ *  This file is subject to the terms and conditions of the GNU General Public
+ *  License. See the file COPYING in the main directory of this archive for
+ *  more details.
+ */
+
+#ifndef _LINUX_XENKBD_H
+#define _LINUX_XENKBD_H
+
+#include <asm/types.h>
+
+/* in events */
+
+#define XENKBD_IN_EVENT_SIZE 40
+
+#define XENKBD_TYPE_MOTION  1     /* mouse movement event */
+#define XENKBD_TYPE_BUTTON  2     /* mouse button event */
+#define XENKBD_TYPE_KEY     3     /* keyboard event */
Quoted 8 lines

linux-2.6-xen-sparse/include/linux/xenkbd.h line 31

Quoted 7 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +7 -0
+
+struct xenkbd_motion
+{
+	__u8 type;         /* XENKBD_TYPE_MOTION */
+	__s16 rel_x;       /* relative X motion */
+	__s16 rel_y;       /* relative Y motion */
+};

Again, this is a slightly ugly way of doing tagged unions.

See above.

Quoted 18 lines
Quoted 18 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +18 -0
+
+struct xenkbd_button
+{
+	__u8 type;         /* XENKBD_TYPE_BUTTON */
+	__u8 pressed;      /* 1 if pressed; 0 otherwise */
+	__u8 button;       /* the button (0, 1, 2 is right, middle, left) */
+};
+
+struct xenkbd_key
+{
+	__u8 type;         /* XENKBD_TYPE_KEY */
+	__u8 pressed;      /* 1 if pressed; 0 otherwise */
+	__u16 keycode;     /* KEY_* from linux/input.h */
+};
+
+union xenkbd_in_event
+{
+	__u8 type;
Quoted 8 lines

linux-2.6-xen-sparse/include/linux/xenkbd.h line 56

Quoted 7 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +7 -0
+	struct xenkbd_motion motion;
+	struct xenkbd_button button;
+	struct xenkbd_key key;
+	char _[XENKBD_IN_EVENT_SIZE];
+};
+
+/* out events */

Maybe a comment that none are currently defined?

Sure.

Quoted 27 lines
Quoted 27 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +27 -0
+
+#define XENKBD_OUT_EVENT_SIZE 40
+
+union xenkbd_out_event
+{
+	__u8 type;
+	char _[XENKBD_OUT_EVENT_SIZE];
+};
+
+/* shared page */
+
+#define XENKBD_IN_RING_SIZE 2048
+#define XENKBD_IN_RING_LEN (XENKBD_IN_RING_SIZE / XENKBD_IN_EVENT_SIZE)
+#define XENKBD_IN_RING_OFFS 1024
+#define XENKBD_IN_RING(page) \
+    ((union xenkbd_in_event *)((char *)(page) + XENKBD_IN_RING_OFFS))
+#define XENKBD_IN_RING_REF(page, idx) \
+    (XENKBD_IN_RING((page))[(idx) % XENKBD_IN_RING_LEN])
+
+#define XENKBD_OUT_RING_SIZE 1024
+#define XENKBD_OUT_RING_LEN (XENKBD_OUT_RING_SIZE / XENKBD_OUT_EVENT_SIZE)
+#define XENKBD_OUT_RING_OFFS (XENKBD_IN_RING_OFFS + XENKBD_IN_RING_SIZE)
+#define XENKBD_OUT_RING(page) \
+    ((union xenkbd_out_event *)((char *)(page) + XENKBD_OUT_RING_OFFS))
+#define XENKBD_OUT_RING_REF(page, idx) \
+    (XENKBD_OUT_RING((page))[(idx) % XENKBD_OUT_RING_LEN])
+
Quoted 8 lines

linux-2.6-xen-sparse/include/linux/xenkbd.h line 90

Quoted 7 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +7 -0
+struct xenkbd_info
+{
+	__u32 in_cons, in_prod;
+	__u32 out_cons, out_prod;
+};
+
+void xenkbd_resume(void);

No longer exists.

Removed.

Quoted 4 lines

linux-2.6-xen-sparse/include/linux/xenkbd.h line 92

Quoted 2 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +2 -0
+
+#endif

Steven.

#10 lore raw
Quoted 7 lines
Quoted 6 lines

Okay, this fixes a lot of the things I complained about last time, and most of the new problems should be fairly easy to fix up.

There are still a couple of things from the last round which are unfixed. This is mostly my fault for going on holiday at an inconvenient time; sorry about that.

I'm about to commit the same crime :) Expect me back on the 19th.

Have fun.

Quoted 12 lines
Quoted 9 lines

The big problem with this is that you don't support shadow translate mode guests, because the frontend exposes mfns directly to the backend. The correct way of fixing this is to use grant tables, but, as Anthony pointed out, that's quite hard due to the amount of memory you need to grant access to.

The easy way of doing this would be to just increase the size of the grant tables. Alternatively, you could add support for granting access to ranges of memory, but that sounds like a whole lot of work.

Yes, it would be great if Xen provided the means to do it right, but as Keir wrote that's post 3.0.3.

Quoted 7 lines
Quoted 3 lines

You could also put in a quick hack of just translating the mfns in the backend if the frontend is in shadow translate mode, but that's not really very nice.

As far as I can see, this is our only option right now. I'm ignorant of `shadow translate mode'; can somebody please educate me on how to detect and translate shadow translate mode frontend mfns in the backend?

Shadow translate mode is a mode of the shadow page tables in which Xen does machine<->physical address translations on behalf of the guest. It's mostly used for HVM guests to give them the impression of a mostly-contiguous block of memory starting at machine address 0.

If you just bung the guest-supplied machine frame numbers through xc_translate_gpfn_list you should be fine. That does the translation if you're in shadow translate mode and does nothing if you're not.

Quoted 5 lines
Quoted 3 lines

It'd also be nice if the protocol supported putting the backend in an unprivileged domain, which grant tables would give you for free, but that's a lower priority.

I guess that has to wait until we can do the framebuffer mapping nicely.

Yep.

Quoted 10 lines
Quoted 4 lines

Aside from that, the lack of support for multiple framebuffers in one guest is kind of annoying. It's not necessary to have this working in a first cut of the patch, but it'd be good if the protocol included some way of adding it in later, which the current one doesn't.

Well, the only place that limits us to a single framebuffer is our use of xenstore, isn't it?. All we need for additional framebuffers then is new xenstore directories, say vfbN and vkbdN, where N is 0, 1, ...

If you feel it's useful, I can rename the xenstore directories to vfb0 and vkbd0 now.

Yes, please. It'd be good not to have to break the protocol in order to add this in the future. I'd prefer vfb/0 and vkbd/0, though, for consistency with other types of devices.

Quoted 8 lines
Quoted 4 lines

Hard coding the resolution is also kind of icky, but is again acceptable for a first cut. It'd be good to have at least a plan for how this is supposed to work before the thing gets merged, including how to change the resolution of a live framebuffer.

It's always good to have a plan for future work. Note, however, that the protocol is easily extensible, and therefore we don't have to fear coding ourselves into a box now that we have to break later, just for lack of foresight.

True.

Quoted 5 lines
Quoted 3 lines

Also, the spec you pointed me at last time appears to be out of date:
it still thinks you get the pgdir mfn from start info, which certainly isn't acceptable for new drivers.

Yes, it's out of date. Let's nail down things here, and if we then feel the spec is useful to have, we update it.

Good plan.

Quoted 48 lines
Quoted 40 lines
Quoted 40 lines

/*
* Do a quick page-table lookup for a single page.
diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/xenfb/Makefile

linux-2.6-xen-sparse/drivers/xen/xenfb/Makefile new file +1 -0
@@ -0,0 +1,1 @@
+obj-$(CONFIG_XEN_FRAMEBUFFER)	:= xenfb.o

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +29 -0
@@ -0,0 +1,571 @@
+/*
+ * linux/drivers/video/xenfb.c -- Xen para-virtual frame buffer device
+ *
+ * Copyright (C) 2005-2006
+ *
+ *      Anthony Liguori <[email protected]>
+ *
+ *  Based on linux/drivers/video/q40fb.c
+ *
+ *  This file is subject to the terms and conditions of the GNU General Public
+ *  License. See the file COPYING in the main directory of this archive for
+ *  more details.
+ */
+
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/fb.h>
+#include <linux/module.h>
+#include <linux/vmalloc.h>
+#include <linux/mm.h>
+#include <asm/hypervisor.h>
+#include <xen/evtchn.h>
+#include <xen/xenbus.h>
+#include <linux/xenfb.h>
+#include <linux/kthread.h>
+
+#define XENFB_WIDTH 800
+#define XENFB_HEIGHT 600
+#define XENFB_DEPTH 32
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 36

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+
+static int xenfb_fps = 20;
+static unsigned long xenfb_mem_len = XENFB_WIDTH * XENFB_HEIGHT * XENFB_DEPTH / 8;
+
+struct xenfb_mapping
+{
+	struct list_head	next;

Perhaps not the best name for a list head?

Quoted 3 lines

The mappings are kept in a list of struct xenfb_mapping, chained through member next. I've seen worse names. Feel free to suggest a better one.

Well, list_heads are double linked, so next is kind of deceptive. Perhaps just ``list''?

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+	struct vm_area_struct	*vma;
+	atomic_t		map_refs;
+	int			faults;
+	struct xenfb_info	*info;
+};
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 48

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+
+struct xenfb_info
+{
+	struct task_struct		*kthread;
+	wait_queue_head_t		wq;
+
+	unsigned char			*fb;

Why unsigned char?

Quoted 2 lines

I think this is a matter of style. Plain char suggests characters, while unsigned char suggests bytes. fb points to an array of bytes.

I'd have gone for void *, but, as you say, matter of style.

Quoted 5 lines
Quoted 4 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 50

Quoted 2 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +2 -0
+	struct fb_fix_screeninfo	*fix;
+	struct fb_var_screeninfo	*var;

Not quite sure what these are for. Why not just get at them through the fb_info pointer?

Fine with me.

Thanks.

Quoted 8 lines
Quoted 7 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 55

Quoted 6 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+	struct fb_info			*fb_info;
+	struct timer_list		refresh;
+	int				dirty;
 
+	int				y1, y2;
+	int				x1, x2;

These could perhaps do with slightly more descriptive names?

Would you settle for a descriptive comment instead?

Sure.

Quoted 6 lines
Quoted 5 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 2 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +2 -0
+
+	struct semaphore		mm_lock;

That's going to confuse people.

(Me, for a start)

What exactly confuses you? The name? Could rename to mm_sem.

Sorry, got my wires crossed a bit here. Solaris has a global mm_lock, and Linux used to, and I thought Linux still did. Ignore me, I'm wibbling.

Quoted 11 lines
Quoted 10 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 66

Quoted 9 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +9 -0
+	int				nr_pages;
+	struct page			**pages;
+	struct list_head		mappings;
+
+	unsigned			evtchn;
+	int				irq;
+	struct xenfb_page		*page;
+	unsigned long 			*mfns;
+	u32				flags;

Maybe a comment to say what sort of flags go in here?

Sure.

Thanks.

Quoted 20 lines
Quoted 12 lines
Quoted 12 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +12 -0
+};
+
+static void xenfb_do_update(struct xenfb_info *info,
+			    int x, int y, int w, int h)
+{
+	union xenfb_out_event event;
+	__u32 prod;
+
+	event.type = XENFB_TYPE_UPDATE;
+	event.update.x = x;
+	event.update.y = y;
+	event.update.width = w;
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 83

Quoted 5 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+	event.update.height = h;
+
+	prod = info->page->out_prod;
+	if (prod - info->page->out_cons == XENFB_OUT_RING_LEN)
+		return;		/* ring buffer full, event lost */

It seems to me like if we lose the event, we probably shouldn't mark the framebuffer as clean and throw away the update region. Perhaps the caller should instead reschedule the timer?

Quoted 4 lines

Actually, this can't happen. xenfb_update_screen() ensures there's space in the ring before it proceeds to walk the mappings. That space can't go away, because no other place puts stuff into the ring, and only one instance of xenfb_do_update() can run (in the kernel thread).

Okay.

Quoted 2 lines
Quoted 1 line

Also, is there any reason the test couldn't use xenfb_queue_full?

I removed the redundant test instead.

Thanks.

Quoted 37 lines
Quoted 29 lines
Quoted 29 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +29 -0
+	mb();			/* ensure ring space available */
+	XENFB_OUT_RING_REF(info->page, prod) = event;
+	wmb();			/* ensure ring contents visible */
+	info->page->out_prod = prod + 1;
+
+	notify_remote_via_evtchn(info->evtchn);
+}
+
+static int xenfb_queue_full(struct xenfb_info *info)
+{
+	__u32 cons, prod;
+
+	prod = info->page->out_prod;
+	cons = info->page->out_cons;
+	return prod - cons == XENFB_OUT_RING_LEN;
+}
+
+static void xenfb_update_screen(struct xenfb_info *info)
+{
+	int y1, y2, x1, x2;
+	struct list_head *item;
+	struct xenfb_mapping *map;
+
+	if (!(info->flags & XENFB_FLAG_UPDATE))
+		return;
+	if (xenfb_queue_full(info))
+		return;
+
+	y1 = info->y1;
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 119

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+	y2 = info->y2;
+	x1 = info->x1;
+	x2 = info->x2;
+	info->y1 = info->y2 = info->x1 = info->x2 = 0;
+	down(&info->mm_lock);
+	list_for_each(item, &info->mappings) {
+		map = list_entry(item, struct xenfb_mapping, next);

list_for_each_entry, perhaps?

Quoted 1 line

Going to try.

Thanks.

Quoted 26 lines
Quoted 26 lines
Quoted 6 lines
Quoted 3 lines

Could this end up running at the same time as update_screen and race to update dirty? I suppose it doesn't actually matter too much if it does.

xenfb_timer() runs from the timer interrupt, sets dirty, then kicks the wait queue.

Quoted 8 lines

A separate kernel thread running xenfb_thread() sleeps on the wait queue. When it wakes up, and dirty is set, it clears dirty and updates the screen. Then goes back to sleep.

Obviously, dirty is always set when the thread wakes up. If xenfb_timer() runs before the thread goes to sleep again, the wakeup is lost, and the value of dirty doesn't matter. I believe that's okay.

It looks to me like we can lose updates to the screen in that case:

xenfb_timer runs, sets dirty to 1, kicks the thread
xenfb_thread wakes up xenfb_thread reads the update region
another update happens, extends the update region, reschedules xenfb_timer xenfb_timer runs again, sets dirty to 2, kicks the thread again
xenfb_thread sets dirty to 0 xenfb_thread sends an update using
the old region

Quoted 4 lines
Quoted 2 lines

I suppose the race is sufficiently unlikely and sufficiently mild that it can probably be ignored for all realistic purposes.

This can't happen, because both use and update of the mappings is protected by mm_lock:

No it isn't. Look at xenfb_update_screen: we read and reset the update area, and then acquire the lock immediately afterwards.

Quoted 40 lines

xenfb_timer runs, sets dirty to 1, kicks the thread
xenfb_thread wakes up xenfb_thread sets dirty to 0

xenfb_thread down mm_lock xenfb_thread reads the update region xenfb_thread up mm_lock

xenfb_thread sends an update using
the old region

Possibly concurrent:

another update happens:
down mm_lock
extends the update region, reschedules xenfb_timer up mm_lock

xenfb_timer runs again, sets dirty to 1, kicks the thread again

Harmless race: dirty can conceivably become 1 when there's no work to do. Then xenfb_thread wakes up, finds nothing to do and goes back to sleep.

Looks safe to me.

Quoted 10 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 184

Quoted 3 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +3 -0
+
+static void xenfb_refresh(struct xenfb_info *info,
+			  int x1, int y1, int w, int h)

Is there any reason why there can't be multiple calls to this function live at the same time? In particular, if vm_nopage and xenfb_copyarea happen at the same time it looks like we can lose part of the update area.

Also, vm_nopage holds the mm_lock when it calls this, whereas the other callers appear not to. Was this deliberate?

The question is what mm_lock protects. If it protects info->mappings, then xenfb_refresh() doesn't care about it, as it doesn't access it.

Quoted 7 lines

If it should protect x1, x2, y1, y2 as well, then locking looks flawed.

x1, x2, y1, y2 need protection if they can be updated concurrently. Users are xenfb_update_screen() and xenfb_refresh(). The latter runs from fb_ops methods and vm_operations_struct method nopage. Looks like they need protection, doesn't it? Anthony?

Once you've figured this out, could you put a comment somewhere sensible, please?

Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +8 -0
+{
+	int y2, x2;
+
+	y2 = y1 + h;
+	x2 = x1 + w;
+	if (info->y2 == 0) {
+		info->y1 = y1;
+		info->y2 = y2;
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 197

Quoted 5 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+	}
+	if (info->x2 == 0) {
+		info->x1 = x1;
+		info->x2 = x2;
+	}

Presumably, these are just looking to see if there's a pre-existing region, and if not copying the new region across verbatim? Could you test dirty instead in that case?

Quoted 2 lines

What about initializing x1, y1 with an impossibly small value, and x2, y2 with an impossibly large value?

If you do it the other way around, that would be ideal.

Quoted 18 lines
Quoted 10 lines
Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+
+	if (info->y1 > y1)
+		info->y1 = y1;
+	if (info->y2 < y2)
+		info->y2 = y2;
+	if (info->x1 > x1)
+		info->x1 = x1;
+	if (info->x2 < x2)
+		info->x2 = x2;
+
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 211

Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +4 -0
+	if (timer_pending(&info->refresh))
+		return;
+
+	mod_timer(&info->refresh, jiffies + HZ/xenfb_fps);

Would it be sensible to have a fallback so that an update is always sent to the backend e.g. 100ms after any change to the framebuffer, so that the user still sees something when the display is being updated very frequently?

Quoted 2 lines

Could this be done in a later revision? Happy to add the idea in a comment now.

xenfb_fps is only 20. 20fps is hardly an excessively fast animation, so I'd expect the display to lock up under some reasonably common and plausible situations with the current design. That isn't really good enough.

It's not a terribly hard thing to fix:

if (jiffies - info->last_update_sent > HZ/10)
xenfb_update_screen(info)
else

	mod_timer(&info->refresh, jiffies + HZ/xenfb_fps);

and then add an

info->last_update_sent = jiffies;

to xenfb_update_screen. You might need to move the locking around a little in update_screen, and maybe move the dirty test in there from xenfb_thread, but that's about it.

Quoted 11 lines
Quoted 10 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 318

Quoted 9 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +9 -0
+	map_pages = (vma->vm_end - vma->vm_start + PAGE_SIZE-1) >> PAGE_SHIFT;
+	if (map_pages > info->nr_pages)
+		goto out;
+
+	ret = -ENOMEM;
+	map = kmalloc(sizeof(*map), GFP_KERNEL);
+	if (map == NULL)
+		goto out;
+	memset(map, 0, sizeof(*map));

kzalloc, maybe?

Sure.

Thanks.

Quoted 9 lines
Quoted 7 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 371

Quoted 6 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +6 -0
+	return IRQ_HANDLED;
+}
+
+static unsigned long vmalloc_to_mfn(void *address)
+{
+	// FIXME was return pfn_to_mfn(vmalloc_to_pfn(address));

Not quite sure why this comment is here.

Accident. However, arbitrary_virt_to_machine() doesn't appear to exist on ia64, so I went back to the old code.

Fair enough.

Quoted 10 lines
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 377

Quoted 6 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +6 -0
+	return arbitrary_virt_to_machine(address) >> PAGE_SHIFT;
+}
+
+static struct xenfb_info *xenfb_info;
+
+static int __init xenfb_probe(void)

I'd find it easier to be confident of the error handling here if the function were split up a little.

It's just the way I found it. My own idea of cleanup would be to make xenfb_cleanup() work for the error case.

That would also be a good thing. It would be even better if you could do both. :)

Quoted 22 lines
Quoted 14 lines
Quoted 14 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +14 -0
+
+	info = kmalloc(sizeof(*info), GFP_KERNEL);
+	if (info == NULL)
+		return -ENOMEM;
+	memset(info, 0, sizeof(*info));
+
+	INIT_LIST_HEAD(&info->mappings);
+
+	info->fb = vmalloc(xenfb_mem_len);
+	if (info->fb == NULL)
+		goto error;
+	memset(info->fb, 0, xenfb_mem_len);
+	info->nr_pages = (xenfb_mem_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
+	info->pages = kmalloc(sizeof(struct page*)*info->nr_pages, GFP_KERNEL);
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 416

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+	if (info->pages == NULL)
+		goto error_vfree;
+	for (i = 0; i < info->nr_pages; i++)
+		info->pages[i] = vmalloc_to_page(info->fb + i * PAGE_SIZE);
+
+	fb_info = framebuffer_alloc(sizeof(u32) * 256, NULL);
+	// FIXME sizeof(struct xenfb_info)

Where did sizeof(u32) * 256 come from? What does the comment mean?

Quoted 3 lines

I *suspect* that the correct argument is sizeof(struct xenfb_info), but can't yet exclude subtle, evil hackery going on here, so I put in a comment for now. Anthony, do you remember hackery here?

I think it's actually the size of the pseudo palette, but I wouldn't swear to it.

Quoted 2 lines
Quoted 1 line

fb_info isn't released from the error path.

Fixing...

Thanks.

Quoted 7 lines
Quoted 6 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 420

Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +4 -0
+	if (fb_info == NULL)
+		goto error_kfree;
+
+	info->mfns = vmalloc(sizeof(unsigned long) * info->nr_pages);

What happens if this fails? Also, you never seem to vfree this if we hit an error later on.

Fixing...

Thanks.

Quoted 19 lines
Quoted 11 lines
Quoted 11 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +11 -0
+	/* set up shared page */
+	info->page = (void *)__get_free_page(GFP_KERNEL);
+	if (!info->page)
+		goto error_kfree;
+	/* set up event channel */
+	alloc_unbound.dom = DOMID_SELF;
+	alloc_unbound.remote_dom = 0;
+	ret = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
+					  &alloc_unbound);
+	if (ret)
+		goto error_freep;
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 436

Quoted 5 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+	info->evtchn = alloc_unbound.port;
+
+	for (i = 0; i < info->nr_pages; i++)
+		info->mfns[i] = vmalloc_to_mfn(info->fb + i * PAGE_SIZE);
+	info->page->pd[0] = vmalloc_to_mfn(info->mfns);

What happens if this domain is running in shadow translate mode? It looks to me like the backend will end up mapping completely the wrong frame.

Quoted 3 lines

Discussion above seems to suggest that our only option is to have the backend detect and translate shadow translate mode frontends, so no action is required here. Correct?

Agreed.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +14 -0
+	info->page->pd[1] = 0;
+	info->page->width = XENFB_WIDTH;
+	info->page->height = XENFB_HEIGHT;
+	info->page->depth = XENFB_DEPTH;
+	info->page->line_length = (info->page->depth / 8) * info->page->width;
+	info->page->mem_length = xenfb_mem_len;
+	info->page->in_cons = info->page->in_prod = 0;
+	info->page->out_cons = info->page->out_prod = 0;
+
+	ret = bind_evtchn_to_irqhandler(info->evtchn, xenfb_event_handler,
+					0, "xenfb", info);
+	if (ret < 0) {
+		close.port = info->evtchn;
+		HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
Quoted 8 lines
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 458

Quoted 8 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +8 -0
+		goto error_freep;
+	}
+
+	info->irq = ret;
+	xenfb_info = info;
+
+	fb_info->pseudo_palette = fb_info->par;
+	fb_info->par = info;

Eh? What was the reasoning behind this?

Quoted 6 lines
Quoted 5 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 462

Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +4 -0
+	fb_info->screen_base = info->fb;
+
+	memset(&fb_info->var, 0, sizeof(fb_info->var));
+	memset(&fb_info->fix, 0, sizeof(fb_info->fix));

I think these are already guaranteed to be zero, aren't they?

Yes, framebuffer_alloc() clears *fb_info. Happy to rely on that?

I can't see any reason not to.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +4 -0
+	fb_info->fix.visual = FB_VISUAL_TRUECOLOR;
+	fb_info->fix.line_length = info->page->line_length;
+	fb_info->fix.smem_start = 0;
+	fb_info->fix.smem_len = xenfb_mem_len;
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 488

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+	strcpy(fb_info->fix.id, "xen");
+	fb_info->fix.type = FB_TYPE_PACKED_PIXELS;
+	fb_info->fix.accel = FB_ACCEL_NONE;
+
+	fb_info->flags = FBINFO_FLAG_DEFAULT;
+
+	fb_alloc_cmap(&fb_info->cmap, 256, 0);

This can fail, and the error paths don't bother to deallocate it.

Quoted 1 line

Fixing...

Thanks.

Quoted 10 lines
Quoted 10 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 514

Quoted 8 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +8 -0
+
+ again:
+	ret = xenbus_transaction_start(&xbt);
+	if (ret)
+		goto error_unreg;
+	ret = xenbus_printf(xbt, "vfb", "page-ref", "%lu",
+			    virt_to_mfn(info->page));
+	// FIXME grant tables?

Again, what happens in shadow translate mode?

Quoted 3 lines
Quoted 2 lines

Also, it's kind of annoying that there's no support for multiple framebuffers in a domU. That's very much a wishlist thing, though.

See above.

Ack.

Quoted 6 lines
Quoted 5 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 536

Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +4 -0
+	unregister_framebuffer(fb_info);
+ error_unbind:
+	unbind_from_irqhandler(info->irq, info);
+	// FIXME do we have to stop info->kthread?

If you've started it you certainly need to stop it.

Fixing...

Thanks.

Quoted 10 lines
Quoted 10 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 552

Quoted 6 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +6 -0
+	return -ENODEV;
+}
+
+static int __init xenfb_init(void)
+{
+	return xenfb_probe();

From last time:

Quoted 1 line
Quoted 1 line

You might want to consider using xenbus_device and friends here.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I considered that, but it wasn't obvious to me how to make it fit cleanly to a user-space backend. Care to advise?

I think if the frontend needs to know whether the backend is in kernel or userspace then we've messed up somewhere. Why do you expect this to be difficult?

I didn't *expect* it to be difficult, I just couldn't see how to fit Anthony's code to xenbus_device without rewriting more than I like, and I still can't see it. That's why I asked for advice.

The probing stuff seems to be fairly self-contained at the moment. You'd probably have to rewrite xenfb_probe, but I can't see any need to change much beyond that.

Unless I'm just missing something?

Quoted 3 lines

Anyway, I'm reluctant to throw out working code while there's so much code that needs fixing. I may be less reluctant once we've drained the swamp some more.

So I think long term we'd probably like to use the standard xenbus device setup protocol, going through XenbusStateInitialised, XenbusStateConnected, XenbusStateClosing, etc. That'd be a non-backwards-compatible change if we do it later, so it'd be good to get it in now. xenbus_device gives you that for free, although I'll admit it is a bit of a pain to use.

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+}
+
+static void __exit xenfb_cleanup(void)
+{
+	struct xenfb_info *info = xenfb_info;
+
+	unregister_framebuffer(info->fb_info);
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 545

Quoted 6 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +6 -0
+	unbind_from_irqhandler(info->irq, info);
+	free_page((unsigned long)info->page);
+	kfree(info->pages);
+	vfree(info->fb);
+	kfree(info);
+	xenfb_info = NULL;

I don't think this releases all of the memory you allocate in xenfb_probe.

Quoted 1 line

Fixing...

Thanks.

Quoted 10 lines
Quoted 9 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 395

Quoted 8 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +8 -0
+	  return -ENODEV;
+#if 0
+        /* if we're not set up to use graphics mode, then don't initialize */
+	if (xenbus_scanf(XBT_NIL, "console", "use_graphics", "%d", &ret) < 0)
+	  return -ENODEV;
+	if (ret == 0)
+	  return -ENODEV;
+#endif

Not sure about this.

Same as for xenfb.c. The #if is an accident.

Ack.

Quoted 10 lines
Quoted 9 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 5 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+
+	dev = kmalloc(sizeof(*dev), GFP_KERNEL);
+	input_dev = input_allocate_device();
+	if (!dev || !input_dev)
+		return -ENOMEM;

You potentially leak memory if one of these fails and the other succeeds.

Also, the error paths don't release input_dev. In fact, nothing ever releases input_dev, unless I'm missing something.

Fixing...

Thanks.

Quoted 20 lines
Quoted 12 lines
Quoted 12 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +12 -0
+
+	dev->dev = input_dev;
+	dev->info = (void *)__get_free_page(GFP_KERNEL);
+	if (!dev->info) {
+		ret = -ENOMEM;
+		goto error;
+	}
+
+	alloc_unbound.dom = DOMID_SELF;
+	alloc_unbound.remote_dom = 0;
+	ret = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
+					  &alloc_unbound);
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+	if (ret)
+		goto error_freep;
+	dev->evtchn = alloc_unbound.port;
+	ret = bind_evtchn_to_irqhandler(dev->evtchn, input_handler, 0,
+					"xenkbd", dev);
+	if (ret < 0)
+		goto error_freep;

You might want to close the event channel here.

Quoted 1 line

Fixing...

Thanks.

Quoted 6 lines
Quoted 5 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +4 -0
+
+	input_dev->name = "Xen Virtual Keyboard/Mouse";
+
+	input_register_device(input_dev);

This can fail.

Most uses elsewhere in the kernel don't care. Fixing anyway.

Yeah, I know, but it's good to get it right anyway. Thanks.

Quoted 9 lines
Quoted 9 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+
+ again:
+	ret = xenbus_transaction_start(&xbt);
+	if (ret)
+		goto error_unreg;
+	ret = xenbus_printf(xbt, "vkbd", "page-ref", "%lu",
+			    virt_to_mfn(dev->info));

There's no reason that I can see not to use grant tables here, and that'd avoid all of the potential issues with shadow translate mode.

Quoted 4 lines

Since we have to fool around with shadow translate mode anyway for sharing the framebuffer, I don't quite see why we need to address this right now. This code works, and there's no shortage of code that needs fixing.

Alright, leave this for now and fix it when you fix the vfb equivalent bits.

Quoted 1 line

Same for vfb/page-ref.

Well, that's a reference to a bunch of untranslated frame numbers, so it makes more sense for it to be untranslated itself. The keyboard page doesn't require any further translation.

Quoted 7 lines
Quoted 6 lines

linux-2.6-xen-sparse/include/linux/xenfb.h

Quoted 4 lines

+MODULE_LICENSE("GPL");
diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/include/linux/xenfb.h

linux-2.6-xen-sparse/include/linux/xenfb.h new file +0 -0

This is defining an io protocol, so belongs in xen/include/public/io

Okay.

Thanks.

Quoted 24 lines
Quoted 24 lines

linux-2.6-xen-sparse/include/linux/xenfb.h line 24

Quoted 9 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +9 -0
+#include <asm/types.h>
+
+/* out events */
+
+#define XENFB_OUT_EVENT_SIZE 40
+
+#define XENFB_TYPE_MOTION 1
+#define XENFB_TYPE_UPDATE 2
+

The other tagged unions in Xen look more like this:

struct {
	u8 type;
	union {
		struct {
			u32 field1;
			u32 field2;
		} type1;
		struct {
			u16 field1;
		} type2;
	} u;
};
Quoted 4 lines
Quoted 3 lines

I'd find this a lot easier to read than the current approach of putting the type into the individual event type structures. Is there any reason why this couldn't be used here?

Matter of taste. Will change it if you insist.

It'd be good to be consistent with the other protocols.

Quoted 9 lines
Quoted 7 lines

linux-2.6-xen-sparse/include/linux/xenfb.h line 30

Quoted 6 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +6 -0
+struct xenfb_motion	    /* currently unused */
+{
+	__u8 type;          /* XENFB_TYPE_MOTION */
+	__u16 x;            /* The new x coordinate */
+	__u16 y;            /* The new y coordinate */
+};

I think if the comment said what was moving this'd be a lot clearer.

I'm not into documenting unused code I didn't write and don't understand, so I'm going to remove this.

Even better.

Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +9 -0
+
+struct xenfb_update
+{
+	__u8 type;          /* XENFB_TYPE_UPDATE */
+	__u16 x;            /* source x */
+	__u16 y;            /* source y */
+	__u16 width;        /* rect width */
+	__u16 height;       /* rect height */
+};
Quoted 8 lines

linux-2.6-xen-sparse/include/linux/xenfb.h line 46

Quoted 7 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +7 -0
+
+union xenfb_out_event
+{
+	__u8 type;
+	struct xenfb_motion motion;
+	struct xenfb_update update;
+	char _[XENFB_OUT_EVENT_SIZE];

Perhaps ``pad'' would be clearer than ``_''?

Quoted 1 line

Works for me.

Thanks.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +4 -0
+struct xenfb_page
+{
+	__u16 width;         /* the width of the framebuffer (in pixels) */
+	__u16 height;        /* the height of the framebuffer (in pixels) */
Quoted 8 lines

linux-2.6-xen-sparse/include/linux/xenfb.h line 100

Quoted 6 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +6 -0
+	__u32 line_length;   /* the length of a row of pixels (in bytes) */
+	__u32 mem_length;    /* the length of the framebuffer (in bytes) */
+	__u8 depth;          /* the depth of a pixel (in bits) */
+
+	unsigned long pd[2];	/* FIXME rename to pgdir? */
+	/* FIXME pd[1] unused at this time, shrink? */

I think it'd be enough to just give some indication of where the magic number 2 came from.

Quoted 1 line

Okay.

Thanks.

Quoted 8 lines
Quoted 7 lines

linux-2.6-xen-sparse/include/linux/xenfb.h line 106

Quoted 6 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +6 -0
+
+	__u32 in_cons, in_prod;
+	__u32 out_cons, out_prod;
+};
+
+void xenfb_resume(void);

No longer exists.

Removed.

Thanks.

Quoted 7 lines
Quoted 6 lines

linux-2.6-xen-sparse/include/linux/xenkbd.h

Quoted 5 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +2 -0
+
+#endif

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/include/linux/xenkbd.h

linux-2.6-xen-sparse/include/linux/xenkbd.h new file +0 -0

Again, this belongs in xen/include/public/io.

Okay.

Thanks.

Quoted 11 lines
Quoted 11 lines

linux-2.6-xen-sparse/include/linux/xenkbd.h line 56

Quoted 10 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +10 -0
+union xenkbd_in_event
+{
+	__u8 type;
+	struct xenkbd_motion motion;
+	struct xenkbd_button button;
+	struct xenkbd_key key;
+	char _[XENKBD_IN_EVENT_SIZE];
+};
+
+/* out events */

Maybe a comment that none are currently defined?

Quoted 1 line

Sure.

Thanks.

Quoted 9 lines
Quoted 8 lines

linux-2.6-xen-sparse/include/linux/xenkbd.h line 90

Quoted 7 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +7 -0
+struct xenkbd_info
+{
+	__u32 in_cons, in_prod;
+	__u32 out_cons, out_prod;
+};
+
+void xenkbd_resume(void);

No longer exists.

Removed.

Thanks.

Steven.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#11 lore raw
Quoted 24 lines · Steven Smith
Quoted 7 lines
Quoted 6 lines

Okay, this fixes a lot of the things I complained about last time, and most of the new problems should be fairly easy to fix up.

There are still a couple of things from the last round which are unfixed. This is mostly my fault for going on holiday at an inconvenient time; sorry about that.

I'm about to commit the same crime :) Expect me back on the 19th.

Have fun.

Quoted 15 lines
Quoted 9 lines

The big problem with this is that you don't support shadow translate mode guests, because the frontend exposes mfns directly to the backend. The correct way of fixing this is to use grant tables, but, as Anthony pointed out, that's quite hard due to the amount of memory you need to grant access to.

The easy way of doing this would be to just increase the size of the grant tables. Alternatively, you could add support for granting access to ranges of memory, but that sounds like a whole lot of work.

Yes, it would be great if Xen provided the means to do it right, but as Keir wrote that's post 3.0.3.

Quoted 3 lines

You could also put in a quick hack of just translating the mfns in the backend if the frontend is in shadow translate mode, but that's not really very nice.

Quoted 8 lines
Quoted 4 lines

As far as I can see, this is our only option right now. I'm ignorant of `shadow translate mode'; can somebody please educate me on how to detect and translate shadow translate mode frontend mfns in the backend?

Shadow translate mode is a mode of the shadow page tables in which Xen does machine<->physical address translations on behalf of the guest. It's mostly used for HVM guests to give them the impression of a mostly-contiguous block of memory starting at machine address 0.

How mostly is `mostly used for HVM guests'? Is it actually used with PV guests? If not, coping with it is unnecessarily general --- I'd rather just detect it (just in case) and bail out.

Quoted 3 lines

If you just bung the guest-supplied machine frame numbers through xc_translate_gpfn_list you should be fine. That does the translation if you're in shadow translate mode and does nothing if you're not.

You mean xc_domain_translate_gpfn_list(), I suppose.

Quoted 15 lines
Quoted 5 lines
Quoted 3 lines

It'd also be nice if the protocol supported putting the backend in an unprivileged domain, which grant tables would give you for free, but that's a lower priority.

I guess that has to wait until we can do the framebuffer mapping nicely.

Yep.

Quoted 8 lines
Quoted 4 lines

Aside from that, the lack of support for multiple framebuffers in one guest is kind of annoying. It's not necessary to have this working in a first cut of the patch, but it'd be good if the protocol included some way of adding it in later, which the current one doesn't.

Well, the only place that limits us to a single framebuffer is our use of xenstore, isn't it?. All we need for additional framebuffers then is new xenstore directories, say vfbN and vkbdN, where N is 0, 1, ...

Quoted 5 lines
Quoted 2 lines

If you feel it's useful, I can rename the xenstore directories to vfb0 and vkbd0 now.

Yes, please. It'd be good not to have to break the protocol in order to add this in the future. I'd prefer vfb/0 and vkbd/0, though, for consistency with other types of devices.

Done.

Quoted 65 lines
Quoted 8 lines
Quoted 4 lines

Hard coding the resolution is also kind of icky, but is again acceptable for a first cut. It'd be good to have at least a plan for how this is supposed to work before the thing gets merged, including how to change the resolution of a live framebuffer.

It's always good to have a plan for future work. Note, however, that the protocol is easily extensible, and therefore we don't have to fear coding ourselves into a box now that we have to break later, just for lack of foresight.

True.

Quoted 5 lines
Quoted 3 lines

Also, the spec you pointed me at last time appears to be out of date:
it still thinks you get the pgdir mfn from start info, which certainly isn't acceptable for new drivers.

Yes, it's out of date. Let's nail down things here, and if we then feel the spec is useful to have, we update it.

Good plan.

Quoted 48 lines
Quoted 48 lines
Quoted 37 lines

/*
* Do a quick page-table lookup for a single page.
diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/xenfb/Makefile

linux-2.6-xen-sparse/drivers/xen/xenfb/Makefile new file +1 -0
@@ -0,0 +1,1 @@
+obj-$(CONFIG_XEN_FRAMEBUFFER)	:= xenfb.o

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +26 -0
@@ -0,0 +1,571 @@
+/*
+ * linux/drivers/video/xenfb.c -- Xen para-virtual frame buffer device
+ *
+ * Copyright (C) 2005-2006
+ *
+ *      Anthony Liguori <[email protected]>
+ *
+ *  Based on linux/drivers/video/q40fb.c
+ *
+ *  This file is subject to the terms and conditions of the GNU General Public
+ *  License. See the file COPYING in the main directory of this archive for
+ *  more details.
+ */
+
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/fb.h>
+#include <linux/module.h>
+#include <linux/vmalloc.h>
+#include <linux/mm.h>
+#include <asm/hypervisor.h>
+#include <xen/evtchn.h>
+#include <xen/xenbus.h>
+#include <linux/xenfb.h>
+#include <linux/kthread.h>
+

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 36

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+#define XENFB_WIDTH 800
+#define XENFB_HEIGHT 600
+#define XENFB_DEPTH 32
+
+static int xenfb_fps = 20;
+static unsigned long xenfb_mem_len = XENFB_WIDTH * XENFB_HEIGHT * XENFB_DEPTH / 8;
+
+struct xenfb_mapping
+{
+	struct list_head	next;

Perhaps not the best name for a list head?

Quoted 5 lines
Quoted 3 lines

The mappings are kept in a list of struct xenfb_mapping, chained through member next. I've seen worse names. Feel free to suggest a better one.

Well, list_heads are double linked, so next is kind of deceptive. Perhaps just ``list''?

Renamed to link.

Quoted 28 lines
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+	struct vm_area_struct	*vma;
+	atomic_t		map_refs;
+	int			faults;
+	struct xenfb_info	*info;
+};
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 48

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+
+struct xenfb_info
+{
+	struct task_struct		*kthread;
+	wait_queue_head_t		wq;
+
+	unsigned char			*fb;

Why unsigned char?

Quoted 2 lines

I think this is a matter of style. Plain char suggests characters, while unsigned char suggests bytes. fb points to an array of bytes.

I'd have gone for void *, but, as you say, matter of style.

Quoted 5 lines
Quoted 4 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 50

Quoted 2 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +2 -0
+	struct fb_fix_screeninfo	*fix;
+	struct fb_var_screeninfo	*var;

Not quite sure what these are for. Why not just get at them through the fb_info pointer?

Fine with me.

Thanks.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +3 -0
+	struct fb_info			*fb_info;
+	struct timer_list		refresh;
+	int				dirty;
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 55

Quoted 2 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +2 -0
+	int				y1, y2;
+	int				x1, x2;

These could perhaps do with slightly more descriptive names?

Would you settle for a descriptive comment instead?

Sure.

Done.

Quoted 90 lines
Quoted 6 lines
Quoted 5 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 57

Quoted 2 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +2 -0
+
+	struct semaphore		mm_lock;

That's going to confuse people.

(Me, for a start)

What exactly confuses you? The name? Could rename to mm_sem.

Sorry, got my wires crossed a bit here. Solaris has a global mm_lock, and Linux used to, and I thought Linux still did. Ignore me, I'm wibbling.

Quoted 11 lines
Quoted 10 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 66

Quoted 9 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +9 -0
+	int				nr_pages;
+	struct page			**pages;
+	struct list_head		mappings;
+
+	unsigned			evtchn;
+	int				irq;
+	struct xenfb_page		*page;
+	unsigned long 			*mfns;
+	u32				flags;

Maybe a comment to say what sort of flags go in here?

Sure.

Thanks.

Quoted 20 lines
Quoted 12 lines
Quoted 12 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +12 -0
+};
+
+static void xenfb_do_update(struct xenfb_info *info,
+			    int x, int y, int w, int h)
+{
+	union xenfb_out_event event;
+	__u32 prod;
+
+	event.type = XENFB_TYPE_UPDATE;
+	event.update.x = x;
+	event.update.y = y;
+	event.update.width = w;
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 83

Quoted 5 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+	event.update.height = h;
+
+	prod = info->page->out_prod;
+	if (prod - info->page->out_cons == XENFB_OUT_RING_LEN)
+		return;		/* ring buffer full, event lost */

It seems to me like if we lose the event, we probably shouldn't mark the framebuffer as clean and throw away the update region. Perhaps the caller should instead reschedule the timer?

Quoted 4 lines

Actually, this can't happen. xenfb_update_screen() ensures there's space in the ring before it proceeds to walk the mappings. That space can't go away, because no other place puts stuff into the ring, and only one instance of xenfb_do_update() can run (in the kernel thread).

Okay.

Quoted 2 lines
Quoted 1 line

Also, is there any reason the test couldn't use xenfb_queue_full?

I removed the redundant test instead.

Thanks.

Quoted 37 lines
Quoted 37 lines
Quoted 26 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +26 -0
+	mb();			/* ensure ring space available */
+	XENFB_OUT_RING_REF(info->page, prod) = event;
+	wmb();			/* ensure ring contents visible */
+	info->page->out_prod = prod + 1;
+
+	notify_remote_via_evtchn(info->evtchn);
+}
+
+static int xenfb_queue_full(struct xenfb_info *info)
+{
+	__u32 cons, prod;
+
+	prod = info->page->out_prod;
+	cons = info->page->out_cons;
+	return prod - cons == XENFB_OUT_RING_LEN;
+}
+
+static void xenfb_update_screen(struct xenfb_info *info)
+{
+	int y1, y2, x1, x2;
+	struct list_head *item;
+	struct xenfb_mapping *map;
+
+	if (!(info->flags & XENFB_FLAG_UPDATE))
+		return;
+	if (xenfb_queue_full(info))

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c line 119

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+		return;
+
+	y1 = info->y1;
+	y2 = info->y2;
+	x1 = info->x1;
+	x2 = info->x2;
+	info->y1 = info->y2 = info->x1 = info->x2 = 0;
+	down(&info->mm_lock);
+	list_for_each(item, &info->mappings) {
+		map = list_entry(item, struct xenfb_mapping, next);

list_for_each_entry, perhaps?

Quoted 2 lines
Quoted 1 line

Going to try.

Thanks.

Done.

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 6 lines
Quoted 3 lines

Could this end up running at the same time as update_screen and race to update dirty? I suppose it doesn't actually matter too much if it does.

xenfb_timer() runs from the timer interrupt, sets dirty, then kicks the wait queue.

Quoted 8 lines

A separate kernel thread running xenfb_thread() sleeps on the wait queue. When it wakes up, and dirty is set, it clears dirty and updates the screen. Then goes back to sleep.

Obviously, dirty is always set when the thread wakes up. If xenfb_timer() runs before the thread goes to sleep again, the wakeup is lost, and the value of dirty doesn't matter. I believe that's okay.

It looks to me like we can lose updates to the screen in that case:

xenfb_timer runs, sets dirty to 1, kicks the thread
xenfb_thread wakes up xenfb_thread reads the update region
another update happens, extends the update region, reschedules xenfb_timer xenfb_timer runs again, sets dirty to 2, kicks the thread again
xenfb_thread sets dirty to 0 xenfb_thread sends an update using
the old region

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

I suppose the race is sufficiently unlikely and sufficiently mild that it can probably be ignored for all realistic purposes.

This can't happen, because both use and update of the mappings is protected by mm_lock:

No it isn't. Look at xenfb_update_screen: we read and reset the update area, and then acquire the lock immediately afterwards.

See below.

Quoted 43 lines
Quoted 43 lines

xenfb_timer runs, sets dirty to 1, kicks the thread
xenfb_thread wakes up xenfb_thread sets dirty to 0

xenfb_thread down mm_lock xenfb_thread reads the update region xenfb_thread up mm_lock

xenfb_thread sends an update using
the old region

Possibly concurrent:

another update happens:
down mm_lock
extends the update region, reschedules xenfb_timer up mm_lock

xenfb_timer runs again, sets dirty to 1, kicks the thread again

Harmless race: dirty can conceivably become 1 when there's no work to do. Then xenfb_thread wakes up, finds nothing to do and goes back to sleep.

Looks safe to me.

Quoted 10 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 3 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +3 -0
+
+static void xenfb_refresh(struct xenfb_info *info,
+			  int x1, int y1, int w, int h)

Is there any reason why there can't be multiple calls to this function live at the same time? In particular, if vm_nopage and xenfb_copyarea happen at the same time it looks like we can lose part of the update area.

Also, vm_nopage holds the mm_lock when it calls this, whereas the other callers appear not to. Was this deliberate?

The question is what mm_lock protects. If it protects info->mappings, then xenfb_refresh() doesn't care about it, as it doesn't access it.

If it should protect x1, x2, y1, y2 as well, then locking looks flawed.

Quoted 6 lines
Quoted 4 lines

x1, x2, y1, y2 need protection if they can be updated concurrently. Users are xenfb_update_screen() and xenfb_refresh(). The latter runs from fb_ops methods and vm_operations_struct method nopage. Looks like they need protection, doesn't it? Anthony?

Once you've figured this out, could you put a comment somewhere sensible, please?

In Anthony's code, mm_lock protects info->mapping.

However, the dirty rectangle x1, y1, x2, y2 needs protection as well, because it's written from multiple threads. It could use a separate lock, but that doesn't seem to be worth the trouble, so I simply use mm_lock.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 13 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +13 -0
+{
+	int y2, x2;
+
+	y2 = y1 + h;
+	x2 = x1 + w;
+	if (info->y2 == 0) {
+		info->y1 = y1;
+		info->y2 = y2;
+	}
+	if (info->x2 == 0) {
+		info->x1 = x1;
+		info->x2 = x2;
+	}

Presumably, these are just looking to see if there's a pre-existing region, and if not copying the new region across verbatim? Could you test dirty instead in that case?

Quoted 3 lines
Quoted 2 lines

What about initializing x1, y1 with an impossibly small value, and x2, y2 with an impossibly large value?

If you do it the other way around, that would be ideal.

Done.

Quoted 33 lines
Quoted 18 lines
Quoted 10 lines
Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+
+	if (info->y1 > y1)
+		info->y1 = y1;
+	if (info->y2 < y2)
+		info->y2 = y2;
+	if (info->x1 > x1)
+		info->x1 = x1;
+	if (info->x2 < x2)
+		info->x2 = x2;
+
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +4 -0
+	if (timer_pending(&info->refresh))
+		return;
+
+	mod_timer(&info->refresh, jiffies + HZ/xenfb_fps);

Would it be sensible to have a fallback so that an update is always sent to the backend e.g. 100ms after any change to the framebuffer, so that the user still sees something when the display is being updated very frequently?

Quoted 2 lines

Could this be done in a later revision? Happy to add the idea in a comment now.

xenfb_fps is only 20. 20fps is hardly an excessively fast animation, so I'd expect the display to lock up under some reasonably common and plausible situations with the current design. That isn't really good enough.

It's not a terribly hard thing to fix:

if (jiffies - info->last_update_sent > HZ/10)
xenfb_update_screen(info)
else

	mod_timer(&info->refresh, jiffies + HZ/xenfb_fps);
Quoted 7 lines

and then add an

info->last_update_sent = jiffies;

to xenfb_update_screen. You might need to move the locking around a little in update_screen, and maybe move the dirty test in there from xenfb_thread, but that's about it.

I reread Anthony's code, and I believe it's is just fine.

When the first dirt arrives, the timer is not pending, so it gets modified to fire in 1/xenfb_fps seconds.

If more dirt arrives before that time, the timer is pending, and therefore does not get modified.

When the timer fires, xenfb_timer() runs and wakes the refresh thread. This happens about 1/xenfb_fps seconds after the first dirt arrived.

When the next dirt arrives, the timer is again not pending, and my argument loops.

If dirt arrives continuously, the timer fires approximately xenfb_fps times per second.

Quoted 32 lines
Quoted 11 lines
Quoted 10 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 9 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +9 -0
+	map_pages = (vma->vm_end - vma->vm_start + PAGE_SIZE-1) >> PAGE_SHIFT;
+	if (map_pages > info->nr_pages)
+		goto out;
+
+	ret = -ENOMEM;
+	map = kmalloc(sizeof(*map), GFP_KERNEL);
+	if (map == NULL)
+		goto out;
+	memset(map, 0, sizeof(*map));

kzalloc, maybe?

Sure.

Thanks.

Quoted 9 lines
Quoted 7 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 6 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +6 -0
+	return IRQ_HANDLED;
+}
+
+static unsigned long vmalloc_to_mfn(void *address)
+{
+	// FIXME was return pfn_to_mfn(vmalloc_to_pfn(address));

Not quite sure why this comment is here.

Accident. However, arbitrary_virt_to_machine() doesn't appear to exist on ia64, so I went back to the old code.

Fair enough.

Quoted 8 lines
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 6 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +6 -0
+	return arbitrary_virt_to_machine(address) >> PAGE_SHIFT;
+}
+
+static struct xenfb_info *xenfb_info;
+
+static int __init xenfb_probe(void)

I'd find it easier to be confident of the error handling here if the function were split up a little.

Quoted 4 lines
Quoted 2 lines

It's just the way I found it. My own idea of cleanup would be to make xenfb_cleanup() work for the error case.

That would also be a good thing. It would be even better if you could do both. :)

All cleanup is now in xenfb_cleanup().

On splitting xenfb_probe(): one step at a time. If we aim for perfection in the initial revision, we'll never get it done.

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 11 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +11 -0
+
+	info = kmalloc(sizeof(*info), GFP_KERNEL);
+	if (info == NULL)
+		return -ENOMEM;
+	memset(info, 0, sizeof(*info));
+
+	INIT_LIST_HEAD(&info->mappings);
+
+	info->fb = vmalloc(xenfb_mem_len);
+	if (info->fb == NULL)
+		goto error;

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 10 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +10 -0
+	memset(info->fb, 0, xenfb_mem_len);
+	info->nr_pages = (xenfb_mem_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
+	info->pages = kmalloc(sizeof(struct page*)*info->nr_pages, GFP_KERNEL);
+	if (info->pages == NULL)
+		goto error_vfree;
+	for (i = 0; i < info->nr_pages; i++)
+		info->pages[i] = vmalloc_to_page(info->fb + i * PAGE_SIZE);
+
+	fb_info = framebuffer_alloc(sizeof(u32) * 256, NULL);
+	// FIXME sizeof(struct xenfb_info)

Where did sizeof(u32) * 256 come from? What does the comment mean?

Quoted 5 lines
Quoted 3 lines

I *suspect* that the correct argument is sizeof(struct xenfb_info), but can't yet exclude subtle, evil hackery going on here, so I put in a comment for now. Anthony, do you remember hackery here?

I think it's actually the size of the pseudo palette, but I wouldn't swear to it.

I think you're right. See `evil hackery' below.

Quoted 52 lines
Quoted 2 lines
Quoted 1 line

fb_info isn't released from the error path.

Fixing...

Thanks.

Quoted 7 lines
Quoted 6 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +4 -0
+	if (fb_info == NULL)
+		goto error_kfree;
+
+	info->mfns = vmalloc(sizeof(unsigned long) * info->nr_pages);

What happens if this fails? Also, you never seem to vfree this if we hit an error later on.

Fixing...

Thanks.

Quoted 19 lines
Quoted 11 lines
Quoted 11 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +11 -0
+	/* set up shared page */
+	info->page = (void *)__get_free_page(GFP_KERNEL);
+	if (!info->page)
+		goto error_kfree;
+	/* set up event channel */
+	alloc_unbound.dom = DOMID_SELF;
+	alloc_unbound.remote_dom = 0;
+	ret = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
+					  &alloc_unbound);
+	if (ret)
+		goto error_freep;
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 5 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+	info->evtchn = alloc_unbound.port;
+
+	for (i = 0; i < info->nr_pages; i++)
+		info->mfns[i] = vmalloc_to_mfn(info->fb + i * PAGE_SIZE);
+	info->page->pd[0] = vmalloc_to_mfn(info->mfns);

What happens if this domain is running in shadow translate mode? It looks to me like the backend will end up mapping completely the wrong frame.

Quoted 3 lines

Discussion above seems to suggest that our only option is to have the backend detect and translate shadow translate mode frontends, so no action is required here. Correct?

Agreed.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +15 -0
+	info->page->pd[1] = 0;
+	info->page->width = XENFB_WIDTH;
+	info->page->height = XENFB_HEIGHT;
+	info->page->depth = XENFB_DEPTH;
+	info->page->line_length = (info->page->depth / 8) * info->page->width;
+	info->page->mem_length = xenfb_mem_len;
+	info->page->in_cons = info->page->in_prod = 0;
+	info->page->out_cons = info->page->out_prod = 0;
+
+	ret = bind_evtchn_to_irqhandler(info->evtchn, xenfb_event_handler,
+					0, "xenfb", info);
+	if (ret < 0) {
+		close.port = info->evtchn;
+		HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
+		goto error_freep;
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+	}
+
+	info->irq = ret;
+	xenfb_info = info;
+
+	fb_info->pseudo_palette = fb_info->par;
+	fb_info->par = info;

Eh? What was the reasoning behind this?

I have no idea. Ugly, evil hackery. The only excuse I can see is that it works. Hope to slay it in a future revision.

Quoted 7 lines
Quoted 6 lines
Quoted 5 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +4 -0
+	fb_info->screen_base = info->fb;
+
+	memset(&fb_info->var, 0, sizeof(fb_info->var));
+	memset(&fb_info->fix, 0, sizeof(fb_info->fix));

I think these are already guaranteed to be zero, aren't they?

Yes, framebuffer_alloc() clears *fb_info. Happy to rely on that?

I can't see any reason not to.

Done.

Quoted 65 lines
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +4 -0
+	fb_info->fix.visual = FB_VISUAL_TRUECOLOR;
+	fb_info->fix.line_length = info->page->line_length;
+	fb_info->fix.smem_start = 0;
+	fb_info->fix.smem_len = xenfb_mem_len;
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+	strcpy(fb_info->fix.id, "xen");
+	fb_info->fix.type = FB_TYPE_PACKED_PIXELS;
+	fb_info->fix.accel = FB_ACCEL_NONE;
+
+	fb_info->flags = FBINFO_FLAG_DEFAULT;
+
+	fb_alloc_cmap(&fb_info->cmap, 256, 0);

This can fail, and the error paths don't bother to deallocate it.

Quoted 1 line

Fixing...

Thanks.

Quoted 10 lines
Quoted 10 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 8 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +8 -0
+
+ again:
+	ret = xenbus_transaction_start(&xbt);
+	if (ret)
+		goto error_unreg;
+	ret = xenbus_printf(xbt, "vfb", "page-ref", "%lu",
+			    virt_to_mfn(info->page));
+	// FIXME grant tables?

Again, what happens in shadow translate mode?

Quoted 3 lines
Quoted 2 lines

Also, it's kind of annoying that there's no support for multiple framebuffers in a domU. That's very much a wishlist thing, though.

See above.

Ack.

Quoted 6 lines
Quoted 5 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +4 -0
+	unregister_framebuffer(fb_info);
+ error_unbind:
+	unbind_from_irqhandler(info->irq, info);
+	// FIXME do we have to stop info->kthread?

If you've started it you certainly need to stop it.

Fixing...

Thanks.

Quoted 10 lines
Quoted 10 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 6 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +6 -0
+	return -ENODEV;
+}
+
+static int __init xenfb_init(void)
+{
+	return xenfb_probe();

From last time:

Quoted 1 line
Quoted 1 line

You might want to consider using xenbus_device and friends here.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I considered that, but it wasn't obvious to me how to make it fit cleanly to a user-space backend. Care to advise?

I think if the frontend needs to know whether the backend is in kernel or userspace then we've messed up somewhere. Why do you expect this to be difficult?

I didn't *expect* it to be difficult, I just couldn't see how to fit Anthony's code to xenbus_device without rewriting more than I like, and I still can't see it. That's why I asked for advice.

The probing stuff seems to be fairly self-contained at the moment. You'd probably have to rewrite xenfb_probe, but I can't see any need to change much beyond that.

Unless I'm just missing something?

Quoted 3 lines

Anyway, I'm reluctant to throw out working code while there's so much code that needs fixing. I may be less reluctant once we've drained the swamp some more.

Quoted 6 lines

So I think long term we'd probably like to use the standard xenbus device setup protocol, going through XenbusStateInitialised, XenbusStateConnected, XenbusStateClosing, etc. That'd be a non-backwards-compatible change if we do it later, so it'd be good to get it in now. xenbus_device gives you that for free, although I'll admit it is a bit of a pain to use.

We'll have to break backwards compatibility anyway when we switch to grant tables. Why dig up the same code twice, and in the meantime deprive users of the code that works?

Quoted 127 lines
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+}
+
+static void __exit xenfb_cleanup(void)
+{
+	struct xenfb_info *info = xenfb_info;
+
+	unregister_framebuffer(info->fb_info);
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 6 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +6 -0
+	unbind_from_irqhandler(info->irq, info);
+	free_page((unsigned long)info->page);
+	kfree(info->pages);
+	vfree(info->fb);
+	kfree(info);
+	xenfb_info = NULL;

I don't think this releases all of the memory you allocate in xenfb_probe.

Quoted 1 line

Fixing...

Thanks.

Quoted 10 lines
Quoted 9 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 8 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +8 -0
+	  return -ENODEV;
+#if 0
+        /* if we're not set up to use graphics mode, then don't initialize */
+	if (xenbus_scanf(XBT_NIL, "console", "use_graphics", "%d", &ret) < 0)
+	  return -ENODEV;
+	if (ret == 0)
+	  return -ENODEV;
+#endif

Not sure about this.

Same as for xenfb.c. The #if is an accident.

Ack.

Quoted 10 lines
Quoted 9 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 5 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +5 -0
+
+	dev = kmalloc(sizeof(*dev), GFP_KERNEL);
+	input_dev = input_allocate_device();
+	if (!dev || !input_dev)
+		return -ENOMEM;

You potentially leak memory if one of these fails and the other succeeds.

Also, the error paths don't release input_dev. In fact, nothing ever releases input_dev, unless I'm missing something.

Fixing...

Thanks.

Quoted 20 lines
Quoted 12 lines
Quoted 12 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +12 -0
+
+	dev->dev = input_dev;
+	dev->info = (void *)__get_free_page(GFP_KERNEL);
+	if (!dev->info) {
+		ret = -ENOMEM;
+		goto error;
+	}
+
+	alloc_unbound.dom = DOMID_SELF;
+	alloc_unbound.remote_dom = 0;
+	ret = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
+					  &alloc_unbound);
Quoted 8 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+	if (ret)
+		goto error_freep;
+	dev->evtchn = alloc_unbound.port;
+	ret = bind_evtchn_to_irqhandler(dev->evtchn, input_handler, 0,
+					"xenkbd", dev);
+	if (ret < 0)
+		goto error_freep;

You might want to close the event channel here.

Quoted 1 line

Fixing...

Thanks.

Quoted 6 lines
Quoted 5 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 4 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +4 -0
+
+	input_dev->name = "Xen Virtual Keyboard/Mouse";
+
+	input_register_device(input_dev);

This can fail.

Most uses elsewhere in the kernel don't care. Fixing anyway.

Yeah, I know, but it's good to get it right anyway. Thanks.

Quoted 9 lines
Quoted 9 lines

linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c

Quoted 7 lines
linux-2.6-xen-sparse/drivers/xen/xenfb/xenfb.c new file +7 -0
+
+ again:
+	ret = xenbus_transaction_start(&xbt);
+	if (ret)
+		goto error_unreg;
+	ret = xenbus_printf(xbt, "vkbd", "page-ref", "%lu",
+			    virt_to_mfn(dev->info));

There's no reason that I can see not to use grant tables here, and that'd avoid all of the potential issues with shadow translate mode.

Quoted 4 lines

Since we have to fool around with shadow translate mode anyway for sharing the framebuffer, I don't quite see why we need to address this right now. This code works, and there's no shortage of code that needs fixing.

Alright, leave this for now and fix it when you fix the vfb equivalent bits.

Quoted 1 line

Same for vfb/page-ref.

Well, that's a reference to a bunch of untranslated frame numbers, so it makes more sense for it to be untranslated itself. The keyboard page doesn't require any further translation.

Quoted 7 lines
Quoted 6 lines

linux-2.6-xen-sparse/include/linux/xenfb.h

Quoted 4 lines

+MODULE_LICENSE("GPL");
diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/include/linux/xenfb.h

linux-2.6-xen-sparse/include/linux/xenfb.h new file +0 -0

This is defining an io protocol, so belongs in xen/include/public/io

Okay.

Thanks.

Quoted 24 lines
Quoted 24 lines

linux-2.6-xen-sparse/include/linux/xenfb.h

Quoted 9 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +9 -0
+#include <asm/types.h>
+
+/* out events */
+
+#define XENFB_OUT_EVENT_SIZE 40
+
+#define XENFB_TYPE_MOTION 1
+#define XENFB_TYPE_UPDATE 2
+

The other tagged unions in Xen look more like this:

struct {
	u8 type;
	union {
		struct {
			u32 field1;
			u32 field2;
		} type1;
		struct {
			u16 field1;
		} type2;
	} u;
};
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

I'd find this a lot easier to read than the current approach of putting the type into the individual event type structures. Is there any reason why this couldn't be used here?

Matter of taste. Will change it if you insist.

It'd be good to be consistent with the other protocols.

How to pad such a struct to a fixed size?

Quoted 87 lines
Quoted 9 lines
Quoted 7 lines

linux-2.6-xen-sparse/include/linux/xenfb.h

Quoted 6 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +6 -0
+struct xenfb_motion	    /* currently unused */
+{
+	__u8 type;          /* XENFB_TYPE_MOTION */
+	__u16 x;            /* The new x coordinate */
+	__u16 y;            /* The new y coordinate */
+};

I think if the comment said what was moving this'd be a lot clearer.

I'm not into documenting unused code I didn't write and don't understand, so I'm going to remove this.

Even better.

Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +9 -0
+
+struct xenfb_update
+{
+	__u8 type;          /* XENFB_TYPE_UPDATE */
+	__u16 x;            /* source x */
+	__u16 y;            /* source y */
+	__u16 width;        /* rect width */
+	__u16 height;       /* rect height */
+};
Quoted 8 lines

linux-2.6-xen-sparse/include/linux/xenfb.h

Quoted 7 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +7 -0
+
+union xenfb_out_event
+{
+	__u8 type;
+	struct xenfb_motion motion;
+	struct xenfb_update update;
+	char _[XENFB_OUT_EVENT_SIZE];

Perhaps ``pad'' would be clearer than ``_''?

Quoted 1 line

Works for me.

Thanks.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +4 -0
+struct xenfb_page
+{
+	__u16 width;         /* the width of the framebuffer (in pixels) */
+	__u16 height;        /* the height of the framebuffer (in pixels) */
Quoted 8 lines

linux-2.6-xen-sparse/include/linux/xenfb.h

Quoted 6 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +6 -0
+	__u32 line_length;   /* the length of a row of pixels (in bytes) */
+	__u32 mem_length;    /* the length of the framebuffer (in bytes) */
+	__u8 depth;          /* the depth of a pixel (in bits) */
+
+	unsigned long pd[2];	/* FIXME rename to pgdir? */
+	/* FIXME pd[1] unused at this time, shrink? */

I think it'd be enough to just give some indication of where the magic number 2 came from.

Quoted 1 line

Okay.

Thanks.

Quoted 8 lines
Quoted 7 lines

linux-2.6-xen-sparse/include/linux/xenfb.h

Quoted 6 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +6 -0
+
+	__u32 in_cons, in_prod;
+	__u32 out_cons, out_prod;
+};
+
+void xenfb_resume(void);

No longer exists.

Removed.

Thanks.

Quoted 7 lines
Quoted 6 lines

linux-2.6-xen-sparse/include/linux/xenkbd.h

Quoted 5 lines
linux-2.6-xen-sparse/include/linux/xenfb.h new file +2 -0
+
+#endif

diff -r 5fa9b746d24f -r 510c6bceb87f linux-2.6-xen-sparse/include/linux/xenkbd.h

linux-2.6-xen-sparse/include/linux/xenkbd.h new file +0 -0

Again, this belongs in xen/include/public/io.

Okay.

Thanks.

Quoted 11 lines
Quoted 11 lines

linux-2.6-xen-sparse/include/linux/xenkbd.h

Quoted 10 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +10 -0
+union xenkbd_in_event
+{
+	__u8 type;
+	struct xenkbd_motion motion;
+	struct xenkbd_button button;
+	struct xenkbd_key key;
+	char _[XENKBD_IN_EVENT_SIZE];
+};
+
+/* out events */

Maybe a comment that none are currently defined?

Quoted 1 line

Sure.

Thanks.

Quoted 8 lines
Quoted 8 lines

linux-2.6-xen-sparse/include/linux/xenkbd.h

Quoted 7 lines
linux-2.6-xen-sparse/include/linux/xenkbd.h new file +7 -0
+struct xenkbd_info
+{
+	__u32 in_cons, in_prod;
+	__u32 out_cons, out_prod;
+};
+
+void xenkbd_resume(void);

No longer exists.

Quoted 4 lines
Quoted 1 line

Removed.

Thanks.

Steven.

Next try. The patch contains Amos Waterland's patch for xenconsole to use /dev/xvc0 instead of taking over ttys, with Jeremy's fixes and cleanups. If you need those parts separate, let me know.

arch/i386/kernel/setup-xen.c +3 -2
diff -r 47c098fdce14 arch/i386/kernel/setup-xen.c
@@ -1788,8 +1788,9 @@ void __init setup_arch(char **cmdline_p)
 #endif
 #endif
 	} else {
-		extern int console_use_vt;
-		console_use_vt = 0;
+#if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
+		conswitchp = &dummy_con;
+#endif
 	}
 #ifdef CONFIG_X86_TSC
 	tsc_init();
arch/ia64/kernel/setup.c +2 -2
diff -r 47c098fdce14 arch/ia64/kernel/setup.c
@@ -583,9 +583,9 @@ setup_arch (char **cmdline_p)
 		       xen_start_info->nr_pages, xen_start_info->flags);
 
 		if (!is_initial_xendomain()) {
-			extern int console_use_vt;
+#if !defined(CONFIG_VT) || !defined(CONFIG_DUMMY_CONSOLE)
 			conswitchp = NULL;
-			console_use_vt = 0;
+#endif
 		}
 	}
 #endif
arch/x86_64/kernel/setup-xen.c +4 -3
diff -r 47c098fdce14 arch/x86_64/kernel/setup-xen.c
@@ -931,9 +931,10 @@ void __init setup_arch(char **cmdline_p)
 #endif
 #endif
 		} else {
-			extern int console_use_vt;
-			console_use_vt = 0;
-		}
+#if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
+                    conswitchp = &dummy_con;
+#endif
+                }
 	}
 #else	/* CONFIG_XEN */
 
drivers/char/tty_io.c +1 -5
diff -r 47c098fdce14 drivers/char/tty_io.c
@@ -130,8 +130,6 @@ LIST_HEAD(tty_drivers);			/* linked list
    vt.c for deeply disgusting hack reasons */
 DEFINE_MUTEX(tty_mutex);
 
-int console_use_vt = 1;
-
 #ifdef CONFIG_UNIX98_PTYS
 extern struct tty_driver *ptm_driver;	/* Unix98 pty masters; for /dev/ptmx */
 extern int pty_limit;		/* Config limit on Unix98 ptys */
@@ -2485,7 +2483,7 @@ retry_open:
 		goto got_driver;
 	}
 #ifdef CONFIG_VT
-	if (console_use_vt && (device == MKDEV(TTY_MAJOR,0))) {
+	if (device == MKDEV(TTY_MAJOR,0)) {
 		extern struct tty_driver *console_driver;
 		driver = console_driver;
 		index = fg_console;
@@ -3911,8 +3909,6 @@ static int __init tty_init(void)
 #endif
 
 #ifdef CONFIG_VT
-	if (!console_use_vt)
-		goto out_vt;
 	cdev_init(&vc0_cdev, &console_fops);
 	if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
 	    register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
drivers/xen/Kconfig +23 -0
diff -r 47c098fdce14 drivers/xen/Kconfig
@@ -172,6 +172,29 @@ config XEN_NETDEV_FRONTEND
 	  dedicated device-driver domain, or your master control domain
 	  (domain 0), then you almost certainly want to say Y here.
 
+config XEN_FRAMEBUFFER
+	tristate "Framebuffer-device frontend driver"
+	depends on XEN && FB
+	select FB_CFB_FILLRECT
+	select FB_CFB_COPYAREA
+	select FB_CFB_IMAGEBLIT
+	default y
+	help
+	  The framebuffer-device frontend drivers allows the kernel to create a
+	  virtual framebuffer.  This framebuffer can be viewed in another
+	  domain.  Unless this domain has access to a real video card, you
+	  probably want to say Y here.
+
+config XEN_KEYBOARD
+	tristate "Keyboard-device frontend driver"
+	depends on XEN
+	default y
+	help
+	  The keyboard-device frontend driver allows the kernel to create a
+	  virtual keyboard.  This keyboard can then be driven by another
+	  domain.  If you've said Y to CONFIG_XEN_FRAMEBUFFER, you probably
+	  want to say Y here.
+
 config XEN_SCRUB_PAGES
 	bool "Scrub memory before freeing it to Xen"
 	default y
drivers/xen/Makefile +2 -0
diff -r 47c098fdce14 drivers/xen/Makefile
@@ -15,3 +15,5 @@ obj-$(CONFIG_XEN_NETDEV_FRONTEND)	+= net
 obj-$(CONFIG_XEN_NETDEV_FRONTEND)	+= netfront/
 obj-$(CONFIG_XEN_PCIDEV_BACKEND)	+= pciback/
 obj-$(CONFIG_XEN_PCIDEV_FRONTEND)	+= pcifront/
+obj-$(CONFIG_XEN_FRAMEBUFFER)		+= xenfb/
+obj-$(CONFIG_XEN_KEYBOARD)		+= xenkbd/
drivers/xen/console/console.c +49 -4

117 lines not on this page · raw message

mm/memory.c +1 -0
diff -r 47c098fdce14 mm/memory.c
@@ -891,6 +891,7 @@ unsigned long zap_page_range(struct vm_a
 		tlb_finish_mmu(tlb, address, end);
 	return end;
 }
+EXPORT_SYMBOL(zap_page_range);
 
 /*
  * Do a quick page-table lookup for a single page.
drivers/xen/xenfb/Makefile new file +1 -0
diff -r 47c098fdce14 drivers/xen/xenfb/Makefile
@@ -0,0 +1,1 @@
+obj-$(CONFIG_XEN_FRAMEBUFFER)	:= xenfb.o
drivers/xen/xenfb/xenfb.c new file +580 -0

580 lines not on this page · raw message

drivers/xen/xenkbd/Makefile new file +1 -0
diff -r 47c098fdce14 drivers/xen/xenkbd/Makefile
@@ -0,0 +1,1 @@
+obj-$(CONFIG_XEN_KEYBOARD)	+= xenkbd.o
drivers/xen/xenkbd/xenkbd.c new file +198 -0

198 lines not on this page · raw message

include/xen/interface/io/xenfb.h new file +103 -0

103 lines not on this page · raw message

include/xen/interface/io/xenkbd.h new file +92 -0

92 lines not on this page · raw message

#12 lore raw

I'm now looking into a conversion to xenbus_driver API. I started with copying some voodoo from blkfront.c. My xenbus_register_frontend() succeeds. Stupid question: what triggers running of the probe function? Because mine doesn't.

#13 lore raw
Quoted 4 lines · Markus Armbruster

I'm now looking into a conversion to xenbus_driver API. I started with copying some voodoo from blkfront.c. My xenbus_register_frontend() succeeds. Stupid question: what triggers running of the probe function? Because mine doesn't.

Either the watch firing when the correct entries in the store are written (for hotplugging) or it probes those paths at startup.

xenbus/xenbus_probe.c:

frontend_changed ->
dev_changed ->
xenbus_probe_node ->
device_register

or

xenbus_probe_init ->
xenbus_probe ->
xenbus_probe_devices ->
xenbus_probe_device_type ->
bus->probe ->
xenbus_probe_frontend ->
xenbus_probe_node ->
device_register

The device_register causes a the specific frontend device to be registered, which triggers the probe.

HTH,

Ewan.

#14 lore raw
Quoted 28 lines · Ewan Mellor
Quoted 4 lines · Markus Armbruster

I'm now looking into a conversion to xenbus_driver API. I started with copying some voodoo from blkfront.c. My xenbus_register_frontend() succeeds. Stupid question: what triggers running of the probe function? Because mine doesn't.

Either the watch firing when the correct entries in the store are written (for hotplugging) or it probes those paths at startup.

xenbus/xenbus_probe.c:

frontend_changed ->
dev_changed ->
xenbus_probe_node ->
device_register

or

xenbus_probe_init ->
xenbus_probe ->
xenbus_probe_devices ->
xenbus_probe_device_type ->
bus->probe ->
xenbus_probe_frontend ->
xenbus_probe_node ->
device_register

Quoted 6 lines

The device_register causes a the specific frontend device to be registered, which triggers the probe.

HTH,

Ewan.

Okay, thanks. Next stupid question: I'm looking for a *user space* xenbus_register_backend(). Where is it?

#15 lore raw
Quoted 2 lines

Okay, thanks. Next stupid question: I'm looking for a *user space* xenbus_register_backend(). Where is it?

I don't think there is such a thing at the moment, just because we don't currently have any userspace backends.

I think in your particularly case, the easiest thing is probably going to be to get xend to set up a skeleton xenstore area for the frontend in the usual way (see DevController.py and e.g. netif.py). With a normal in-kernel driver you'd be able to watch the relevant location to notice when a new device is created, but you don't have a permanently-running daemon, which makes that a bit tricky. I think it'd be okay to just fake the relevant entry in the store (set ${backend_path}/hotplug-status to connected) from the backend. It's a little skanky, but it should be adequate and reasonably straightforward to implement.

Once you've got that, it should be easy to just do the usual state machine directly in the backend:

-- Both front and back are put into Initialising by xend.
-- When the backend is ready, it goes to state InitWait and triggers
the hotplug event.
-- When the frontend is ready, it sets up its area of the store, and goes to
Initialised.
-- The backend notices this, connects to the ring, and goes to state
Connected.
-- The frontend notices this, goes to Connected itself, and makes
itself available to the rest of the operating system.

Teardown is usually started by the frontend, and goes like this:

-- Frontend goes to state Closing
-- Backend disconnects from the ring and goes to state Closing -- Frontend disconnects from the ring and goes to state Closed -- Backend goes to Closed and destroys itself, unless online=1 in the
store, in which case it prepares for further connections.

Xend can also trigger backend teardown by simply deleting the frontend area, e.g. if the frontend domain crashed. The normal in-kernel infrastructure calls otherend_changed with the state XenbusStateUnknown in that case, which causes the backends to destroy themselves.

Steven.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#16 lore raw
Quoted 5 lines · Steven Smith
Quoted 2 lines

Okay, thanks. Next stupid question: I'm looking for a *user space* xenbus_register_backend(). Where is it?

I don't think there is such a thing at the moment, just because we don't currently have any userspace backends.

Quoted 10 lines

I think in your particularly case, the easiest thing is probably going to be to get xend to set up a skeleton xenstore area for the frontend in the usual way (see DevController.py and e.g. netif.py). With a normal in-kernel driver you'd be able to watch the relevant location to notice when a new device is created, but you don't have a permanently-running daemon, which makes that a bit tricky. I think it'd be okay to just fake the relevant entry in the store (set ${backend_path}/hotplug-status to connected) from the backend. It's a little skanky, but it should be adequate and reasonably straightforward to implement.

This is what we do for blktap -- tools/examples/blktap just calls success immediately, which writes "connected" into the store as you suggest.

Ewan.

#17 lore raw

I tried to follow your advice, but Xend hates me. I must be doing something stupid, can anybody help?

I created a trivial xend/server/vfbif.py:

from xen.xend.server.DevController import DevController

class VfbifController(DevController):
"""Virtual frame buffer controller. Handles all vfb devices for a domain. """

def __init__(self, vm):
DevController.__init__(self, vm)

def getDeviceDetails(self, config):
"""@see DevController.getDeviceDetails"""
devid = 0
back = {}
front = {}
return (devid, back, front)

I added it to xend/XendDomainInfo.py, as follows:

XendDomainInfo.py 2006-11-03 14:33:28.000000000 +0100 renamed from XendDomainInfo.py.orig 2006-10-10 16:23:45.000000000 +0200 +2 -1
@@ -1786,7 +1786,7 @@
     controllerClasses[device_class] = cls
 
 
-from xen.xend.server import blkif, netif, tpmif, pciif, iopif, irqif, usbif
+from xen.xend.server import blkif, netif, tpmif, pciif, iopif, irqif, usbif, vfbif
 from xen.xend.server.BlktapController import BlktapController
 addControllerClass('vbd',  blkif.BlkifController)
 addControllerClass('vif',  netif.NetifController)
@@ -1796,3 +1796,4 @@
 addControllerClass('irq',  irqif.IRQController)
 addControllerClass('usb',  usbif.UsbifController)
 addControllerClass('tap',  BlktapController)
+addControllerClass('vfb',  vfbif.VfbifController)

I set hotplug-status in the backend, resulting in this:

    # xenstore-ls /local/domain/0/backend
    vfb = ""
     1 = ""
      0 = ""
       hotplug-status = "connected"

Then I attempt to create domain 1:

    # xm create -c parafat
[pygrub screen...]
    Using config file "/etc/xen/parafat".
    Going to boot Fedora Core (2.6.18.1)
      kernel: /vmlinuz-2.6.18.1
      initrd: /initrd-2.6.18.1.img
    Error: Device 0 not connected

This is actually from configuration when it tries to read /local/domain/1/device/vfb/0/backend-id. /local/domain/1/device contains entries vbd and vif, but not vfb. Shouldn't xend have written stuff there?

I'm confused.

#18 lore raw
Quoted 1 line

I created a trivial xend/server/vfbif.py:

Looks plausible. You probably want to have a createDevice method which calls DevController.createDevice and then spawns the backend, but that can come later.

Quoted 1 line

I added it to xend/XendDomainInfo.py, as follows:

I think this bit is correct.

I think this should work if your SXP includes an entry for the backend. Adding something like ``(device (vfb ()))'' to the end of the (vm) section should work.

``xm create -n'' will convert an ordinary xm configuration file to SXP format, and ``xm create -C'' can create a domain directly from the SXP. Eventually, you'll want to add something to the xm parser which makes it possible to do this without directly editting the SXP, but that can wait for a bit.

Steven.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#19 lore raw
In reply to Ewan Mellor
Quoted 28 lines · Ewan Mellor
Quoted 4 lines · Markus Armbruster

I'm now looking into a conversion to xenbus_driver API. I started with copying some voodoo from blkfront.c. My xenbus_register_frontend() succeeds. Stupid question: what triggers running of the probe function? Because mine doesn't.

Either the watch firing when the correct entries in the store are written (for hotplugging) or it probes those paths at startup.

xenbus/xenbus_probe.c:

frontend_changed ->
dev_changed ->
xenbus_probe_node ->
device_register

or

xenbus_probe_init ->
xenbus_probe ->
xenbus_probe_devices ->
xenbus_probe_device_type ->
bus->probe ->
xenbus_probe_frontend ->
xenbus_probe_node ->
device_register

Quoted 2 lines

The device_register causes a the specific frontend device to be registered, which triggers the probe.

Err, I still don't get it. What exactly makes the guest run the frontend's probe? I figure you're trying to tell me that it runs when a certain xenstore entry is present at startup, or when it gets created later. Corrrect? Which entry exactly? I tried a few (more voodoo), but no luck.

#20 lore raw
Quoted 30 lines · Markus Armbruster
Quoted 28 lines · Ewan Mellor
Quoted 4 lines · Markus Armbruster

I'm now looking into a conversion to xenbus_driver API. I started with copying some voodoo from blkfront.c. My xenbus_register_frontend() succeeds. Stupid question: what triggers running of the probe function? Because mine doesn't.

Either the watch firing when the correct entries in the store are written (for hotplugging) or it probes those paths at startup.

xenbus/xenbus_probe.c:

frontend_changed ->
dev_changed ->
xenbus_probe_node ->
device_register

or

xenbus_probe_init ->
xenbus_probe ->
xenbus_probe_devices ->
xenbus_probe_device_type ->
bus->probe ->
xenbus_probe_frontend ->
xenbus_probe_node ->
device_register

Quoted 8 lines
Quoted 2 lines

The device_register causes a the specific frontend device to be registered, which triggers the probe.

Err, I still don't get it. What exactly makes the guest run the frontend's probe? I figure you're trying to tell me that it runs when a certain xenstore entry is present at startup, or when it gets created later. Corrrect? Which entry exactly? I tried a few (more voodoo), but no luck.

/local/domain/<domid>/device/<devclass>/<devid>

<devclass> is the string that you've put in xenbus_driver.name when registering it with xenbus_register_frontend. The xenbus driver registers a watch on /local/domain/<domid>/device, and whenever that fires, dev_changed looks through the registered drivers, looking for the one with the right <devclass>, and it all gets kicked off from there.

Ewan.

#21 lore raw
In reply to Steven Smith

Steven Smith <[email protected]> writes:

[...]

Quoted 4 lines
Quoted 4 lines

As far as I can see, this is our only option right now. I'm ignorant of `shadow translate mode'; can somebody please educate me on how to detect and translate shadow translate mode frontend mfns in the backend?

Quoted 8 lines

Shadow translate mode is a mode of the shadow page tables in which Xen does machine<->physical address translations on behalf of the guest. It's mostly used for HVM guests to give them the impression of a mostly-contiguous block of memory starting at machine address 0.

If you just bung the guest-supplied machine frame numbers through xc_translate_gpfn_list you should be fine. That does the translation if you're in shadow translate mode and does nothing if you're not.

Assuming you mean xc_domain_translate_gpfn_list(). I tried translating fbdev_mfn right before use, as follows:

	ret = xc_domain_translate_gpfn_list(xenfb->xc, domid, 1,
					    &fbdev_mfn, &fbdev_mfn);
	if (ret < 0)
		goto error;
	xenfb->fb_info = xc_map_foreign_range(xenfb->xc, domid, XC_PAGE_SIZE,
					      PROT_READ | PROT_WRITE,
					      fbdev_mfn);
	if (xenfb->fb_info == NULL)
		goto error;

However, xc_domain_translate_gpfn_list() always fails with EINVAL for me. If I read its code correctly, this can happen because op.nr_gpfns is too large, or !shadow_mode_translate(). I suspect its the latter. Should I call it only when in shadow translate mode? How to check for that? Or am I doing something wrong?

#22 lore raw
Quoted 5 lines · Markus Armbruster

However, xc_domain_translate_gpfn_list() always fails with EINVAL for me. If I read its code correctly, this can happen because op.nr_gpfns is too large, or !shadow_mode_translate(). I suspect its the latter. Should I call it only when in shadow translate mode? How to check for that? Or am I doing something wrong?

If the call returns -EINVAL you can carry on. The guest is not shadow-translate and so you already had an MFN in hand in the first place. We will soon get rid of these translate_gpfn calls anyway, as I'll change Xen to accept GMFNs when mapping foreign pages.

-- Keir