win-pvdrivers

annotate xenpci/xenbus.c @ 404:1a1ddc1beba6

implemented mutex's in xenbus. Looks like this broke save/restore somewhere though...
author James Harper <james.harper@bendigoit.com.au>
date Mon Jul 21 15:45:14 2008 +1000 (2008-07-21)
parents 04883e1dc65c
children b9028997f48b
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
andy@15 27 struct write_req {
andy@15 28 const void *data;
andy@15 29 unsigned len;
james@0 30 };
james@0 31
andy@328 32 static DDKAPI void
james@0 33 XenBus_ReadThreadProc(PVOID StartContext);
andy@328 34 static DDKAPI void
james@0 35 XenBus_WatchThreadProc(PVOID StartContext);
andy@328 36 static DDKAPI BOOLEAN
james@0 37 XenBus_Interrupt(PKINTERRUPT Interrupt, PVOID ServiceContext);
james@0 38
james@404 39 /* called with xenbus_mutex held */
james@258 40 static int allocate_xenbus_id(PXENPCI_DEVICE_DATA xpdd)
james@0 41 {
james@0 42 static int probe;
james@0 43 int o_probe;
james@390 44 KIRQL old_irql;
james@0 45
james@342 46 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 47
james@0 48 for (;;)
james@0 49 {
james@390 50 KeAcquireSpinLock(&xpdd->xenbus_id_lock, &old_irql);
andy@15 51 if (xpdd->nr_live_reqs < NR_XB_REQS)
james@0 52 break;
james@390 53 KeReleaseSpinLock(&xpdd->xenbus_id_lock, old_irql);
james@390 54 KeWaitForSingleObject(&xpdd->xenbus_id_event, Executive, KernelMode, FALSE, NULL);
james@0 55 }
james@0 56
james@0 57 o_probe = probe;
james@0 58
james@0 59 for (;;)
james@0 60 {
andy@15 61 if (!xpdd->req_info[o_probe].In_Use)
james@0 62 break;
andy@15 63 o_probe = (o_probe + 1) % NR_XB_REQS;
james@390 64 ASSERT(o_probe != probe);
james@0 65 }
andy@15 66 xpdd->nr_live_reqs++;
andy@15 67 xpdd->req_info[o_probe].In_Use = 1;
andy@15 68 probe = (o_probe + 1) % NR_XB_REQS;
james@390 69 KeReleaseSpinLock(&xpdd->xenbus_id_lock, old_irql);
andy@15 70 KeInitializeEvent(&xpdd->req_info[o_probe].WaitEvent, SynchronizationEvent, FALSE);
james@0 71
james@342 72 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 73
james@0 74 return o_probe;
james@0 75 }
james@0 76
james@404 77 /* called with xenbus_mutex held */
james@258 78 static void release_xenbus_id(PXENPCI_DEVICE_DATA xpdd, int id)
james@0 79 {
james@390 80 KIRQL old_irql;
james@390 81
james@390 82 ASSERT(xpdd->req_info[id].In_Use);
james@390 83 KeAcquireSpinLock(&xpdd->xenbus_id_lock, &old_irql);
james@390 84 xpdd->req_info[id].In_Use = 0;
james@390 85 xpdd->nr_live_reqs--;
james@390 86 xpdd->req_info[id].In_Use = 0;
james@390 87 if (xpdd->nr_live_reqs == NR_XB_REQS - 1)
james@390 88 KeSetEvent(&xpdd->xenbus_id_event, IO_NO_INCREMENT, FALSE);
james@390 89 KeReleaseSpinLock(&xpdd->xenbus_id_lock, old_irql);
james@0 90 }
james@0 91
james@157 92 // This routine free's the rep structure if there was an error!!!
james@0 93 static char *errmsg(struct xsd_sockmsg *rep)
james@0 94 {
james@0 95 char *res;
andy@15 96
james@0 97 if (!rep) {
james@0 98 char msg[] = "No reply";
james@0 99 size_t len = strlen(msg) + 1;
james@0 100 return memcpy(ExAllocatePoolWithTag(NonPagedPool, len, XENPCI_POOL_TAG), msg, len);
james@0 101 }
james@0 102 if (rep->type != XS_ERROR)
james@0 103 return NULL;
james@0 104 res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
james@0 105 memcpy(res, rep + 1, rep->len);
james@0 106 res[rep->len] = 0;
james@0 107 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 108 return res;
james@0 109 }
james@0 110
james@0 111 static void memcpy_from_ring(const void *Ring,
james@0 112 void *Dest,
james@0 113 int off,
james@0 114 int len)
james@0 115 {
james@0 116 int c1, c2;
james@0 117 const char *ring = Ring;
james@0 118 char *dest = Dest;
james@0 119 c1 = min(len, XENSTORE_RING_SIZE - off);
james@0 120 c2 = len - c1;
james@0 121 memcpy(dest, ring + off, c1);
james@0 122 memcpy(dest + c1, ring, c2);
james@0 123 }
james@0 124
james@404 125 /* called with xenbus_mutex held */
andy@15 126 static void xb_write(
james@258 127 PXENPCI_DEVICE_DATA xpdd,
andy@13 128 int type,
andy@13 129 int req_id,
andy@13 130 xenbus_transaction_t trans_id,
andy@13 131 const struct write_req *req,
andy@13 132 int nr_reqs)
james@0 133 {
james@0 134 XENSTORE_RING_IDX prod;
james@0 135 int r;
james@0 136 size_t len = 0;
james@0 137 const struct write_req *cur_req;
james@0 138 size_t req_off;
james@0 139 size_t total_off;
james@0 140 size_t this_chunk;
james@0 141 struct xsd_sockmsg m = {type, req_id, trans_id };
james@0 142 struct write_req header_req = { &m, sizeof(m) };
james@0 143
james@342 144 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 145
james@0 146 for (r = 0; r < nr_reqs; r++)
james@145 147 len += (size_t)req[r].len;
james@145 148 m.len = (ULONG)len;
james@0 149 len += sizeof(m);
james@0 150
james@0 151 cur_req = &header_req;
james@0 152
james@390 153 ASSERT(len <= XENSTORE_RING_SIZE);
james@0 154 /* Wait for the ring to drain to the point where we can send the
james@0 155 message. */
andy@15 156 prod = xpdd->xen_store_interface->req_prod;
james@0 157
james@390 158 while (prod + len - xpdd->xen_store_interface->req_cons > XENSTORE_RING_SIZE)
james@0 159 {
james@0 160 /* Wait for there to be space on the ring */
james@390 161 KeWaitForSingleObject(&xpdd->XenBus_ReadThreadEvent, Executive, KernelMode, FALSE, NULL);
andy@15 162 prod = xpdd->xen_store_interface->req_prod;
james@0 163 }
james@0 164
james@0 165 /* We're now guaranteed to be able to send the message without
james@0 166 overflowing the ring. Do so. */
james@0 167
james@0 168 total_off = 0;
james@0 169 req_off = 0;
james@0 170
james@0 171 while (total_off < len)
james@0 172 {
james@0 173 this_chunk = min(cur_req->len - req_off,XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod));
andy@15 174 memcpy((char *)xpdd->xen_store_interface->req + MASK_XENSTORE_IDX(prod), (char *)cur_req->data + req_off, this_chunk);
james@145 175 prod += (XENSTORE_RING_IDX)this_chunk;
james@0 176 req_off += this_chunk;
james@0 177 total_off += this_chunk;
james@0 178 if (req_off == cur_req->len)
james@0 179 {
james@0 180 req_off = 0;
james@0 181 if (cur_req == &header_req)
james@0 182 cur_req = req;
james@0 183 else
james@0 184 cur_req++;
james@0 185 }
james@0 186 }
james@0 187
james@0 188 /* Remote must see entire message before updating indexes */
james@0 189 KeMemoryBarrier();
james@0 190
james@145 191 xpdd->xen_store_interface->req_prod += (XENSTORE_RING_IDX)len;
james@0 192
james@0 193 /* Send evtchn to notify remote */
james@258 194 EvtChn_Notify(xpdd, xpdd->xen_store_evtchn);
james@0 195
james@342 196 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 197 }
james@0 198
james@404 199 /* called with xenbus_mutex held */
james@0 200 static struct xsd_sockmsg *
andy@13 201 xenbus_msg_reply(
james@258 202 PXENPCI_DEVICE_DATA xpdd,
andy@13 203 int type,
andy@13 204 xenbus_transaction_t trans,
andy@13 205 struct write_req *io,
andy@13 206 int nr_reqs)
james@0 207 {
james@0 208 int id;
james@0 209
james@342 210 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 211
james@258 212 id = allocate_xenbus_id(xpdd);
andy@94 213
james@258 214 xb_write(xpdd, type, id, trans, io, nr_reqs);
james@95 215
andy@15 216 KeWaitForSingleObject(&xpdd->req_info[id].WaitEvent, Executive, KernelMode, FALSE, NULL);
james@95 217
james@258 218 release_xenbus_id(xpdd, id);
james@0 219
james@342 220 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@96 221
andy@94 222 return xpdd->req_info[id].Reply;
james@0 223 }
james@0 224
james@404 225 /*
james@404 226 Called at PASSIVE_LEVEL
james@404 227 Acquires the mutex
james@404 228 */
james@0 229 char *
andy@15 230 XenBus_Read(
andy@13 231 PVOID Context,
andy@13 232 xenbus_transaction_t xbt,
andy@13 233 const char *path,
andy@13 234 char **value)
james@0 235 {
james@258 236 PXENPCI_DEVICE_DATA xpdd = Context;
james@145 237 struct write_req req[] = { {path, (ULONG)strlen(path) + 1} };
andy@13 238 struct xsd_sockmsg *rep;
andy@13 239 char *res;
andy@13 240 char *msg;
james@0 241
james@342 242 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@95 243
james@272 244 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 245
james@404 246 // get mutex or wait for mutex to be acquired
james@404 247
james@404 248 ExAcquireFastMutex(&xpdd->xenbus_mutex);
james@258 249 rep = xenbus_msg_reply(xpdd, XS_READ, xbt, req, ARRAY_SIZE(req));
james@404 250 ExReleaseFastMutex(&xpdd->xenbus_mutex);
andy@13 251 msg = errmsg(rep);
andy@13 252 if (msg) {
andy@13 253 *value = NULL;
andy@13 254 return msg;
andy@13 255 }
andy@13 256 res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
andy@13 257 memcpy(res, rep + 1, rep->len);
andy@13 258 res[rep->len] = 0;
andy@13 259 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
andy@13 260 *value = res;
james@95 261
james@342 262 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 263
andy@13 264 return NULL;
james@0 265 }
james@0 266
james@404 267 /*
james@404 268 Called at PASSIVE_LEVEL
james@404 269 Acquires the mutex
james@404 270 */
james@0 271 char *
andy@13 272 XenBus_Write(
andy@13 273 PVOID Context,
andy@13 274 xenbus_transaction_t xbt,
andy@13 275 const char *path,
andy@13 276 const char *value)
james@0 277 {
james@258 278 PXENPCI_DEVICE_DATA xpdd = Context;
andy@13 279 struct write_req req[] = {
james@145 280 {path, (ULONG)strlen(path) + 1},
andy@264 281 {value, (ULONG)strlen(value)},
andy@13 282 };
andy@13 283 struct xsd_sockmsg *rep;
andy@13 284 char *msg;
james@0 285
james@342 286 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@95 287
james@272 288 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 289
james@404 290 ExAcquireFastMutex(&xpdd->xenbus_mutex);
james@258 291 rep = xenbus_msg_reply(xpdd, XS_WRITE, xbt, req, ARRAY_SIZE(req));
james@404 292 ExReleaseFastMutex(&xpdd->xenbus_mutex);
andy@13 293 msg = errmsg(rep);
andy@13 294 if (msg)
andy@13 295 return msg;
andy@13 296 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 297
james@342 298 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 299
andy@13 300 return NULL;
james@0 301 }
james@0 302
james@390 303 NTSTATUS
james@342 304 XenBus_Connect(PXENPCI_DEVICE_DATA xpdd)
james@342 305 {
james@342 306 PHYSICAL_ADDRESS pa_xen_store_interface;
james@342 307 xen_ulong_t xen_store_mfn;
james@390 308 NTSTATUS status;
james@342 309
james@342 310 xpdd->xen_store_evtchn = (evtchn_port_t)hvm_get_parameter(xpdd, HVM_PARAM_STORE_EVTCHN);
james@342 311 xen_store_mfn = (xen_ulong_t)hvm_get_parameter(xpdd, HVM_PARAM_STORE_PFN);
james@342 312 pa_xen_store_interface.QuadPart = (ULONGLONG)xen_store_mfn << PAGE_SHIFT;
james@342 313 xpdd->xen_store_interface = MmMapIoSpace(pa_xen_store_interface, PAGE_SIZE, MmNonCached);
james@342 314
james@342 315 EvtChn_BindDpc(xpdd, xpdd->xen_store_evtchn, XenBus_Interrupt, xpdd);
james@390 316
james@390 317 xpdd->XenBus_ShuttingDown = FALSE;
james@390 318 KeMemoryBarrier();
james@390 319
james@390 320 status = PsCreateSystemThread(&xpdd->XenBus_ReadThreadHandle, THREAD_ALL_ACCESS, NULL, NULL, NULL, XenBus_ReadThreadProc, xpdd);
james@390 321 if (!NT_SUCCESS(status))
james@390 322 {
james@390 323 KdPrint((__DRIVER_NAME " Could not start read thread\n"));
james@390 324 return status;
james@390 325 }
james@390 326
james@390 327 status = PsCreateSystemThread(&xpdd->XenBus_WatchThreadHandle, THREAD_ALL_ACCESS, NULL, NULL, NULL, XenBus_WatchThreadProc, xpdd);
james@390 328 if (!NT_SUCCESS(status))
james@390 329 {
james@390 330 KdPrint((__DRIVER_NAME " Could not start watch thread\n"));
james@390 331 return status;
james@390 332 }
james@390 333
james@390 334 return STATUS_SUCCESS;
james@342 335 }
james@342 336
james@0 337 NTSTATUS
james@258 338 XenBus_Init(PXENPCI_DEVICE_DATA xpdd)
james@0 339 {
james@390 340 NTSTATUS status;
james@35 341 int i;
andy@37 342
james@267 343 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@35 344
james@272 345 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@35 346
james@128 347 KeInitializeSpinLock(&xpdd->WatchLock);
james@404 348 ExInitializeFastMutex(&xpdd->xenbus_mutex);
james@128 349
james@35 350 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@128 351 {
andy@38 352 xpdd->XenBus_WatchEntries[i].Active = 0;
james@128 353 xpdd->XenBus_WatchEntries[i].Running = 0;
james@128 354 KeInitializeEvent(&xpdd->XenBus_WatchEntries[i].CompleteEvent, SynchronizationEvent, FALSE);
james@128 355 }
james@0 356
andy@15 357 KeInitializeEvent(&xpdd->XenBus_ReadThreadEvent, SynchronizationEvent, FALSE);
andy@15 358 KeInitializeEvent(&xpdd->XenBus_WatchThreadEvent, SynchronizationEvent, FALSE);
james@390 359 KeInitializeEvent(&xpdd->xenbus_id_event, SynchronizationEvent, FALSE);
james@390 360 KeInitializeSpinLock(&xpdd->xenbus_id_lock);
andy@38 361 xpdd->XenBus_ShuttingDown = FALSE;
james@0 362
james@390 363 status = XenBus_Connect(xpdd);
james@390 364 if (!NT_SUCCESS(status))
andy@101 365 {
james@390 366 return status;
andy@101 367 }
james@342 368
james@267 369 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 370
james@0 371 return STATUS_SUCCESS;
james@0 372 }
james@0 373
james@390 374 #if 0
james@0 375 NTSTATUS
james@258 376 XenBus_Stop(PXENPCI_DEVICE_DATA xpdd)
james@0 377 {
james@0 378 int i;
james@0 379
james@267 380 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@35 381
james@0 382 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@0 383 {
andy@38 384 if (xpdd->XenBus_WatchEntries[i].Active)
james@258 385 XenBus_RemWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path,
andy@38 386 xpdd->XenBus_WatchEntries[i].ServiceRoutine,
andy@38 387 xpdd->XenBus_WatchEntries[i].ServiceContext);
james@0 388 }
james@0 389
james@258 390 EvtChn_Unbind(xpdd, xpdd->xen_store_evtchn);
james@0 391
james@267 392 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@35 393
james@35 394 return STATUS_SUCCESS;
james@35 395 }
james@390 396 #endif
james@35 397
james@35 398 NTSTATUS
james@390 399 XenBus_StopThreads(PXENPCI_DEVICE_DATA xpdd)
james@35 400 {
andy@56 401 //KWAIT_BLOCK WaitBlockArray[2];
james@35 402 PVOID WaitArray[2];
james@35 403
james@272 404 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 405
andy@38 406 xpdd->XenBus_ShuttingDown = TRUE;
james@35 407
james@385 408 KeSetEvent(&xpdd->XenBus_ReadThreadEvent, IO_NO_INCREMENT, FALSE);
james@385 409 KeSetEvent(&xpdd->XenBus_WatchThreadEvent, IO_NO_INCREMENT, FALSE);
andy@38 410 ObReferenceObjectByHandle(xpdd->XenBus_ReadThreadHandle, THREAD_ALL_ACCESS, NULL, KernelMode, &WaitArray[0], NULL);
andy@38 411 ObReferenceObjectByHandle(xpdd->XenBus_WatchThreadHandle, THREAD_ALL_ACCESS, NULL, KernelMode, &WaitArray[1], NULL);
james@48 412 KeWaitForSingleObject(WaitArray[0], Executive, KernelMode, FALSE, NULL);
james@48 413 KeWaitForSingleObject(WaitArray[1], Executive, KernelMode, FALSE, NULL);
andy@38 414 xpdd->XenBus_ShuttingDown = FALSE;
james@35 415
andy@15 416 ZwClose(xpdd->XenBus_WatchThreadHandle);
andy@15 417 ZwClose(xpdd->XenBus_ReadThreadHandle);
james@0 418
james@267 419 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@35 420
james@0 421 return STATUS_SUCCESS;
james@0 422 }
james@0 423
james@0 424 char *
andy@13 425 XenBus_List(
andy@13 426 PVOID Context,
andy@13 427 xenbus_transaction_t xbt,
andy@13 428 const char *pre,
andy@13 429 char ***contents)
james@0 430 {
james@258 431 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 432 struct xsd_sockmsg *reply, *repmsg;
james@145 433 struct write_req req[] = { { pre, (ULONG)strlen(pre)+1 } };
james@0 434 ULONG nr_elems, x, i;
james@0 435 char **res;
james@0 436 char *msg;
james@0 437
james@134 438 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 439
james@272 440 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 441
james@258 442 repmsg = xenbus_msg_reply(xpdd, XS_DIRECTORY, xbt, req, ARRAY_SIZE(req));
james@0 443 msg = errmsg(repmsg);
andy@15 444 if (msg)
andy@15 445 {
james@0 446 *contents = NULL;
james@134 447 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 448 return msg;
james@0 449 }
james@0 450 reply = repmsg + 1;
james@0 451 for (x = nr_elems = 0; x < repmsg->len; x++)
andy@15 452 {
james@0 453 nr_elems += (((char *)reply)[x] == 0);
andy@15 454 }
andy@15 455 res = ExAllocatePoolWithTag(NonPagedPool, sizeof(res[0]) * (nr_elems + 1),
andy@15 456 XENPCI_POOL_TAG);
andy@15 457 for (x = i = 0; i < nr_elems; i++)
andy@15 458 {
james@145 459 int l = (int)strlen((char *)reply + x);
james@0 460 res[i] = ExAllocatePoolWithTag(NonPagedPool, l + 1, XENPCI_POOL_TAG);
james@0 461 memcpy(res[i], (char *)reply + x, l + 1);
james@0 462 x += l + 1;
james@0 463 }
james@0 464 res[i] = NULL;
james@0 465 ExFreePoolWithTag(repmsg, XENPCI_POOL_TAG);
james@0 466 *contents = res;
james@134 467 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 468 return NULL;
james@0 469 }
james@0 470
andy@328 471 static DDKAPI void
andy@15 472 XenBus_ReadThreadProc(PVOID StartContext)
james@0 473 {
james@0 474 int NewWriteIndex;
james@0 475 struct xsd_sockmsg msg;
james@0 476 char *payload;
james@0 477 char *path, *token;
james@258 478 PXENPCI_DEVICE_DATA xpdd = StartContext;
james@0 479
james@0 480 for(;;)
james@0 481 {
andy@15 482 KeWaitForSingleObject(&xpdd->XenBus_ReadThreadEvent, Executive, KernelMode, FALSE, NULL);
andy@38 483 if (xpdd->XenBus_ShuttingDown)
james@35 484 {
james@35 485 KdPrint((__DRIVER_NAME " Shutdown detected in ReadThreadProc\n"));
james@35 486 PsTerminateSystemThread(0);
james@35 487 }
andy@15 488 while (xpdd->xen_store_interface->rsp_prod != xpdd->xen_store_interface->rsp_cons)
james@0 489 {
james@0 490 //KdPrint((__DRIVER_NAME " a - Rsp_cons %d, rsp_prod %d.\n", xen_store_interface->rsp_cons, xen_store_interface->rsp_prod));
andy@15 491 if (xpdd->xen_store_interface->rsp_prod - xpdd->xen_store_interface->rsp_cons < sizeof(msg))
james@0 492 {
james@0 493 //KdPrint((__DRIVER_NAME " +++ Message incomplete (not even a full header)\n"));
james@0 494 break;
james@0 495 }
james@0 496 KeMemoryBarrier();
andy@15 497 memcpy_from_ring(xpdd->xen_store_interface->rsp, &msg,
andy@15 498 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons), sizeof(msg));
andy@15 499 if (xpdd->xen_store_interface->rsp_prod - xpdd->xen_store_interface->rsp_cons < sizeof(msg) + msg.len)
james@0 500 {
james@0 501 //KdPrint((__DRIVER_NAME " +++ Message incomplete (header but not full body)\n"));
james@0 502 break;
james@0 503 }
james@0 504
andy@20 505 if (msg.type != XS_WATCH_EVENT)
andy@20 506 {
andy@20 507 xpdd->req_info[msg.req_id].Reply = ExAllocatePoolWithTag(NonPagedPool, sizeof(msg) + msg.len, XENPCI_POOL_TAG);
andy@20 508 memcpy_from_ring(xpdd->xen_store_interface->rsp,
andy@20 509 xpdd->req_info[msg.req_id].Reply,
andy@20 510 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons),
andy@20 511 msg.len + sizeof(msg));
andy@20 512 xpdd->xen_store_interface->rsp_cons += msg.len + sizeof(msg);
james@385 513 KeSetEvent(&xpdd->req_info[msg.req_id].WaitEvent, IO_NO_INCREMENT, FALSE);
andy@20 514 }
andy@20 515 else // a watch: add to watch ring and signal watch thread
james@0 516 {
james@0 517 payload = ExAllocatePoolWithTag(NonPagedPool, sizeof(msg) + msg.len, XENPCI_POOL_TAG);
andy@15 518 memcpy_from_ring(xpdd->xen_store_interface->rsp, payload,
andy@15 519 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons), msg.len + sizeof(msg));
andy@15 520 xpdd->xen_store_interface->rsp_cons += msg.len + sizeof(msg);
james@0 521 path = payload + sizeof(msg);
james@0 522 token = path + strlen(path) + 1;
james@0 523
andy@15 524 NewWriteIndex = (xpdd->XenBus_WatchRingWriteIndex + 1) & 127;
andy@15 525 if (NewWriteIndex != xpdd->XenBus_WatchRingReadIndex)
james@0 526 {
andy@15 527 strncpy(xpdd->XenBus_WatchRing[NewWriteIndex].Path, path, 128);
andy@15 528 strncpy(xpdd->XenBus_WatchRing[NewWriteIndex].Token, token, 10);
andy@15 529 xpdd->XenBus_WatchRingWriteIndex = NewWriteIndex;
james@0 530 }
james@0 531 else
james@0 532 {
james@0 533 KdPrint((__DRIVER_NAME " +++ Queue full Path = %s Token = %s\n", path, token));
james@0 534 // drop the message on the floor
james@0 535 continue;
james@0 536 }
james@0 537
james@0 538 ExFreePoolWithTag(payload, XENPCI_POOL_TAG);
james@385 539 KeSetEvent(&xpdd->XenBus_WatchThreadEvent, IO_NO_INCREMENT, FALSE);
james@0 540 }
james@0 541 }
james@0 542 }
james@0 543 }
james@0 544
andy@328 545 static DDKAPI void
james@0 546 XenBus_WatchThreadProc(PVOID StartContext)
james@0 547 {
james@0 548 int index;
james@0 549 PXENBUS_WATCH_ENTRY entry;
james@258 550 PXENPCI_DEVICE_DATA xpdd = StartContext;
james@128 551 KIRQL OldIrql;
james@0 552
james@0 553 for(;;)
james@0 554 {
andy@37 555 KeWaitForSingleObject(&xpdd->XenBus_WatchThreadEvent, Executive, KernelMode, FALSE, NULL);
andy@38 556 if (xpdd->XenBus_ShuttingDown)
james@35 557 {
james@35 558 KdPrint((__DRIVER_NAME " Shutdown detected in WatchThreadProc\n"));
james@35 559 PsTerminateSystemThread(0);
james@35 560 }
andy@38 561 while (xpdd->XenBus_WatchRingReadIndex != xpdd->XenBus_WatchRingWriteIndex)
james@0 562 {
andy@15 563 xpdd->XenBus_WatchRingReadIndex =
andy@15 564 (xpdd->XenBus_WatchRingReadIndex + 1) % WATCH_RING_SIZE;
andy@15 565 index = atoi(xpdd->XenBus_WatchRing[xpdd->XenBus_WatchRingReadIndex].Token);
james@0 566
andy@15 567 entry = &xpdd->XenBus_WatchEntries[index];
james@128 568 KeAcquireSpinLock(&xpdd->WatchLock, &OldIrql);
james@128 569 if (!entry->Active || !entry->ServiceRoutine)
james@0 570 {
james@128 571 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@138 572 KdPrint((__DRIVER_NAME " No watch for index %d\n", index));
james@0 573 continue;
james@0 574 }
james@138 575 if (entry->RemovePending)
james@138 576 {
james@138 577 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@138 578 KdPrint((__DRIVER_NAME " Not calling watch - remove is pending\n"));
james@138 579 continue;
james@138 580 }
james@128 581 entry->Running = 1;
james@128 582 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@0 583 entry->Count++;
andy@15 584 entry->ServiceRoutine(xpdd->XenBus_WatchRing[xpdd->XenBus_WatchRingReadIndex].Path, entry->ServiceContext);
james@128 585 entry->Running = 0;
james@385 586 KeSetEvent(&entry->CompleteEvent, IO_NO_INCREMENT, FALSE);
james@0 587 }
james@0 588 }
james@0 589 }
james@0 590
james@404 591 /*
james@404 592 Called at PASSIVE_LEVEL
james@404 593 Acquires the mutex
james@404 594 */
james@279 595 static char *
james@279 596 XenBus_SendAddWatch(
james@279 597 PVOID Context,
james@279 598 xenbus_transaction_t xbt,
james@279 599 const char *Path,
james@279 600 int slot)
james@279 601 {
james@279 602 PXENPCI_DEVICE_DATA xpdd = Context;
james@279 603 struct xsd_sockmsg *rep;
james@279 604 char *msg;
james@279 605 char Token[20];
james@279 606 struct write_req req[2];
james@279 607
james@279 608 req[0].data = Path;
james@279 609 req[0].len = (ULONG)strlen(Path) + 1;
james@279 610
james@279 611 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", slot);
james@279 612 req[1].data = Token;
james@279 613 req[1].len = (ULONG)strlen(Token) + 1;
james@279 614
james@404 615 ExAcquireFastMutex(&xpdd->xenbus_mutex);
james@279 616 rep = xenbus_msg_reply(xpdd, XS_WATCH, xbt, req, ARRAY_SIZE(req));
james@404 617 ExReleaseFastMutex(&xpdd->xenbus_mutex);
james@404 618
james@279 619 msg = errmsg(rep);
james@283 620 if (!msg)
james@283 621 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@279 622
james@279 623 return msg;
james@279 624 }
james@279 625
james@342 626 /* called at PASSIVE_LEVEL */
james@390 627 NTSTATUS
james@279 628 XenBus_Resume(PXENPCI_DEVICE_DATA xpdd)
james@279 629 {
james@390 630 NTSTATUS status;
james@279 631 int i;
james@341 632
andy@398 633 FUNCTION_ENTER();
james@342 634
james@390 635 status = XenBus_Connect(xpdd);
james@390 636 if (!NT_SUCCESS(status))
james@390 637 {
james@390 638 return status;
james@390 639 }
james@279 640
james@279 641 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@341 642 {
james@279 643 if (xpdd->XenBus_WatchEntries[i].Active)
james@341 644 {
james@341 645 KdPrint((__DRIVER_NAME " Adding watch for path = %s\n", xpdd->XenBus_WatchEntries[i].Path));
james@279 646 XenBus_SendAddWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path, i);
james@341 647 }
james@341 648 }
andy@398 649 FUNCTION_EXIT();
james@390 650
james@390 651 return STATUS_SUCCESS;
james@279 652 }
james@279 653
james@0 654 char *
andy@15 655 XenBus_AddWatch(
andy@13 656 PVOID Context,
andy@13 657 xenbus_transaction_t xbt,
andy@13 658 const char *Path,
andy@13 659 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 660 PVOID ServiceContext)
james@0 661 {
james@258 662 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 663 char *msg;
james@0 664 int i;
andy@94 665 PXENBUS_WATCH_ENTRY w_entry;
james@128 666 KIRQL OldIrql;
james@0 667
james@267 668 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 669
james@272 670 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 671
andy@94 672 ASSERT(strlen(Path) < ARRAY_SIZE(w_entry->Path));
james@0 673
james@128 674 KeAcquireSpinLock(&xpdd->WatchLock, &OldIrql);
james@128 675
james@0 676 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
andy@15 677 if (xpdd->XenBus_WatchEntries[i].Active == 0)
james@0 678 break;
james@0 679
james@0 680 if (i == MAX_WATCH_ENTRIES)
james@0 681 {
james@0 682 KdPrint((__DRIVER_NAME " +++ No more watch slots left\n"));
james@128 683 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@0 684 return NULL;
james@0 685 }
james@0 686
andy@94 687 /* must init watchentry before starting watch */
james@128 688
andy@94 689 w_entry = &xpdd->XenBus_WatchEntries[i];
andy@94 690 strncpy(w_entry->Path, Path, ARRAY_SIZE(w_entry->Path));
andy@94 691 w_entry->ServiceRoutine = ServiceRoutine;
andy@94 692 w_entry->ServiceContext = ServiceContext;
andy@94 693 w_entry->Count = 0;
andy@94 694 w_entry->Active = 1;
andy@94 695
james@128 696 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@128 697
james@279 698 msg = XenBus_SendAddWatch(xpdd, xbt, Path, i);
james@0 699
andy@93 700 if (msg)
andy@93 701 {
andy@93 702 xpdd->XenBus_WatchEntries[i].Active = 0;
james@341 703 //KdPrint((__DRIVER_NAME " <-- XenBus_AddWatch (%s)\n", msg));
andy@93 704 return msg;
andy@93 705 }
andy@93 706
james@341 707 //KdPrint((__DRIVER_NAME " <-- XenBus_AddWatch\n"));
james@0 708
james@0 709 return NULL;
james@0 710 }
james@0 711
james@0 712 char *
andy@13 713 XenBus_RemWatch(
andy@13 714 PVOID Context,
andy@13 715 xenbus_transaction_t xbt,
andy@13 716 const char *Path,
andy@13 717 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 718 PVOID ServiceContext)
james@0 719 {
james@258 720 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 721 struct xsd_sockmsg *rep;
james@0 722 char *msg;
james@0 723 int i;
james@0 724 char Token[20];
james@0 725 struct write_req req[2];
james@128 726 KIRQL OldIrql;
james@0 727
james@267 728 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@272 729 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 730
james@128 731 KeAcquireSpinLock(&xpdd->WatchLock, &OldIrql);
james@128 732
james@0 733 // check that Path < 128 chars
james@0 734
james@0 735 for (i = 0; i < MAX_WATCH_ENTRIES; i++) {
andy@15 736 if (xpdd->XenBus_WatchEntries[i].Active == 1
andy@15 737 && strcmp(xpdd->XenBus_WatchEntries[i].Path, Path) == 0
andy@15 738 && xpdd->XenBus_WatchEntries[i].ServiceRoutine == ServiceRoutine
andy@15 739 && xpdd->XenBus_WatchEntries[i].ServiceContext == ServiceContext)
james@0 740 break;
james@0 741 }
james@0 742
james@0 743 if (i == MAX_WATCH_ENTRIES)
james@0 744 {
james@128 745 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@0 746 KdPrint((__DRIVER_NAME " Watch not set - can't remove\n"));
james@0 747 return NULL;
james@0 748 }
james@0 749
james@138 750 if (xpdd->XenBus_WatchEntries[i].RemovePending)
james@128 751 {
james@132 752 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@138 753 KdPrint((__DRIVER_NAME " Remove already pending - can't remove\n"));
james@138 754 return NULL;
james@138 755 }
james@138 756 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@138 757
james@138 758 while (xpdd->XenBus_WatchEntries[i].Running)
james@128 759 KeWaitForSingleObject(&xpdd->XenBus_WatchEntries[i].CompleteEvent, Executive, KernelMode, FALSE, NULL);
james@138 760
james@138 761 KeAcquireSpinLock(&xpdd->WatchLock, &OldIrql);
james@128 762
james@128 763 xpdd->XenBus_WatchEntries[i].Active = 0;
james@138 764 xpdd->XenBus_WatchEntries[i].RemovePending = 0;
james@128 765 xpdd->XenBus_WatchEntries[i].Path[0] = 0;
james@128 766
james@128 767 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@128 768
james@0 769 req[0].data = Path;
james@145 770 req[0].len = (ULONG)strlen(Path) + 1;
james@0 771
james@0 772 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", i);
james@0 773 req[1].data = Token;
james@145 774 req[1].len = (ULONG)strlen(Token) + 1;
james@0 775
james@258 776 rep = xenbus_msg_reply(xpdd, XS_UNWATCH, xbt, req, ARRAY_SIZE(req));
james@0 777
james@0 778 msg = errmsg(rep);
james@0 779 if (msg)
james@128 780 {
james@0 781 return msg;
james@128 782 }
james@0 783
james@0 784 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 785
james@267 786 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 787
james@0 788 return NULL;
james@0 789 }
james@0 790
james@0 791
james@0 792 char *
andy@13 793 XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt)
james@0 794 {
james@258 795 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 796 /* xenstored becomes angry if you send a length 0 message, so just
james@0 797 shove a nul terminator on the end */
james@0 798 struct write_req req = { "", 1};
james@0 799 struct xsd_sockmsg *rep;
james@0 800 char *err;
james@0 801
james@134 802 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@272 803 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@95 804
james@258 805 rep = xenbus_msg_reply(xpdd, XS_TRANSACTION_START, 0, &req, 1);
james@0 806 err = errmsg(rep);
james@0 807 if (err)
james@0 808 return err;
james@0 809 *xbt = atoi((char *)(rep + 1));
james@0 810 //sscanf((char *)(rep + 1), "%u", xbt);
james@0 811 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 812
james@134 813 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 814
james@0 815 return NULL;
james@0 816 }
james@0 817
james@0 818 char *
andy@15 819 XenBus_EndTransaction(
andy@15 820 PVOID Context,
andy@15 821 xenbus_transaction_t t,
andy@15 822 int abort,
andy@15 823 int *retry)
james@0 824 {
james@258 825 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 826 struct xsd_sockmsg *rep;
james@0 827 struct write_req req;
james@0 828 char *err;
james@0 829
james@134 830 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@95 831
james@0 832 *retry = 0;
james@0 833
james@0 834 req.data = abort ? "F" : "T";
james@0 835 req.len = 2;
james@258 836 rep = xenbus_msg_reply(xpdd, XS_TRANSACTION_END, t, &req, 1);
james@0 837 err = errmsg(rep);
james@0 838 if (err) {
james@0 839 if (!strcmp(err, "EAGAIN")) {
james@0 840 *retry = 1;
james@0 841 ExFreePoolWithTag(err, XENPCI_POOL_TAG);
james@0 842 return NULL;
james@0 843 } else {
james@0 844 return err;
james@0 845 }
james@0 846 }
james@0 847 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 848
james@134 849 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 850
james@0 851 return NULL;
james@0 852 }
james@0 853
andy@328 854 static DDKAPI BOOLEAN
james@0 855 XenBus_Interrupt(PKINTERRUPT Interrupt, PVOID ServiceContext)
james@0 856 {
james@258 857 PXENPCI_DEVICE_DATA xpdd = ServiceContext;
andy@15 858
james@0 859 UNREFERENCED_PARAMETER(Interrupt);
james@0 860
james@134 861 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 862
james@385 863 KeSetEvent(&xpdd->XenBus_ReadThreadEvent, IO_NO_INCREMENT, FALSE);
james@0 864
james@134 865 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 866
james@0 867 return TRUE;
james@0 868 }
james@0 869
james@0 870 char *
andy@15 871 XenBus_Printf(
andy@15 872 PVOID Context,
andy@15 873 xenbus_transaction_t xbt,
andy@15 874 const char *path,
andy@15 875 const char *fmt,
andy@15 876 ...)
james@0 877 {
james@258 878 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 879 va_list ap;
andy@101 880 char buf[512];
james@95 881 char *retval;
james@95 882
james@134 883 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@272 884 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 885
james@0 886 va_start(ap, fmt);
james@0 887 RtlStringCbVPrintfA(buf, ARRAY_SIZE(buf), fmt, ap);
james@0 888 va_end(ap);
james@258 889 retval = XenBus_Write(xpdd, xbt, path, buf);
james@95 890
james@134 891 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 892
james@95 893 return retval;
andy@37 894 }