Index: uspace/drv/fb/kfb/port.c
===================================================================
--- uspace/drv/fb/kfb/port.c	(revision 5fae1237429d6d7f6e5c59d4a0b9c9e56446a466)
+++ uspace/drv/fb/kfb/port.c	(revision b43edabea6b4e7a51e81261a5aec4d8b514e4e8f)
@@ -68,6 +68,5 @@
 
 	sysarg_t paddr;
-	sysarg_t width;
-	sysarg_t height;
+	gfx_rect_t rect;
 	size_t offset;
 	size_t scanline;
@@ -154,10 +153,12 @@
 {
 	kfb_t *kfb = (kfb_t *) arg;
+	gfx_rect_t crect;
 	gfx_coord_t x, y;
 
-	// XXX We should handle p0.x > p1.x and p0.y > p1.y
-
-	for (y = rect->p0.y; y < rect->p1.y; y++) {
-		for (x = rect->p0.x; x < rect->p1.x; x++) {
+	/* Make sure we have a sorted, clipped rectangle */
+	gfx_rect_clip(rect, &kfb->rect, &crect);
+
+	for (y = crect.p0.y; y < crect.p1.y; y++) {
+		for (x = crect.p0.x; x < crect.p1.x; x++) {
 			kfb->pixel2visual(kfb->addr + FB_POS(kfb, x, y),
 			    kfb->color);
@@ -242,4 +243,6 @@
 	gfx_rect_t srect;
 	gfx_rect_t drect;
+	gfx_rect_t skfbrect;
+	gfx_rect_t crect;
 	gfx_coord2_t offs;
 	gfx_coord2_t bmdim;
@@ -251,6 +254,8 @@
 	pixel_t color;
 
+	/* Clip source rectangle to bitmap bounds */
+
 	if (srect0 != NULL)
-		srect = *srect0;
+		gfx_rect_clip(srect0, &kfbbm->rect, &srect);
 	else
 		srect = kfbbm->rect;
@@ -272,6 +277,15 @@
 	pbm.data = kfbbm->alloc.pixels;
 
-	for (pos.y = srect.p0.y; pos.y < srect.p1.y; pos.y++) {
-		for (pos.x = srect.p0.x; pos.x < srect.p1.x; pos.x++) {
+	/* Transform KFB bounding rectangle back to bitmap coordinate system */
+	gfx_rect_rtranslate(&offs, &kfb->rect, &skfbrect);
+
+	/*
+	 * Make sure we have a sorted source rectangle, clipped so that
+	 * destination lies within KFB bounding rectangle
+	 */
+	gfx_rect_clip(&srect, &skfbrect, &crect);
+
+	for (pos.y = crect.p0.y; pos.y < crect.p1.y; pos.y++) {
+		for (pos.x = crect.p0.x; pos.x < crect.p1.x; pos.x++) {
 			gfx_coord2_subtract(&pos, &kfbbm->rect.p0, &sp);
 			gfx_coord2_add(&pos, &offs, &dp);
@@ -429,6 +443,9 @@
 	kfb->fun = fun;
 
-	kfb->width = width;
-	kfb->height = height;
+	kfb->rect.p0.x = 0;
+	kfb->rect.p0.y = 0;
+	kfb->rect.p1.x = width;
+	kfb->rect.p1.y = height;
+
 	kfb->paddr = paddr;
 	kfb->offset = offset;
