958b17f
diff --git a/tools/ocaml/libs/xb/partial.ml b/tools/ocaml/libs/xb/partial.ml
958b17f
index 3558889..d4d1c7b 100644
958b17f
--- a/tools/ocaml/libs/xb/partial.ml
958b17f
+++ b/tools/ocaml/libs/xb/partial.ml
958b17f
@@ -27,8 +27,15 @@ external header_size: unit -> int = "stub_header_size"
958b17f
 external header_of_string_internal: string -> int * int * int * int
958b17f
          = "stub_header_of_string"
958b17f
 
958b17f
+let xenstore_payload_max = 4096 (* xen/include/public/io/xs_wire.h *)
958b17f
+
958b17f
 let of_string s =
958b17f
 	let tid, rid, opint, dlen = header_of_string_internal s in
958b17f
+	(* A packet which is bigger than xenstore_payload_max is illegal.
958b17f
+	   This will leave the guest connection is a bad state and will
958b17f
+	   be hard to recover from without restarting the connection
958b17f
+	   (ie rebooting the guest) *)
958b17f
+	let dlen = min xenstore_payload_max dlen in
958b17f
 	{
958b17f
 		tid = tid;
958b17f
 		rid = rid;
958b17f
@@ -38,6 +45,7 @@ let of_string s =
958b17f
 	}
958b17f
 
958b17f
 let append pkt s sz =
958b17f
+	if pkt.len > 4096 then failwith "Buffer.add: cannot grow buffer";
958b17f
 	Buffer.add_string pkt.buf (String.sub s 0 sz)
958b17f
 
958b17f
 let to_complete pkt =
958b17f
diff --git a/tools/ocaml/libs/xb/xs_ring_stubs.c b/tools/ocaml/libs/xb/xs_ring_stubs.c
958b17f
index 00414c5..4888ac5 100644
958b17f
--- a/tools/ocaml/libs/xb/xs_ring_stubs.c
958b17f
+++ b/tools/ocaml/libs/xb/xs_ring_stubs.c
958b17f
@@ -39,21 +39,23 @@ static int xs_ring_read(struct mmap_interface *interface,
958b17f
                              char *buffer, int len)
958b17f
 {
958b17f
 	struct xenstore_domain_interface *intf = interface->addr;
958b17f
-	XENSTORE_RING_IDX cons, prod;
958b17f
+	XENSTORE_RING_IDX cons, prod; /* offsets only */
958b17f
 	int to_read;
958b17f
 
958b17f
-	cons = intf->req_cons;
958b17f
-	prod = intf->req_prod;
958b17f
+	cons = *(volatile uint32*)&intf->req_cons;
958b17f
+	prod = *(volatile uint32*)&intf->req_prod;
958b17f
 	xen_mb();
958b17f
+	cons = MASK_XENSTORE_IDX(cons);
958b17f
+	prod = MASK_XENSTORE_IDX(prod);
958b17f
 	if (prod == cons)
958b17f
 		return 0;
958b17f
-	if (MASK_XENSTORE_IDX(prod) > MASK_XENSTORE_IDX(cons)) 
958b17f
+	if (prod > cons)
958b17f
 		to_read = prod - cons;
958b17f
 	else
958b17f
-		to_read = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
958b17f
+		to_read = XENSTORE_RING_SIZE - cons;
958b17f
 	if (to_read < len)
958b17f
 		len = to_read;
958b17f
-	memcpy(buffer, intf->req + MASK_XENSTORE_IDX(cons), len);
958b17f
+	memcpy(buffer, intf->req + cons, len);
958b17f
 	xen_mb();
958b17f
 	intf->req_cons += len;
958b17f
 	return len;
958b17f
@@ -66,8 +68,8 @@ static int xs_ring_write(struct mmap_interface *interface,
958b17f
 	XENSTORE_RING_IDX cons, prod;
958b17f
 	int can_write;
958b17f
 
958b17f
-	cons = intf->rsp_cons;
958b17f
-	prod = intf->rsp_prod;
958b17f
+	cons = *(volatile uint32*)&intf->rsp_cons;
958b17f
+	prod = *(volatile uint32*)&intf->rsp_prod;
958b17f
 	xen_mb();
958b17f
 	if ( (prod - cons) >= XENSTORE_RING_SIZE )
958b17f
 		return 0;