win-pvdrivers

view xenpci/xenbus.c @ 0:435e5753300f

Initial upload of files
author James Harper <james.harper@bendigoit.com.au>
date Sat Nov 10 14:40:56 2007 +1100 (2007-11-10)
parents
children 8f643f8e229b
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2007 James Harper
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
20 #include "xenpci.h"
21 #include "io/xs_wire.h"
22 #include <stdlib.h>
24 #pragma warning( disable : 4204 )
25 #pragma warning( disable : 4221 )
27 struct xenbus_req_info
28 {
29 int In_Use:1;
30 KEVENT WaitEvent;
31 void *Reply;
32 };
34 typedef struct {
35 char Path[128];
36 PXENBUS_WATCH_CALLBACK ServiceRoutine;
37 PVOID ServiceContext;
38 int Count;
39 int Active;
40 } XENBUS_WATCH_ENTRY, *PXENBUS_WATCH_ENTRY;
42 typedef struct {
43 char Path[128];
44 char Token[10];
45 } XENBUS_WATCH_RING;
47 #define WATCH_RING_SIZE 128
49 static XENBUS_WATCH_RING XenBus_WatchRing[WATCH_RING_SIZE];
50 static int XenBus_WatchRingReadIndex;
51 static int XenBus_WatchRingWriteIndex;
53 #define MAX_WATCH_ENTRIES 128
55 static XENBUS_WATCH_ENTRY XenBus_WatchEntries[MAX_WATCH_ENTRIES];
57 #define NR_REQS 32
58 //#define XENSTORE_RING_SIZE 1024
60 //#define XENSTORE_RING_SIZE 1024
61 //typedef uint32_t XENSTORE_RING_IDX;
62 //#define MASK_XENSTORE_IDX(idx) ((idx) & (XENSTORE_RING_SIZE-1))
64 static struct xenstore_domain_interface *xen_store_interface;
66 static struct xenbus_req_info req_info[NR_REQS];
67 static int nr_live_reqs;
68 //static spinlock_t req_lock = SPIN_LOCK_UNLOCKED;
70 static HANDLE XenBus_ReadThreadHandle;
71 static KEVENT XenBus_ReadThreadEvent;
73 static HANDLE XenBus_WatchThreadHandle;
74 static KEVENT XenBus_WatchThreadEvent;
76 static void
77 XenBus_ReadThreadProc(PVOID StartContext);
78 static void
79 XenBus_WatchThreadProc(PVOID StartContext);
81 static BOOLEAN
82 XenBus_Interrupt(PKINTERRUPT Interrupt, PVOID ServiceContext);
84 static int allocate_xenbus_id(void)
85 {
86 static int probe;
87 int o_probe;
89 //KdPrint((__DRIVER_NAME " --> allocate_xenbus_id\n"));
91 for (;;)
92 {
93 // spin_lock(&req_lock);
94 if (nr_live_reqs < NR_REQS)
95 break;
96 // spin_unlock(&req_lock);
97 // wait_event(req_wq, (nr_live_reqs < NR_REQS));
98 }
100 o_probe = probe;
102 for (;;)
103 {
104 if (!req_info[o_probe].In_Use)
105 break;
106 o_probe = (o_probe + 1) % NR_REQS;
107 // BUG_ON(o_probe == probe);
108 }
109 nr_live_reqs++;
110 req_info[o_probe].In_Use = 1;
111 probe = (o_probe + 1) % NR_REQS;
112 //spin_unlock(&req_lock);
113 //init_waitqueue_head(&req_info[o_probe].waitq);
114 KeInitializeEvent(&req_info[o_probe].WaitEvent, SynchronizationEvent, FALSE);
116 //KdPrint((__DRIVER_NAME " <-- allocate_xenbus_id\n"));
118 return o_probe;
119 }
121 static void release_xenbus_id(int id)
122 {
123 // BUG_ON(!req_info[id].in_use);
124 // spin_lock(&req_lock);
125 req_info[id].In_Use = 0;
126 nr_live_reqs--;
127 req_info[id].In_Use = 0;
128 // if (nr_live_reqs == NR_REQS - 1)
129 // wake_up(&req_wq);
130 // spin_unlock(&req_lock);
131 }
134 static char *errmsg(struct xsd_sockmsg *rep)
135 {
136 char *res;
137 if (!rep) {
138 char msg[] = "No reply";
139 size_t len = strlen(msg) + 1;
140 return memcpy(ExAllocatePoolWithTag(NonPagedPool, len, XENPCI_POOL_TAG), msg, len);
141 }
142 if (rep->type != XS_ERROR)
143 return NULL;
144 res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
145 memcpy(res, rep + 1, rep->len);
146 res[rep->len] = 0;
147 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
148 return res;
149 }
151 static void memcpy_from_ring(const void *Ring,
152 void *Dest,
153 int off,
154 int len)
155 {
156 int c1, c2;
157 const char *ring = Ring;
158 char *dest = Dest;
159 c1 = min(len, XENSTORE_RING_SIZE - off);
160 c2 = len - c1;
161 memcpy(dest, ring + off, c1);
162 memcpy(dest + c1, ring, c2);
163 }
165 void wait_for_watch(void)
166 {
167 // DEFINE_WAIT(w);
168 // add_waiter(w,watch_queue);
169 // schedule();
170 // remove_waiter(w);
171 // wake(current);
172 }
174 struct write_req {
175 const void *data;
176 unsigned len;
177 };
179 static evtchn_port_t xen_store_evtchn;
181 static void xb_write(int type, int req_id, xenbus_transaction_t trans_id,
182 const struct write_req *req, int nr_reqs)
183 {
184 XENSTORE_RING_IDX prod;
185 int r;
186 size_t len = 0;
187 const struct write_req *cur_req;
188 size_t req_off;
189 size_t total_off;
190 size_t this_chunk;
191 struct xsd_sockmsg m = {type, req_id, trans_id };
192 struct write_req header_req = { &m, sizeof(m) };
194 //KdPrint((__DRIVER_NAME " --> xb_write\n"));
196 for (r = 0; r < nr_reqs; r++)
197 len += req[r].len;
198 m.len = len;
199 len += sizeof(m);
201 cur_req = &header_req;
203 // BUG_ON(len > XENSTORE_RING_SIZE);
204 /* Wait for the ring to drain to the point where we can send the
205 message. */
206 prod = xen_store_interface->req_prod;
208 //KdPrint((__DRIVER_NAME " prod = %08x\n", prod));
210 if (prod + len - xen_store_interface->req_cons > XENSTORE_RING_SIZE)
211 {
212 /* Wait for there to be space on the ring */
213 //KdPrint((__DRIVER_NAME " prod %d, len %d, cons %d, size %d; waiting.\n", prod, len, xen_store_interface->req_cons, XENSTORE_RING_SIZE));
214 // wait_event(xb_waitq, xen_store_interface->req_prod + len - xen_store_interface->req_cons <= XENSTORE_RING_SIZE);
215 //KdPrint((__DRIVER_NAME " Back from wait.\n"));
216 prod = xen_store_interface->req_prod;
217 }
219 /* We're now guaranteed to be able to send the message without
220 overflowing the ring. Do so. */
222 total_off = 0;
223 req_off = 0;
225 while (total_off < len)
226 {
227 this_chunk = min(cur_req->len - req_off,XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod));
228 memcpy((char *)xen_store_interface->req + MASK_XENSTORE_IDX(prod), (char *)cur_req->data + req_off, this_chunk);
229 prod += this_chunk;
230 req_off += this_chunk;
231 total_off += this_chunk;
232 if (req_off == cur_req->len)
233 {
234 req_off = 0;
235 if (cur_req == &header_req)
236 cur_req = req;
237 else
238 cur_req++;
239 }
240 }
242 //KdPrint((__DRIVER_NAME " Complete main loop of xb_write.\n"));
244 // BUG_ON(req_off != 0);
245 // BUG_ON(total_off != len);
246 // BUG_ON(prod > xen_store_interface->req_cons + XENSTORE_RING_SIZE);
248 /* Remote must see entire message before updating indexes */
249 //_WriteBarrier();
250 KeMemoryBarrier();
252 xen_store_interface->req_prod += len;
254 //KdPrint((__DRIVER_NAME " prod = %08x\n", xen_store_interface->req_prod));
256 /* Send evtchn to notify remote */
257 EvtChn_Notify(xen_store_evtchn);
259 //KdPrint((__DRIVER_NAME " <-- xb_write\n"));
260 }
262 static struct xsd_sockmsg *
263 xenbus_msg_reply(int type,
264 xenbus_transaction_t trans,
265 struct write_req *io,
266 int nr_reqs)
267 {
268 int id;
269 // DEFINE_WAIT(w);
270 struct xsd_sockmsg *rep;
272 //KdPrint((__DRIVER_NAME " --> xenbus_msg_reply\n"));
274 id = allocate_xenbus_id();
275 // add_waiter(w, req_info[id].waitq);
277 xb_write(type, id, trans, io, nr_reqs);
278 //
279 // schedule();
280 // remove_waiter(w);
281 // wake(current);
282 //
283 //KdPrint((__DRIVER_NAME " starting wait\n"));
285 KeWaitForSingleObject(&req_info[id].WaitEvent, Executive, KernelMode, FALSE, NULL);
287 //KdPrint((__DRIVER_NAME " wait complete\n"));
289 rep = req_info[id].Reply;
290 // BUG_ON(rep->req_id != id);
291 release_xenbus_id(id);
292 //KdPrint((__DRIVER_NAME " <-- xenbus_msg_reply\n"));
293 return rep;
294 // return NULL;
295 }
297 char *
298 XenBus_Read(xenbus_transaction_t xbt, const char *path, char **value)
299 {
300 struct write_req req[] = { {path, strlen(path) + 1} };
301 struct xsd_sockmsg *rep;
302 char *res;
303 char *msg;
305 rep = xenbus_msg_reply(XS_READ, xbt, req, ARRAY_SIZE(req));
306 msg = errmsg(rep);
307 if (msg) {
308 *value = NULL;
309 return msg;
310 }
311 res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
312 memcpy(res, rep + 1, rep->len);
313 res[rep->len] = 0;
314 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
315 *value = res;
316 return NULL;
317 }
319 char *
320 XenBus_Write(xenbus_transaction_t xbt, const char *path, const char *value)
321 {
322 struct write_req req[] = {
323 {path, strlen(path) + 1},
324 {value, strlen(value) + 1},
325 };
326 struct xsd_sockmsg *rep;
327 char *msg;
329 rep = xenbus_msg_reply(XS_WRITE, xbt, req, ARRAY_SIZE(req));
330 msg = errmsg(rep);
331 if (msg)
332 return msg;
333 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
334 return NULL;
335 }
337 char* xenbus_wait_for_value(const char* path,const char* value)
338 {
339 UNREFERENCED_PARAMETER(path);
340 UNREFERENCED_PARAMETER(value);
341 // for(;;)
342 // {
343 // char *res, *msg;
344 // int r;
345 //
346 // msg = xenbus_read(XBT_NIL, path, &res);
347 // if(msg) return msg;
348 //
349 // r = strcmp(value,res);
350 // ExFreePoolWithTag(res, XENPCI_POOL_TAG);
351 //
352 // if(r==0)
353 // break;
354 // else
355 // wait_for_watch();
356 // }
357 return NULL;
358 }
360 NTSTATUS
361 XenBus_Init()
362 {
363 //KdPrint((__DRIVER_NAME " --> XenBus_Init\n"));
365 xen_store_evtchn = EvtChn_GetXenStorePort();
366 xen_store_interface = EvtChn_GetXenStoreRingAddr();
368 //KdPrint((__DRIVER_NAME " xen_store_evtchn = %08x\n", xen_store_evtchn));
369 //KdPrint((__DRIVER_NAME " xen_store_interface = %08x\n", xen_store_interface));
371 KeInitializeEvent(&XenBus_ReadThreadEvent, SynchronizationEvent, FALSE);
372 KeInitializeEvent(&XenBus_WatchThreadEvent, SynchronizationEvent, FALSE);
374 //KdPrint((__DRIVER_NAME " <-- XenBus_Init\n"));
376 return STATUS_SUCCESS;
377 }
379 NTSTATUS
380 XenBus_Start()
381 {
382 OBJECT_ATTRIBUTES oa;
383 NTSTATUS status;
384 int i;
386 //KdPrint((__DRIVER_NAME " --> XenBus_Start\n"));
388 InitializeObjectAttributes(&oa, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
389 status = PsCreateSystemThread(&XenBus_ReadThreadHandle, THREAD_ALL_ACCESS, &oa, NULL, NULL, XenBus_ReadThreadProc, NULL);
391 // remove all existing watches already in Xen too...
393 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
394 XenBus_WatchEntries[i].Active = 0;
396 InitializeObjectAttributes(&oa, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
397 status = PsCreateSystemThread(&XenBus_WatchThreadHandle, THREAD_ALL_ACCESS, &oa, NULL, NULL, XenBus_WatchThreadProc, NULL);
399 EvtChn_Bind(xen_store_evtchn, XenBus_Interrupt, NULL);
401 //KdPrint((__DRIVER_NAME " <-- XenBus_Start\n"));
403 return STATUS_SUCCESS;
404 }
406 NTSTATUS
407 XenBus_Stop()
408 {
409 int i;
411 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
412 {
413 if (!XenBus_WatchEntries[i].Active)
414 continue;
415 XenBus_RemWatch(XBT_NIL, XenBus_WatchEntries[i].Path, XenBus_WatchEntries[i].ServiceRoutine, XenBus_WatchEntries[i].ServiceContext);
416 }
418 EvtChn_Unbind(xen_store_evtchn);
420 // Does this actually stop the threads???
421 ZwClose(XenBus_WatchThreadHandle);
422 ZwClose(XenBus_ReadThreadHandle);
424 return STATUS_SUCCESS;
425 }
427 char *
428 XenBus_List(xenbus_transaction_t xbt, const char *pre, char ***contents)
429 {
430 struct xsd_sockmsg *reply, *repmsg;
431 struct write_req req[] = { { pre, strlen(pre)+1 } };
432 ULONG nr_elems, x, i;
433 char **res;
434 char *msg;
436 //KdPrint((__DRIVER_NAME " --> xenbus_ls\n"));
438 repmsg = xenbus_msg_reply(XS_DIRECTORY, xbt, req, ARRAY_SIZE(req));
439 msg = errmsg(repmsg);
440 if (msg) {
441 *contents = NULL;
442 //KdPrint((__DRIVER_NAME " <-- xenbus_ls (error)\n"));
443 return msg;
444 }
445 reply = repmsg + 1;
446 for (x = nr_elems = 0; x < repmsg->len; x++)
447 nr_elems += (((char *)reply)[x] == 0);
448 res = ExAllocatePoolWithTag(NonPagedPool, sizeof(res[0]) * (nr_elems + 1), XENPCI_POOL_TAG);
449 for (x = i = 0; i < nr_elems; i++) {
450 int l = strlen((char *)reply + x);
451 res[i] = ExAllocatePoolWithTag(NonPagedPool, l + 1, XENPCI_POOL_TAG);
452 memcpy(res[i], (char *)reply + x, l + 1);
453 x += l + 1;
454 }
455 res[i] = NULL;
456 ExFreePoolWithTag(repmsg, XENPCI_POOL_TAG);
457 *contents = res;
458 //KdPrint((__DRIVER_NAME " <-- xenbus_ls\n"));
459 return NULL;
460 }
462 void
463 do_ls_test(const char *pre)
464 {
465 char **dirs;
466 int x;
467 char *msg;
469 //KdPrint((__DRIVER_NAME " <-- do_ls_test(\"%s\")\n", pre));
471 msg = XenBus_List(XBT_NIL, pre, &dirs);
472 if (msg)
473 {
474 //KdPrint((__DRIVER_NAME " Error in xenbus ls: %s\n", msg));
475 ExFreePoolWithTag(msg, XENPCI_POOL_TAG);
476 return;
477 }
478 for (x = 0; dirs[x]; x++)
479 {
480 //KdPrint((__DRIVER_NAME " ls %s[%d] -> %s\n", pre, x, dirs[x]));
481 ExFreePoolWithTag(dirs[x], XENPCI_POOL_TAG);
482 }
483 ExFreePoolWithTag(dirs, XENPCI_POOL_TAG);
484 //KdPrint((__DRIVER_NAME " --> do_ls_test\n"));
485 }
487 int ReadThreadSetCount;
488 int ReadThreadWaitCount;
490 static void
491 XenBus_ReadThreadProc(PVOID StartContext) {
492 int NewWriteIndex;
493 struct xsd_sockmsg msg;
494 char *payload;
495 char *path, *token;
497 UNREFERENCED_PARAMETER(StartContext);
499 for(;;)
500 {
501 KeWaitForSingleObject(&XenBus_ReadThreadEvent, Executive, KernelMode, FALSE, NULL);
502 //KdPrint((__DRIVER_NAME " ReadThread Woken (Count = %d)\n", ReadThreadWaitCount++));
503 while (xen_store_interface->rsp_prod != xen_store_interface->rsp_cons)
504 {
505 //KdPrint((__DRIVER_NAME " a - Rsp_cons %d, rsp_prod %d.\n", xen_store_interface->rsp_cons, xen_store_interface->rsp_prod));
506 if (xen_store_interface->rsp_prod - xen_store_interface->rsp_cons < sizeof(msg))
507 {
508 //KdPrint((__DRIVER_NAME " +++ Message incomplete (not even a full header)\n"));
509 break;
510 }
511 //_ReadBarrier();
512 KeMemoryBarrier();
513 memcpy_from_ring(xen_store_interface->rsp, &msg, MASK_XENSTORE_IDX(xen_store_interface->rsp_cons), sizeof(msg));
514 if (xen_store_interface->rsp_prod - xen_store_interface->rsp_cons < sizeof(msg) + msg.len)
515 {
516 //KdPrint((__DRIVER_NAME " +++ Message incomplete (header but not full body)\n"));
517 break;
518 }
520 if(msg.type == XS_WATCH_EVENT)
521 {
522 payload = ExAllocatePoolWithTag(NonPagedPool, sizeof(msg) + msg.len, XENPCI_POOL_TAG);
524 memcpy_from_ring(xen_store_interface->rsp, payload, MASK_XENSTORE_IDX(xen_store_interface->rsp_cons), msg.len + sizeof(msg));
526 xen_store_interface->rsp_cons += msg.len + sizeof(msg);
527 //KdPrint((__DRIVER_NAME " b - Rsp_cons %d, rsp_prod %d.\n", xen_store_interface->rsp_cons, xen_store_interface->rsp_prod));
529 path = payload + sizeof(msg);
530 token = path + strlen(path) + 1;
532 NewWriteIndex = (XenBus_WatchRingWriteIndex + 1) & 127;
533 if (NewWriteIndex != XenBus_WatchRingReadIndex)
534 {
535 strncpy(XenBus_WatchRing[NewWriteIndex].Path, path, 128);
536 strncpy(XenBus_WatchRing[NewWriteIndex].Token, token, 10);
537 XenBus_WatchRingWriteIndex = NewWriteIndex;
538 }
539 else
540 {
541 KdPrint((__DRIVER_NAME " +++ Queue full Path = %s Token = %s\n", path, token));
542 // drop the message on the floor
543 continue;
544 }
546 ExFreePoolWithTag(payload, XENPCI_POOL_TAG);
547 //KdPrint((__DRIVER_NAME " +++ Watch Path = %s Token = %s\n", path, token));
548 KeSetEvent(&XenBus_WatchThreadEvent, 1, FALSE);
549 }
550 else
551 {
552 req_info[msg.req_id].Reply = ExAllocatePoolWithTag(NonPagedPool, sizeof(msg) + msg.len, XENPCI_POOL_TAG);
553 memcpy_from_ring(xen_store_interface->rsp, req_info[msg.req_id].Reply, MASK_XENSTORE_IDX(xen_store_interface->rsp_cons), msg.len + sizeof(msg));
554 xen_store_interface->rsp_cons += msg.len + sizeof(msg);
555 //KdPrint((__DRIVER_NAME " c - Rsp_cons %d, rsp_prod %d.\n", xen_store_interface->rsp_cons, xen_store_interface->rsp_prod));
556 //KdPrint((__DRIVER_NAME " +++ Message = %s\n", ((char *)req_info[msg.req_id].Reply) + sizeof(msg)));
557 KeSetEvent(&req_info[msg.req_id].WaitEvent, 1, FALSE);
558 }
559 }
560 }
561 }
563 static void
564 XenBus_WatchThreadProc(PVOID StartContext)
565 {
566 int index;
567 PXENBUS_WATCH_ENTRY entry;
569 UNREFERENCED_PARAMETER(StartContext);
571 for(;;)
572 {
573 KeWaitForSingleObject(&XenBus_WatchThreadEvent, Executive, KernelMode, FALSE, NULL);
574 while (XenBus_WatchRingReadIndex != XenBus_WatchRingWriteIndex)
575 {
576 XenBus_WatchRingReadIndex = (XenBus_WatchRingReadIndex + 1) & 127;
577 index = atoi(XenBus_WatchRing[XenBus_WatchRingReadIndex].Token);
578 //XenBus_WatchRing[XenBus_WatchRingReadIndex].Path
579 //XenBus_WatchRing[XenBus_WatchRingReadIndex].Token
581 entry = &XenBus_WatchEntries[index];
582 if (!entry->Active)
583 {
584 KdPrint((__DRIVER_NAME " +++ Watch not active! = %s Token = %s\n", XenBus_WatchRing[XenBus_WatchRingReadIndex].Path, XenBus_WatchRing[XenBus_WatchRingReadIndex].Token));
585 continue;
586 }
587 entry->Count++;
588 if (!entry->ServiceRoutine)
589 {
590 KdPrint((__DRIVER_NAME " +++ no handler for watch! = %s Token = %s\n", XenBus_WatchRing[XenBus_WatchRingReadIndex].Path, XenBus_WatchRing[XenBus_WatchRingReadIndex].Token));
591 continue;
592 }
593 //KdPrint((__DRIVER_NAME " +++ Watch Triggered Path = %s Token = %d (%s)\n", XenBus_WatchRing[XenBus_WatchRingReadIndex].Path, index, XenBus_WatchRing[XenBus_WatchRingReadIndex].Token));
594 entry->ServiceRoutine(XenBus_WatchRing[XenBus_WatchRingReadIndex].Path, entry->ServiceContext);
595 }
596 }
597 }
599 char *
600 XenBus_AddWatch(xenbus_transaction_t xbt, const char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext)
601 {
602 struct xsd_sockmsg *rep;
603 char *msg;
604 int i;
605 char Token[20];
606 struct write_req req[2];
608 //KdPrint((__DRIVER_NAME " --> XenBus_AddWatch\n"));
610 // check that Path < 128 chars
612 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
613 if (XenBus_WatchEntries[i].Active == 0)
614 break;
616 if (i == MAX_WATCH_ENTRIES)
617 {
618 KdPrint((__DRIVER_NAME " +++ No more watch slots left\n"));
619 return NULL;
620 }
622 req[0].data = Path;
623 req[0].len = strlen(Path) + 1;
625 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", i);
626 req[1].data = Token;
627 req[1].len = strlen(Token) + 1;
629 rep = xenbus_msg_reply(XS_WATCH, xbt, req, ARRAY_SIZE(req));
631 msg = errmsg(rep);
632 if (msg)
633 return msg;
635 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
637 strncpy(XenBus_WatchEntries[i].Path, Path, 128);
638 XenBus_WatchEntries[i].ServiceRoutine = ServiceRoutine;
639 XenBus_WatchEntries[i].ServiceContext = ServiceContext;
640 XenBus_WatchEntries[i].Count = 0;
641 XenBus_WatchEntries[i].Active = 1;
643 //KdPrint((__DRIVER_NAME " <-- XenBus_AddWatch\n"));
645 return NULL;
646 }
648 char *
649 XenBus_RemWatch(xenbus_transaction_t xbt, const char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext)
650 {
651 struct xsd_sockmsg *rep;
652 char *msg;
653 int i;
654 char Token[20];
655 struct write_req req[2];
657 //KdPrint((__DRIVER_NAME " --> XenBus_RemWatch\n"));
659 // check that Path < 128 chars
661 for (i = 0; i < MAX_WATCH_ENTRIES; i++) {
662 if (XenBus_WatchEntries[i].Active == 1 && strcmp(XenBus_WatchEntries[i].Path, Path) == 0 && XenBus_WatchEntries[i].ServiceRoutine == ServiceRoutine && XenBus_WatchEntries[i].ServiceContext == ServiceContext)
663 break;
664 }
666 if (i == MAX_WATCH_ENTRIES)
667 {
668 KdPrint((__DRIVER_NAME " Watch not set - can't remove\n"));
669 return NULL;
670 }
672 req[0].data = Path;
673 req[0].len = strlen(Path) + 1;
675 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", i);
676 req[1].data = Token;
677 req[1].len = strlen(Token) + 1;
679 rep = xenbus_msg_reply(XS_UNWATCH, xbt, req, ARRAY_SIZE(req));
681 msg = errmsg(rep);
682 if (msg)
683 return msg;
685 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
687 XenBus_WatchEntries[i].Active = 0;
689 //KdPrint((__DRIVER_NAME " <-- XenBus_RemWatch\n"));
691 return NULL;
692 }
695 char *
696 XenBus_StartTransaction(xenbus_transaction_t *xbt)
697 {
698 /* xenstored becomes angry if you send a length 0 message, so just
699 shove a nul terminator on the end */
700 struct write_req req = { "", 1};
701 struct xsd_sockmsg *rep;
702 char *err;
704 rep = xenbus_msg_reply(XS_TRANSACTION_START, 0, &req, 1);
705 err = errmsg(rep);
706 if (err)
707 return err;
708 *xbt = atoi((char *)(rep + 1));
709 //sscanf((char *)(rep + 1), "%u", xbt);
710 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
711 return NULL;
712 }
714 char *
715 XenBus_EndTransaction(xenbus_transaction_t t, int abort, int *retry)
716 {
717 struct xsd_sockmsg *rep;
718 struct write_req req;
719 char *err;
721 *retry = 0;
723 req.data = abort ? "F" : "T";
724 req.len = 2;
725 rep = xenbus_msg_reply(XS_TRANSACTION_END, t, &req, 1);
726 err = errmsg(rep);
727 if (err) {
728 if (!strcmp(err, "EAGAIN")) {
729 *retry = 1;
730 ExFreePoolWithTag(err, XENPCI_POOL_TAG);
731 return NULL;
732 } else {
733 return err;
734 }
735 }
736 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
737 return NULL;
738 }
739 /*
740 void
741 XenBus_ThreadProc(PVOID StartContext)
742 {
743 char *response;
745 //KdPrint((__DRIVER_NAME " --> XenBus_ThreadProc\n"));
747 //do_ls_test("device");
749 // do_ls_test("local");
751 // do_ls_test("control");
753 // do_ls_test(".");
755 response = XenBus_AddWatch(XBT_NIL, SHUTDOWN_PATH, XenBus_ShutdownHandler, NULL);
757 //KdPrint((__DRIVER_NAME " <-- watch response = '%s'\n", response));
759 //KdPrint((__DRIVER_NAME " <-- XenBus_ThreadProc\n"));
760 }
761 */
763 static BOOLEAN
764 XenBus_Interrupt(PKINTERRUPT Interrupt, PVOID ServiceContext)
765 {
766 UNREFERENCED_PARAMETER(Interrupt);
767 UNREFERENCED_PARAMETER(ServiceContext);
769 //KdPrint((__DRIVER_NAME " --> XenBus_Interrupt (Count = %d)\n", ReadThreadSetCount++));
771 KeSetEvent(&XenBus_ReadThreadEvent, 1, FALSE);
773 //KdPrint((__DRIVER_NAME " <-- XenBus_Interrupt\n"));
775 return TRUE;
776 }
778 char *
779 XenBus_Printf(xenbus_transaction_t xbt, const char *path, const char *fmt, ...)
780 {
781 va_list ap;
782 char buf[1024];
784 va_start(ap, fmt);
785 RtlStringCbVPrintfA(buf, ARRAY_SIZE(buf), fmt, ap);
786 va_end(ap);
787 return XenBus_Write(xbt, path, buf);