win-pvdrivers

annotate xenpci/xenbus.c @ 6:8f643f8e229b

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