win-pvdrivers

annotate 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
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@0 263 xenbus_msg_reply(int type,
james@0 264 xenbus_transaction_t trans,
james@0 265 struct write_req *io,
james@0 266 int nr_reqs)
james@0 267 {
james@0 268 int id;
james@0 269 // DEFINE_WAIT(w);
james@0 270 struct xsd_sockmsg *rep;
james@0 271
james@0 272 //KdPrint((__DRIVER_NAME " --> xenbus_msg_reply\n"));
james@0 273
james@0 274 id = allocate_xenbus_id();
james@0 275 // add_waiter(w, req_info[id].waitq);
james@0 276
james@0 277 xb_write(type, id, trans, io, nr_reqs);
james@0 278 //
james@0 279 // schedule();
james@0 280 // remove_waiter(w);
james@0 281 // wake(current);
james@0 282 //
james@0 283 //KdPrint((__DRIVER_NAME " starting wait\n"));
james@0 284
james@0 285 KeWaitForSingleObject(&req_info[id].WaitEvent, Executive, KernelMode, FALSE, NULL);
james@0 286
james@0 287 //KdPrint((__DRIVER_NAME " wait complete\n"));
james@0 288
james@0 289 rep = req_info[id].Reply;
james@0 290 // BUG_ON(rep->req_id != id);
james@0 291 release_xenbus_id(id);
james@0 292 //KdPrint((__DRIVER_NAME " <-- xenbus_msg_reply\n"));
james@0 293 return rep;
james@0 294 // return NULL;
james@0 295 }
james@0 296
james@0 297 char *
james@0 298 XenBus_Read(xenbus_transaction_t xbt, const char *path, char **value)
james@0 299 {
james@0 300 struct write_req req[] = { {path, strlen(path) + 1} };
james@0 301 struct xsd_sockmsg *rep;
james@0 302 char *res;
james@0 303 char *msg;
james@0 304
james@0 305 rep = xenbus_msg_reply(XS_READ, xbt, req, ARRAY_SIZE(req));
james@0 306 msg = errmsg(rep);
james@0 307 if (msg) {
james@0 308 *value = NULL;
james@0 309 return msg;
james@0 310 }
james@0 311 res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
james@0 312 memcpy(res, rep + 1, rep->len);
james@0 313 res[rep->len] = 0;
james@0 314 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 315 *value = res;
james@0 316 return NULL;
james@0 317 }
james@0 318
james@0 319 char *
james@0 320 XenBus_Write(xenbus_transaction_t xbt, const char *path, const char *value)
james@0 321 {
james@0 322 struct write_req req[] = {
james@0 323 {path, strlen(path) + 1},
james@0 324 {value, strlen(value) + 1},
james@0 325 };
james@0 326 struct xsd_sockmsg *rep;
james@0 327 char *msg;
james@0 328
james@0 329 rep = xenbus_msg_reply(XS_WRITE, xbt, req, ARRAY_SIZE(req));
james@0 330 msg = errmsg(rep);
james@0 331 if (msg)
james@0 332 return msg;
james@0 333 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 334 return NULL;
james@0 335 }
james@0 336
james@0 337 char* xenbus_wait_for_value(const char* path,const char* value)
james@0 338 {
james@0 339 UNREFERENCED_PARAMETER(path);
james@0 340 UNREFERENCED_PARAMETER(value);
james@0 341 // for(;;)
james@0 342 // {
james@0 343 // char *res, *msg;
james@0 344 // int r;
james@0 345 //
james@0 346 // msg = xenbus_read(XBT_NIL, path, &res);
james@0 347 // if(msg) return msg;
james@0 348 //
james@0 349 // r = strcmp(value,res);
james@0 350 // ExFreePoolWithTag(res, XENPCI_POOL_TAG);
james@0 351 //
james@0 352 // if(r==0)
james@0 353 // break;
james@0 354 // else
james@0 355 // wait_for_watch();
james@0 356 // }
james@0 357 return NULL;
james@0 358 }
james@0 359
james@0 360 NTSTATUS
james@0 361 XenBus_Init()
james@0 362 {
james@0 363 //KdPrint((__DRIVER_NAME " --> XenBus_Init\n"));
james@0 364
james@0 365 xen_store_evtchn = EvtChn_GetXenStorePort();
james@0 366 xen_store_interface = EvtChn_GetXenStoreRingAddr();
james@0 367
james@0 368 //KdPrint((__DRIVER_NAME " xen_store_evtchn = %08x\n", xen_store_evtchn));
james@0 369 //KdPrint((__DRIVER_NAME " xen_store_interface = %08x\n", xen_store_interface));
james@0 370
james@0 371 KeInitializeEvent(&XenBus_ReadThreadEvent, SynchronizationEvent, FALSE);
james@0 372 KeInitializeEvent(&XenBus_WatchThreadEvent, SynchronizationEvent, FALSE);
james@0 373
james@0 374 //KdPrint((__DRIVER_NAME " <-- XenBus_Init\n"));
james@0 375
james@0 376 return STATUS_SUCCESS;
james@0 377 }
james@0 378
james@0 379 NTSTATUS
james@0 380 XenBus_Start()
james@0 381 {
james@0 382 OBJECT_ATTRIBUTES oa;
james@0 383 NTSTATUS status;
james@0 384 int i;
james@0 385
james@0 386 //KdPrint((__DRIVER_NAME " --> XenBus_Start\n"));
james@0 387
james@0 388 InitializeObjectAttributes(&oa, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
james@0 389 status = PsCreateSystemThread(&XenBus_ReadThreadHandle, THREAD_ALL_ACCESS, &oa, NULL, NULL, XenBus_ReadThreadProc, NULL);
james@0 390
james@0 391 // remove all existing watches already in Xen too...
james@0 392
james@0 393 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@0 394 XenBus_WatchEntries[i].Active = 0;
james@0 395
james@0 396 InitializeObjectAttributes(&oa, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
james@0 397 status = PsCreateSystemThread(&XenBus_WatchThreadHandle, THREAD_ALL_ACCESS, &oa, NULL, NULL, XenBus_WatchThreadProc, NULL);
james@0 398
james@0 399 EvtChn_Bind(xen_store_evtchn, XenBus_Interrupt, NULL);
james@0 400
james@0 401 //KdPrint((__DRIVER_NAME " <-- XenBus_Start\n"));
james@0 402
james@0 403 return STATUS_SUCCESS;
james@0 404 }
james@0 405
james@0 406 NTSTATUS
james@0 407 XenBus_Stop()
james@0 408 {
james@0 409 int i;
james@0 410
james@0 411 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@0 412 {
james@0 413 if (!XenBus_WatchEntries[i].Active)
james@0 414 continue;
james@0 415 XenBus_RemWatch(XBT_NIL, XenBus_WatchEntries[i].Path, XenBus_WatchEntries[i].ServiceRoutine, XenBus_WatchEntries[i].ServiceContext);
james@0 416 }
james@0 417
james@0 418 EvtChn_Unbind(xen_store_evtchn);
james@0 419
james@0 420 // Does this actually stop the threads???
james@0 421 ZwClose(XenBus_WatchThreadHandle);
james@0 422 ZwClose(XenBus_ReadThreadHandle);
james@0 423
james@0 424 return STATUS_SUCCESS;
james@0 425 }
james@0 426
james@0 427 char *
james@0 428 XenBus_List(xenbus_transaction_t xbt, const char *pre, char ***contents)
james@0 429 {
james@0 430 struct xsd_sockmsg *reply, *repmsg;
james@0 431 struct write_req req[] = { { pre, strlen(pre)+1 } };
james@0 432 ULONG nr_elems, x, i;
james@0 433 char **res;
james@0 434 char *msg;
james@0 435
james@0 436 //KdPrint((__DRIVER_NAME " --> xenbus_ls\n"));
james@0 437
james@0 438 repmsg = xenbus_msg_reply(XS_DIRECTORY, xbt, req, ARRAY_SIZE(req));
james@0 439 msg = errmsg(repmsg);
james@0 440 if (msg) {
james@0 441 *contents = NULL;
james@0 442 //KdPrint((__DRIVER_NAME " <-- xenbus_ls (error)\n"));
james@0 443 return msg;
james@0 444 }
james@0 445 reply = repmsg + 1;
james@0 446 for (x = nr_elems = 0; x < repmsg->len; x++)
james@0 447 nr_elems += (((char *)reply)[x] == 0);
james@0 448 res = ExAllocatePoolWithTag(NonPagedPool, sizeof(res[0]) * (nr_elems + 1), XENPCI_POOL_TAG);
james@0 449 for (x = i = 0; i < nr_elems; i++) {
james@0 450 int l = strlen((char *)reply + x);
james@0 451 res[i] = ExAllocatePoolWithTag(NonPagedPool, l + 1, XENPCI_POOL_TAG);
james@0 452 memcpy(res[i], (char *)reply + x, l + 1);
james@0 453 x += l + 1;
james@0 454 }
james@0 455 res[i] = NULL;
james@0 456 ExFreePoolWithTag(repmsg, XENPCI_POOL_TAG);
james@0 457 *contents = res;
james@0 458 //KdPrint((__DRIVER_NAME " <-- xenbus_ls\n"));
james@0 459 return NULL;
james@0 460 }
james@0 461
james@0 462 void
james@0 463 do_ls_test(const char *pre)
james@0 464 {
james@0 465 char **dirs;
james@0 466 int x;
james@0 467 char *msg;
james@0 468
james@0 469 //KdPrint((__DRIVER_NAME " <-- do_ls_test(\"%s\")\n", pre));
james@0 470
james@0 471 msg = XenBus_List(XBT_NIL, pre, &dirs);
james@0 472 if (msg)
james@0 473 {
james@0 474 //KdPrint((__DRIVER_NAME " Error in xenbus ls: %s\n", msg));
james@0 475 ExFreePoolWithTag(msg, XENPCI_POOL_TAG);
james@0 476 return;
james@0 477 }
james@0 478 for (x = 0; dirs[x]; x++)
james@0 479 {
james@0 480 //KdPrint((__DRIVER_NAME " ls %s[%d] -> %s\n", pre, x, dirs[x]));
james@0 481 ExFreePoolWithTag(dirs[x], XENPCI_POOL_TAG);
james@0 482 }
james@0 483 ExFreePoolWithTag(dirs, XENPCI_POOL_TAG);
james@0 484 //KdPrint((__DRIVER_NAME " --> do_ls_test\n"));
james@0 485 }
james@0 486
james@0 487 int ReadThreadSetCount;
james@0 488 int ReadThreadWaitCount;
james@0 489
james@0 490 static void
james@0 491 XenBus_ReadThreadProc(PVOID StartContext) {
james@0 492 int NewWriteIndex;
james@0 493 struct xsd_sockmsg msg;
james@0 494 char *payload;
james@0 495 char *path, *token;
james@0 496
james@0 497 UNREFERENCED_PARAMETER(StartContext);
james@0 498
james@0 499 for(;;)
james@0 500 {
james@0 501 KeWaitForSingleObject(&XenBus_ReadThreadEvent, Executive, KernelMode, FALSE, NULL);
james@0 502 //KdPrint((__DRIVER_NAME " ReadThread Woken (Count = %d)\n", ReadThreadWaitCount++));
james@0 503 while (xen_store_interface->rsp_prod != xen_store_interface->rsp_cons)
james@0 504 {
james@0 505 //KdPrint((__DRIVER_NAME " a - Rsp_cons %d, rsp_prod %d.\n", xen_store_interface->rsp_cons, xen_store_interface->rsp_prod));
james@0 506 if (xen_store_interface->rsp_prod - xen_store_interface->rsp_cons < sizeof(msg))
james@0 507 {
james@0 508 //KdPrint((__DRIVER_NAME " +++ Message incomplete (not even a full header)\n"));
james@0 509 break;
james@0 510 }
james@0 511 //_ReadBarrier();
james@0 512 KeMemoryBarrier();
james@0 513 memcpy_from_ring(xen_store_interface->rsp, &msg, MASK_XENSTORE_IDX(xen_store_interface->rsp_cons), sizeof(msg));
james@0 514 if (xen_store_interface->rsp_prod - xen_store_interface->rsp_cons < sizeof(msg) + msg.len)
james@0 515 {
james@0 516 //KdPrint((__DRIVER_NAME " +++ Message incomplete (header but not full body)\n"));
james@0 517 break;
james@0 518 }
james@0 519
james@0 520 if(msg.type == XS_WATCH_EVENT)
james@0 521 {
james@0 522 payload = ExAllocatePoolWithTag(NonPagedPool, sizeof(msg) + msg.len, XENPCI_POOL_TAG);
james@0 523
james@0 524 memcpy_from_ring(xen_store_interface->rsp, payload, MASK_XENSTORE_IDX(xen_store_interface->rsp_cons), msg.len + sizeof(msg));
james@0 525
james@0 526 xen_store_interface->rsp_cons += msg.len + sizeof(msg);
james@0 527 //KdPrint((__DRIVER_NAME " b - Rsp_cons %d, rsp_prod %d.\n", xen_store_interface->rsp_cons, xen_store_interface->rsp_prod));
james@0 528
james@0 529 path = payload + sizeof(msg);
james@0 530 token = path + strlen(path) + 1;
james@0 531
james@0 532 NewWriteIndex = (XenBus_WatchRingWriteIndex + 1) & 127;
james@0 533 if (NewWriteIndex != XenBus_WatchRingReadIndex)
james@0 534 {
james@0 535 strncpy(XenBus_WatchRing[NewWriteIndex].Path, path, 128);
james@0 536 strncpy(XenBus_WatchRing[NewWriteIndex].Token, token, 10);
james@0 537 XenBus_WatchRingWriteIndex = NewWriteIndex;
james@0 538 }
james@0 539 else
james@0 540 {
james@0 541 KdPrint((__DRIVER_NAME " +++ Queue full Path = %s Token = %s\n", path, token));
james@0 542 // drop the message on the floor
james@0 543 continue;
james@0 544 }
james@0 545
james@0 546 ExFreePoolWithTag(payload, XENPCI_POOL_TAG);
james@0 547 //KdPrint((__DRIVER_NAME " +++ Watch Path = %s Token = %s\n", path, token));
james@0 548 KeSetEvent(&XenBus_WatchThreadEvent, 1, FALSE);
james@0 549 }
james@0 550 else
james@0 551 {
james@0 552 req_info[msg.req_id].Reply = ExAllocatePoolWithTag(NonPagedPool, sizeof(msg) + msg.len, XENPCI_POOL_TAG);
james@0 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));
james@0 554 xen_store_interface->rsp_cons += msg.len + sizeof(msg);
james@0 555 //KdPrint((__DRIVER_NAME " c - Rsp_cons %d, rsp_prod %d.\n", xen_store_interface->rsp_cons, xen_store_interface->rsp_prod));
james@0 556 //KdPrint((__DRIVER_NAME " +++ Message = %s\n", ((char *)req_info[msg.req_id].Reply) + sizeof(msg)));
james@0 557 KeSetEvent(&req_info[msg.req_id].WaitEvent, 1, FALSE);
james@0 558 }
james@0 559 }
james@0 560 }
james@0 561 }
james@0 562
james@0 563 static void
james@0 564 XenBus_WatchThreadProc(PVOID StartContext)
james@0 565 {
james@0 566 int index;
james@0 567 PXENBUS_WATCH_ENTRY entry;
james@0 568
james@0 569 UNREFERENCED_PARAMETER(StartContext);
james@0 570
james@0 571 for(;;)
james@0 572 {
james@0 573 KeWaitForSingleObject(&XenBus_WatchThreadEvent, Executive, KernelMode, FALSE, NULL);
james@0 574 while (XenBus_WatchRingReadIndex != XenBus_WatchRingWriteIndex)
james@0 575 {
james@0 576 XenBus_WatchRingReadIndex = (XenBus_WatchRingReadIndex + 1) & 127;
james@0 577 index = atoi(XenBus_WatchRing[XenBus_WatchRingReadIndex].Token);
james@0 578 //XenBus_WatchRing[XenBus_WatchRingReadIndex].Path
james@0 579 //XenBus_WatchRing[XenBus_WatchRingReadIndex].Token
james@0 580
james@0 581 entry = &XenBus_WatchEntries[index];
james@0 582 if (!entry->Active)
james@0 583 {
james@0 584 KdPrint((__DRIVER_NAME " +++ Watch not active! = %s Token = %s\n", XenBus_WatchRing[XenBus_WatchRingReadIndex].Path, XenBus_WatchRing[XenBus_WatchRingReadIndex].Token));
james@0 585 continue;
james@0 586 }
james@0 587 entry->Count++;
james@0 588 if (!entry->ServiceRoutine)
james@0 589 {
james@0 590 KdPrint((__DRIVER_NAME " +++ no handler for watch! = %s Token = %s\n", XenBus_WatchRing[XenBus_WatchRingReadIndex].Path, XenBus_WatchRing[XenBus_WatchRingReadIndex].Token));
james@0 591 continue;
james@0 592 }
james@0 593 //KdPrint((__DRIVER_NAME " +++ Watch Triggered Path = %s Token = %d (%s)\n", XenBus_WatchRing[XenBus_WatchRingReadIndex].Path, index, XenBus_WatchRing[XenBus_WatchRingReadIndex].Token));
james@0 594 entry->ServiceRoutine(XenBus_WatchRing[XenBus_WatchRingReadIndex].Path, entry->ServiceContext);
james@0 595 }
james@0 596 }
james@0 597 }
james@0 598
james@0 599 char *
james@0 600 XenBus_AddWatch(xenbus_transaction_t xbt, const char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext)
james@0 601 {
james@0 602 struct xsd_sockmsg *rep;
james@0 603 char *msg;
james@0 604 int i;
james@0 605 char Token[20];
james@0 606 struct write_req req[2];
james@0 607
james@0 608 //KdPrint((__DRIVER_NAME " --> XenBus_AddWatch\n"));
james@0 609
james@0 610 // check that Path < 128 chars
james@0 611
james@0 612 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@0 613 if (XenBus_WatchEntries[i].Active == 0)
james@0 614 break;
james@0 615
james@0 616 if (i == MAX_WATCH_ENTRIES)
james@0 617 {
james@0 618 KdPrint((__DRIVER_NAME " +++ No more watch slots left\n"));
james@0 619 return NULL;
james@0 620 }
james@0 621
james@0 622 req[0].data = Path;
james@0 623 req[0].len = strlen(Path) + 1;
james@0 624
james@0 625 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", i);
james@0 626 req[1].data = Token;
james@0 627 req[1].len = strlen(Token) + 1;
james@0 628
james@0 629 rep = xenbus_msg_reply(XS_WATCH, xbt, req, ARRAY_SIZE(req));
james@0 630
james@0 631 msg = errmsg(rep);
james@0 632 if (msg)
james@0 633 return msg;
james@0 634
james@0 635 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 636
james@0 637 strncpy(XenBus_WatchEntries[i].Path, Path, 128);
james@0 638 XenBus_WatchEntries[i].ServiceRoutine = ServiceRoutine;
james@0 639 XenBus_WatchEntries[i].ServiceContext = ServiceContext;
james@0 640 XenBus_WatchEntries[i].Count = 0;
james@0 641 XenBus_WatchEntries[i].Active = 1;
james@0 642
james@0 643 //KdPrint((__DRIVER_NAME " <-- XenBus_AddWatch\n"));
james@0 644
james@0 645 return NULL;
james@0 646 }
james@0 647
james@0 648 char *
james@0 649 XenBus_RemWatch(xenbus_transaction_t xbt, const char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext)
james@0 650 {
james@0 651 struct xsd_sockmsg *rep;
james@0 652 char *msg;
james@0 653 int i;
james@0 654 char Token[20];
james@0 655 struct write_req req[2];
james@0 656
james@0 657 //KdPrint((__DRIVER_NAME " --> XenBus_RemWatch\n"));
james@0 658
james@0 659 // check that Path < 128 chars
james@0 660
james@0 661 for (i = 0; i < MAX_WATCH_ENTRIES; i++) {
james@0 662 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 663 break;
james@0 664 }
james@0 665
james@0 666 if (i == MAX_WATCH_ENTRIES)
james@0 667 {
james@0 668 KdPrint((__DRIVER_NAME " Watch not set - can't remove\n"));
james@0 669 return NULL;
james@0 670 }
james@0 671
james@0 672 req[0].data = Path;
james@0 673 req[0].len = strlen(Path) + 1;
james@0 674
james@0 675 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", i);
james@0 676 req[1].data = Token;
james@0 677 req[1].len = strlen(Token) + 1;
james@0 678
james@0 679 rep = xenbus_msg_reply(XS_UNWATCH, xbt, req, ARRAY_SIZE(req));
james@0 680
james@0 681 msg = errmsg(rep);
james@0 682 if (msg)
james@0 683 return msg;
james@0 684
james@0 685 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 686
james@0 687 XenBus_WatchEntries[i].Active = 0;
james@0 688
james@0 689 //KdPrint((__DRIVER_NAME " <-- XenBus_RemWatch\n"));
james@0 690
james@0 691 return NULL;
james@0 692 }
james@0 693
james@0 694
james@0 695 char *
james@0 696 XenBus_StartTransaction(xenbus_transaction_t *xbt)
james@0 697 {
james@0 698 /* xenstored becomes angry if you send a length 0 message, so just
james@0 699 shove a nul terminator on the end */
james@0 700 struct write_req req = { "", 1};
james@0 701 struct xsd_sockmsg *rep;
james@0 702 char *err;
james@0 703
james@0 704 rep = xenbus_msg_reply(XS_TRANSACTION_START, 0, &req, 1);
james@0 705 err = errmsg(rep);
james@0 706 if (err)
james@0 707 return err;
james@0 708 *xbt = atoi((char *)(rep + 1));
james@0 709 //sscanf((char *)(rep + 1), "%u", xbt);
james@0 710 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 711 return NULL;
james@0 712 }
james@0 713
james@0 714 char *
james@0 715 XenBus_EndTransaction(xenbus_transaction_t t, int abort, int *retry)
james@0 716 {
james@0 717 struct xsd_sockmsg *rep;
james@0 718 struct write_req req;
james@0 719 char *err;
james@0 720
james@0 721 *retry = 0;
james@0 722
james@0 723 req.data = abort ? "F" : "T";
james@0 724 req.len = 2;
james@0 725 rep = xenbus_msg_reply(XS_TRANSACTION_END, t, &req, 1);
james@0 726 err = errmsg(rep);
james@0 727 if (err) {
james@0 728 if (!strcmp(err, "EAGAIN")) {
james@0 729 *retry = 1;
james@0 730 ExFreePoolWithTag(err, XENPCI_POOL_TAG);
james@0 731 return NULL;
james@0 732 } else {
james@0 733 return err;
james@0 734 }
james@0 735 }
james@0 736 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 737 return NULL;
james@0 738 }
james@0 739 /*
james@0 740 void
james@0 741 XenBus_ThreadProc(PVOID StartContext)
james@0 742 {
james@0 743 char *response;
james@0 744
james@0 745 //KdPrint((__DRIVER_NAME " --> XenBus_ThreadProc\n"));
james@0 746
james@0 747 //do_ls_test("device");
james@0 748
james@0 749 // do_ls_test("local");
james@0 750
james@0 751 // do_ls_test("control");
james@0 752
james@0 753 // do_ls_test(".");
james@0 754
james@0 755 response = XenBus_AddWatch(XBT_NIL, SHUTDOWN_PATH, XenBus_ShutdownHandler, NULL);
james@0 756
james@0 757 //KdPrint((__DRIVER_NAME " <-- watch response = '%s'\n", response));
james@0 758
james@0 759 //KdPrint((__DRIVER_NAME " <-- XenBus_ThreadProc\n"));
james@0 760 }
james@0 761 */
james@0 762
james@0 763 static BOOLEAN
james@0 764 XenBus_Interrupt(PKINTERRUPT Interrupt, PVOID ServiceContext)
james@0 765 {
james@0 766 UNREFERENCED_PARAMETER(Interrupt);
james@0 767 UNREFERENCED_PARAMETER(ServiceContext);
james@0 768
james@0 769 //KdPrint((__DRIVER_NAME " --> XenBus_Interrupt (Count = %d)\n", ReadThreadSetCount++));
james@0 770
james@0 771 KeSetEvent(&XenBus_ReadThreadEvent, 1, FALSE);
james@0 772
james@0 773 //KdPrint((__DRIVER_NAME " <-- XenBus_Interrupt\n"));
james@0 774
james@0 775 return TRUE;
james@0 776 }
james@0 777
james@0 778 char *
james@0 779 XenBus_Printf(xenbus_transaction_t xbt, const char *path, const char *fmt, ...)
james@0 780 {
james@0 781 va_list ap;
james@0 782 char buf[1024];
james@0 783
james@0 784 va_start(ap, fmt);
james@0 785 RtlStringCbVPrintfA(buf, ARRAY_SIZE(buf), fmt, ap);
james@0 786 va_end(ap);
james@0 787 return XenBus_Write(xbt, path, buf);
james@0 788 }