win-pvdrivers

annotate xenpci/xenbus.c @ 13:be8c09632f31

fixes to xenpci

Move xenpci globals into XENPCI_DEVICE_DATA struct
External interfaces take a PVOID Context field
Use __writemsr intrinsic
Some lines broken for length
Dead (?) io queue code removed
XEN_IFACE_XEN removed, no one seems to use it?
Use case 3: in XenPCI_XenBusWatchHandler instead of default:
Xenvbd and xennet changed to pass back context field when calling xenpci interfaces
author Andy Grover <andy@groveronline.com>
date Wed Nov 28 14:51:17 2007 -0800 (2007-11-28)
parents b55f3c61e509
children 7f0f866dced6
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
andy@13 34 typedef struct _XENBUS_WATCH_ENTRY {
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
andy@13 42 typedef struct _XENBUS_WATCH_RING
andy@13 43 {
james@0 44 char Path[128];
james@0 45 char Token[10];
james@0 46 } XENBUS_WATCH_RING;
james@0 47
james@0 48 #define WATCH_RING_SIZE 128
james@0 49
james@0 50 static XENBUS_WATCH_RING XenBus_WatchRing[WATCH_RING_SIZE];
james@0 51 static int XenBus_WatchRingReadIndex;
james@0 52 static int XenBus_WatchRingWriteIndex;
james@0 53
james@0 54 #define MAX_WATCH_ENTRIES 128
james@0 55
james@0 56 static XENBUS_WATCH_ENTRY XenBus_WatchEntries[MAX_WATCH_ENTRIES];
james@0 57
james@0 58 #define NR_REQS 32
james@0 59 //#define XENSTORE_RING_SIZE 1024
james@0 60
james@0 61 //#define XENSTORE_RING_SIZE 1024
james@0 62 //typedef uint32_t XENSTORE_RING_IDX;
james@0 63 //#define MASK_XENSTORE_IDX(idx) ((idx) & (XENSTORE_RING_SIZE-1))
james@0 64
james@0 65 static struct xenstore_domain_interface *xen_store_interface;
james@0 66
james@0 67 static struct xenbus_req_info req_info[NR_REQS];
james@0 68 static int nr_live_reqs;
james@0 69 //static spinlock_t req_lock = SPIN_LOCK_UNLOCKED;
james@0 70
james@0 71 static HANDLE XenBus_ReadThreadHandle;
james@0 72 static KEVENT XenBus_ReadThreadEvent;
james@0 73
james@0 74 static HANDLE XenBus_WatchThreadHandle;
james@0 75 static KEVENT XenBus_WatchThreadEvent;
james@0 76
james@0 77 static void
james@0 78 XenBus_ReadThreadProc(PVOID StartContext);
james@0 79 static void
james@0 80 XenBus_WatchThreadProc(PVOID StartContext);
james@0 81
james@0 82 static BOOLEAN
james@0 83 XenBus_Interrupt(PKINTERRUPT Interrupt, PVOID ServiceContext);
james@0 84
james@0 85 static int allocate_xenbus_id(void)
james@0 86 {
james@0 87 static int probe;
james@0 88 int o_probe;
james@0 89
james@0 90 //KdPrint((__DRIVER_NAME " --> allocate_xenbus_id\n"));
james@0 91
james@0 92 for (;;)
james@0 93 {
james@0 94 // spin_lock(&req_lock);
james@0 95 if (nr_live_reqs < NR_REQS)
james@0 96 break;
james@0 97 // spin_unlock(&req_lock);
james@0 98 // wait_event(req_wq, (nr_live_reqs < NR_REQS));
james@0 99 }
james@0 100
james@0 101 o_probe = probe;
james@0 102
james@0 103 for (;;)
james@0 104 {
james@0 105 if (!req_info[o_probe].In_Use)
james@0 106 break;
james@0 107 o_probe = (o_probe + 1) % NR_REQS;
james@0 108 // BUG_ON(o_probe == probe);
james@0 109 }
james@0 110 nr_live_reqs++;
james@0 111 req_info[o_probe].In_Use = 1;
james@0 112 probe = (o_probe + 1) % NR_REQS;
james@0 113 //spin_unlock(&req_lock);
james@0 114 //init_waitqueue_head(&req_info[o_probe].waitq);
james@0 115 KeInitializeEvent(&req_info[o_probe].WaitEvent, SynchronizationEvent, FALSE);
james@0 116
james@0 117 //KdPrint((__DRIVER_NAME " <-- allocate_xenbus_id\n"));
james@0 118
james@0 119 return o_probe;
james@0 120 }
james@0 121
james@0 122 static void release_xenbus_id(int id)
james@0 123 {
james@0 124 // BUG_ON(!req_info[id].in_use);
james@0 125 // spin_lock(&req_lock);
james@0 126 req_info[id].In_Use = 0;
james@0 127 nr_live_reqs--;
james@0 128 req_info[id].In_Use = 0;
james@0 129 // if (nr_live_reqs == NR_REQS - 1)
james@0 130 // wake_up(&req_wq);
james@0 131 // spin_unlock(&req_lock);
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
andy@13 179 static void xb_write (
andy@13 180 WDFDEVICE Device,
andy@13 181 int type,
andy@13 182 int req_id,
andy@13 183 xenbus_transaction_t trans_id,
andy@13 184 const struct write_req *req,
andy@13 185 int nr_reqs)
james@0 186 {
andy@13 187 PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
james@0 188 XENSTORE_RING_IDX prod;
james@0 189 int r;
james@0 190 size_t len = 0;
james@0 191 const struct write_req *cur_req;
james@0 192 size_t req_off;
james@0 193 size_t total_off;
james@0 194 size_t this_chunk;
james@0 195 struct xsd_sockmsg m = {type, req_id, trans_id };
james@0 196 struct write_req header_req = { &m, sizeof(m) };
james@0 197
james@0 198 //KdPrint((__DRIVER_NAME " --> xb_write\n"));
james@0 199
james@0 200 for (r = 0; r < nr_reqs; r++)
james@0 201 len += req[r].len;
james@0 202 m.len = len;
james@0 203 len += sizeof(m);
james@0 204
james@0 205 cur_req = &header_req;
james@0 206
james@0 207 // BUG_ON(len > XENSTORE_RING_SIZE);
james@0 208 /* Wait for the ring to drain to the point where we can send the
james@0 209 message. */
james@0 210 prod = xen_store_interface->req_prod;
james@0 211
james@0 212 //KdPrint((__DRIVER_NAME " prod = %08x\n", prod));
james@0 213
james@0 214 if (prod + len - xen_store_interface->req_cons > XENSTORE_RING_SIZE)
james@0 215 {
james@0 216 /* Wait for there to be space on the ring */
james@0 217 //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 218 // wait_event(xb_waitq, xen_store_interface->req_prod + len - xen_store_interface->req_cons <= XENSTORE_RING_SIZE);
james@0 219 //KdPrint((__DRIVER_NAME " Back from wait.\n"));
james@0 220 prod = xen_store_interface->req_prod;
james@0 221 }
james@0 222
james@0 223 /* We're now guaranteed to be able to send the message without
james@0 224 overflowing the ring. Do so. */
james@0 225
james@0 226 total_off = 0;
james@0 227 req_off = 0;
james@0 228
james@0 229 while (total_off < len)
james@0 230 {
james@0 231 this_chunk = min(cur_req->len - req_off,XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod));
james@0 232 memcpy((char *)xen_store_interface->req + MASK_XENSTORE_IDX(prod), (char *)cur_req->data + req_off, this_chunk);
james@0 233 prod += this_chunk;
james@0 234 req_off += this_chunk;
james@0 235 total_off += this_chunk;
james@0 236 if (req_off == cur_req->len)
james@0 237 {
james@0 238 req_off = 0;
james@0 239 if (cur_req == &header_req)
james@0 240 cur_req = req;
james@0 241 else
james@0 242 cur_req++;
james@0 243 }
james@0 244 }
james@0 245
james@0 246 //KdPrint((__DRIVER_NAME " Complete main loop of xb_write.\n"));
james@0 247
james@0 248 // BUG_ON(req_off != 0);
james@0 249 // BUG_ON(total_off != len);
james@0 250 // BUG_ON(prod > xen_store_interface->req_cons + XENSTORE_RING_SIZE);
james@0 251
james@0 252 /* Remote must see entire message before updating indexes */
james@0 253 //_WriteBarrier();
james@0 254 KeMemoryBarrier();
james@0 255
james@0 256 xen_store_interface->req_prod += len;
james@0 257
james@0 258 //KdPrint((__DRIVER_NAME " prod = %08x\n", xen_store_interface->req_prod));
james@0 259
james@0 260 /* Send evtchn to notify remote */
andy@13 261 EvtChn_Notify(Device, deviceData->xen_store_evtchn);
james@0 262
james@0 263 //KdPrint((__DRIVER_NAME " <-- xb_write\n"));
james@0 264 }
james@0 265
james@0 266 static struct xsd_sockmsg *
andy@13 267 xenbus_msg_reply(
andy@13 268 WDFDEVICE Device,
andy@13 269 int type,
andy@13 270 xenbus_transaction_t trans,
andy@13 271 struct write_req *io,
andy@13 272 int nr_reqs)
james@0 273 {
james@0 274 int id;
james@0 275 // DEFINE_WAIT(w);
james@0 276 struct xsd_sockmsg *rep;
james@0 277
james@6 278 KdPrint((__DRIVER_NAME " --> xenbus_msg_reply\n"));
james@0 279
james@0 280 id = allocate_xenbus_id();
james@0 281 // add_waiter(w, req_info[id].waitq);
james@0 282
andy@13 283 xb_write(Device, type, id, trans, io, nr_reqs);
james@0 284 //
james@0 285 // schedule();
james@0 286 // remove_waiter(w);
james@0 287 // wake(current);
james@0 288 //
james@6 289 KdPrint((__DRIVER_NAME " starting wait\n"));
james@0 290
james@0 291 KeWaitForSingleObject(&req_info[id].WaitEvent, Executive, KernelMode, FALSE, NULL);
james@0 292
james@9 293 //KdPrint((__DRIVER_NAME " wait complete\n"));
james@0 294
james@0 295 rep = req_info[id].Reply;
james@0 296 // BUG_ON(rep->req_id != id);
james@0 297 release_xenbus_id(id);
james@6 298 KdPrint((__DRIVER_NAME " <-- xenbus_msg_reply\n"));
james@0 299 return rep;
james@0 300 }
james@0 301
james@0 302 char *
andy@13 303 XenBus_Read (
andy@13 304 PVOID Context,
andy@13 305 xenbus_transaction_t xbt,
andy@13 306 const char *path,
andy@13 307 char **value)
james@0 308 {
andy@13 309 WDFDEVICE Device = Context;
andy@13 310 struct write_req req[] = { {path, strlen(path) + 1} };
andy@13 311 struct xsd_sockmsg *rep;
andy@13 312 char *res;
andy@13 313 char *msg;
james@0 314
andy@13 315 rep = xenbus_msg_reply(Device, XS_READ, xbt, req, ARRAY_SIZE(req));
andy@13 316 msg = errmsg(rep);
andy@13 317 if (msg) {
andy@13 318 *value = NULL;
andy@13 319 return msg;
andy@13 320 }
andy@13 321 res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
andy@13 322 memcpy(res, rep + 1, rep->len);
andy@13 323 res[rep->len] = 0;
andy@13 324 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
andy@13 325 *value = res;
andy@13 326 return NULL;
james@0 327 }
james@0 328
james@0 329 char *
andy@13 330 XenBus_Write(
andy@13 331 PVOID Context,
andy@13 332 xenbus_transaction_t xbt,
andy@13 333 const char *path,
andy@13 334 const char *value)
james@0 335 {
andy@13 336 WDFDEVICE Device = Context;
andy@13 337 struct write_req req[] = {
andy@13 338 {path, strlen(path) + 1},
andy@13 339 {value, strlen(value) + 1},
andy@13 340 };
andy@13 341 struct xsd_sockmsg *rep;
andy@13 342 char *msg;
james@0 343
andy@13 344 rep = xenbus_msg_reply(Device, XS_WRITE, xbt, req, ARRAY_SIZE(req));
andy@13 345 msg = errmsg(rep);
andy@13 346 if (msg)
andy@13 347 return msg;
andy@13 348 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
andy@13 349 return NULL;
james@0 350 }
james@0 351
james@0 352 char* xenbus_wait_for_value(const char* path,const char* value)
james@0 353 {
james@0 354 UNREFERENCED_PARAMETER(path);
james@0 355 UNREFERENCED_PARAMETER(value);
james@0 356 // for(;;)
james@0 357 // {
james@0 358 // char *res, *msg;
james@0 359 // int r;
james@0 360 //
james@0 361 // msg = xenbus_read(XBT_NIL, path, &res);
james@0 362 // if(msg) return msg;
james@0 363 //
james@0 364 // r = strcmp(value,res);
james@0 365 // ExFreePoolWithTag(res, XENPCI_POOL_TAG);
james@0 366 //
james@0 367 // if(r==0)
james@0 368 // break;
james@0 369 // else
james@0 370 // wait_for_watch();
james@0 371 // }
james@0 372 return NULL;
james@0 373 }
james@0 374
james@0 375 NTSTATUS
andy@13 376 XenBus_Init(WDFDEVICE Device)
james@0 377 {
andy@13 378 PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
james@0 379 //KdPrint((__DRIVER_NAME " --> XenBus_Init\n"));
james@0 380
andy@13 381 deviceData->xen_store_evtchn = EvtChn_GetXenStorePort(Device);
andy@13 382 xen_store_interface = EvtChn_GetXenStoreRingAddr(Device);
james@0 383
james@0 384 //KdPrint((__DRIVER_NAME " xen_store_evtchn = %08x\n", xen_store_evtchn));
james@0 385 //KdPrint((__DRIVER_NAME " xen_store_interface = %08x\n", xen_store_interface));
james@0 386
james@0 387 KeInitializeEvent(&XenBus_ReadThreadEvent, SynchronizationEvent, FALSE);
james@0 388 KeInitializeEvent(&XenBus_WatchThreadEvent, SynchronizationEvent, FALSE);
james@0 389
james@0 390 //KdPrint((__DRIVER_NAME " <-- XenBus_Init\n"));
james@0 391
james@0 392 return STATUS_SUCCESS;
james@0 393 }
james@0 394
james@0 395 NTSTATUS
andy@13 396 XenBus_Start(WDFDEVICE Device)
james@0 397 {
andy@13 398 PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
james@0 399 OBJECT_ATTRIBUTES oa;
james@0 400 NTSTATUS status;
james@0 401 int i;
james@0 402
james@6 403 KdPrint((__DRIVER_NAME " --> XenBus_Start\n"));
james@0 404
james@0 405 InitializeObjectAttributes(&oa, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
james@0 406 status = PsCreateSystemThread(&XenBus_ReadThreadHandle, THREAD_ALL_ACCESS, &oa, NULL, NULL, XenBus_ReadThreadProc, NULL);
james@0 407
james@0 408 // remove all existing watches already in Xen too...
james@0 409
james@0 410 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@0 411 XenBus_WatchEntries[i].Active = 0;
james@0 412
james@0 413 InitializeObjectAttributes(&oa, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
james@0 414 status = PsCreateSystemThread(&XenBus_WatchThreadHandle, THREAD_ALL_ACCESS, &oa, NULL, NULL, XenBus_WatchThreadProc, NULL);
james@0 415
andy@13 416 EvtChn_Bind(Device, deviceData->xen_store_evtchn, XenBus_Interrupt, NULL);
james@0 417
james@6 418 KdPrint((__DRIVER_NAME " <-- XenBus_Start\n"));
james@0 419
james@0 420 return STATUS_SUCCESS;
james@0 421 }
james@0 422
james@0 423 NTSTATUS
andy@13 424 XenBus_Stop(WDFDEVICE Device)
james@0 425 {
andy@13 426 PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
james@0 427 int i;
james@0 428
james@0 429 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@0 430 {
james@0 431 if (!XenBus_WatchEntries[i].Active)
james@0 432 continue;
andy@13 433 XenBus_RemWatch(Device, XBT_NIL, XenBus_WatchEntries[i].Path, XenBus_WatchEntries[i].ServiceRoutine, XenBus_WatchEntries[i].ServiceContext);
james@0 434 }
james@0 435
andy@13 436 EvtChn_Unbind(Device, deviceData->xen_store_evtchn);
james@0 437
james@0 438 // Does this actually stop the threads???
james@0 439 ZwClose(XenBus_WatchThreadHandle);
james@0 440 ZwClose(XenBus_ReadThreadHandle);
james@0 441
james@0 442 return STATUS_SUCCESS;
james@0 443 }
james@0 444
james@0 445 char *
andy@13 446 XenBus_List(
andy@13 447 PVOID Context,
andy@13 448 xenbus_transaction_t xbt,
andy@13 449 const char *pre,
andy@13 450 char ***contents)
james@0 451 {
andy@13 452 WDFDEVICE Device = Context;
james@0 453 struct xsd_sockmsg *reply, *repmsg;
james@0 454 struct write_req req[] = { { pre, strlen(pre)+1 } };
james@0 455 ULONG nr_elems, x, i;
james@0 456 char **res;
james@0 457 char *msg;
james@0 458
james@0 459 //KdPrint((__DRIVER_NAME " --> xenbus_ls\n"));
james@0 460
andy@13 461 repmsg = xenbus_msg_reply(Device, XS_DIRECTORY, xbt, req, ARRAY_SIZE(req));
james@0 462 msg = errmsg(repmsg);
james@0 463 if (msg) {
james@0 464 *contents = NULL;
james@0 465 //KdPrint((__DRIVER_NAME " <-- xenbus_ls (error)\n"));
james@0 466 return msg;
james@0 467 }
james@0 468 reply = repmsg + 1;
james@0 469 for (x = nr_elems = 0; x < repmsg->len; x++)
james@0 470 nr_elems += (((char *)reply)[x] == 0);
james@0 471 res = ExAllocatePoolWithTag(NonPagedPool, sizeof(res[0]) * (nr_elems + 1), XENPCI_POOL_TAG);
james@0 472 for (x = i = 0; i < nr_elems; i++) {
james@0 473 int l = strlen((char *)reply + x);
james@0 474 res[i] = ExAllocatePoolWithTag(NonPagedPool, l + 1, XENPCI_POOL_TAG);
james@0 475 memcpy(res[i], (char *)reply + x, l + 1);
james@0 476 x += l + 1;
james@0 477 }
james@0 478 res[i] = NULL;
james@0 479 ExFreePoolWithTag(repmsg, XENPCI_POOL_TAG);
james@0 480 *contents = res;
james@0 481 //KdPrint((__DRIVER_NAME " <-- xenbus_ls\n"));
james@0 482 return NULL;
james@0 483 }
james@0 484
andy@13 485 #if 0 // test code
james@0 486 void
james@0 487 do_ls_test(const char *pre)
james@0 488 {
james@0 489 char **dirs;
james@0 490 int x;
james@0 491 char *msg;
james@0 492
james@0 493 //KdPrint((__DRIVER_NAME " <-- do_ls_test(\"%s\")\n", pre));
james@0 494
james@0 495 msg = XenBus_List(XBT_NIL, pre, &dirs);
james@0 496 if (msg)
james@0 497 {
james@0 498 //KdPrint((__DRIVER_NAME " Error in xenbus ls: %s\n", msg));
james@0 499 ExFreePoolWithTag(msg, XENPCI_POOL_TAG);
james@0 500 return;
james@0 501 }
james@0 502 for (x = 0; dirs[x]; x++)
james@0 503 {
james@0 504 //KdPrint((__DRIVER_NAME " ls %s[%d] -> %s\n", pre, x, dirs[x]));
james@0 505 ExFreePoolWithTag(dirs[x], XENPCI_POOL_TAG);
james@0 506 }
james@0 507 ExFreePoolWithTag(dirs, XENPCI_POOL_TAG);
james@0 508 //KdPrint((__DRIVER_NAME " --> do_ls_test\n"));
james@0 509 }
andy@13 510 #endif
james@0 511
james@0 512 int ReadThreadSetCount;
james@0 513 int ReadThreadWaitCount;
james@0 514
james@0 515 static void
james@0 516 XenBus_ReadThreadProc(PVOID StartContext) {
james@0 517 int NewWriteIndex;
james@0 518 struct xsd_sockmsg msg;
james@0 519 char *payload;
james@0 520 char *path, *token;
james@0 521
james@0 522 UNREFERENCED_PARAMETER(StartContext);
james@0 523
james@0 524 for(;;)
james@0 525 {
james@0 526 KeWaitForSingleObject(&XenBus_ReadThreadEvent, Executive, KernelMode, FALSE, NULL);
james@0 527 //KdPrint((__DRIVER_NAME " ReadThread Woken (Count = %d)\n", ReadThreadWaitCount++));
james@0 528 while (xen_store_interface->rsp_prod != xen_store_interface->rsp_cons)
james@0 529 {
james@0 530 //KdPrint((__DRIVER_NAME " a - Rsp_cons %d, rsp_prod %d.\n", xen_store_interface->rsp_cons, xen_store_interface->rsp_prod));
james@0 531 if (xen_store_interface->rsp_prod - xen_store_interface->rsp_cons < sizeof(msg))
james@0 532 {
james@0 533 //KdPrint((__DRIVER_NAME " +++ Message incomplete (not even a full header)\n"));
james@0 534 break;
james@0 535 }
james@0 536 //_ReadBarrier();
james@0 537 KeMemoryBarrier();
james@0 538 memcpy_from_ring(xen_store_interface->rsp, &msg, MASK_XENSTORE_IDX(xen_store_interface->rsp_cons), sizeof(msg));
james@0 539 if (xen_store_interface->rsp_prod - xen_store_interface->rsp_cons < sizeof(msg) + msg.len)
james@0 540 {
james@0 541 //KdPrint((__DRIVER_NAME " +++ Message incomplete (header but not full body)\n"));
james@0 542 break;
james@0 543 }
james@0 544
james@0 545 if(msg.type == XS_WATCH_EVENT)
james@0 546 {
james@0 547 payload = ExAllocatePoolWithTag(NonPagedPool, sizeof(msg) + msg.len, XENPCI_POOL_TAG);
james@0 548
james@0 549 memcpy_from_ring(xen_store_interface->rsp, payload, MASK_XENSTORE_IDX(xen_store_interface->rsp_cons), msg.len + sizeof(msg));
james@0 550
james@0 551 xen_store_interface->rsp_cons += msg.len + sizeof(msg);
james@0 552 //KdPrint((__DRIVER_NAME " b - Rsp_cons %d, rsp_prod %d.\n", xen_store_interface->rsp_cons, xen_store_interface->rsp_prod));
james@0 553
james@0 554 path = payload + sizeof(msg);
james@0 555 token = path + strlen(path) + 1;
james@0 556
james@0 557 NewWriteIndex = (XenBus_WatchRingWriteIndex + 1) & 127;
james@0 558 if (NewWriteIndex != XenBus_WatchRingReadIndex)
james@0 559 {
james@0 560 strncpy(XenBus_WatchRing[NewWriteIndex].Path, path, 128);
james@0 561 strncpy(XenBus_WatchRing[NewWriteIndex].Token, token, 10);
james@0 562 XenBus_WatchRingWriteIndex = NewWriteIndex;
james@0 563 }
james@0 564 else
james@0 565 {
james@0 566 KdPrint((__DRIVER_NAME " +++ Queue full Path = %s Token = %s\n", path, token));
james@0 567 // drop the message on the floor
james@0 568 continue;
james@0 569 }
james@0 570
james@0 571 ExFreePoolWithTag(payload, XENPCI_POOL_TAG);
james@0 572 //KdPrint((__DRIVER_NAME " +++ Watch Path = %s Token = %s\n", path, token));
james@0 573 KeSetEvent(&XenBus_WatchThreadEvent, 1, FALSE);
james@0 574 }
james@0 575 else
james@0 576 {
james@0 577 req_info[msg.req_id].Reply = ExAllocatePoolWithTag(NonPagedPool, sizeof(msg) + msg.len, XENPCI_POOL_TAG);
james@0 578 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 579 xen_store_interface->rsp_cons += msg.len + sizeof(msg);
james@0 580 //KdPrint((__DRIVER_NAME " c - Rsp_cons %d, rsp_prod %d.\n", xen_store_interface->rsp_cons, xen_store_interface->rsp_prod));
james@0 581 //KdPrint((__DRIVER_NAME " +++ Message = %s\n", ((char *)req_info[msg.req_id].Reply) + sizeof(msg)));
james@0 582 KeSetEvent(&req_info[msg.req_id].WaitEvent, 1, FALSE);
james@0 583 }
james@0 584 }
james@0 585 }
james@0 586 }
james@0 587
james@0 588 static void
james@0 589 XenBus_WatchThreadProc(PVOID StartContext)
james@0 590 {
james@0 591 int index;
james@0 592 PXENBUS_WATCH_ENTRY entry;
james@0 593
james@0 594 UNREFERENCED_PARAMETER(StartContext);
james@0 595
james@0 596 for(;;)
james@0 597 {
james@0 598 KeWaitForSingleObject(&XenBus_WatchThreadEvent, Executive, KernelMode, FALSE, NULL);
james@0 599 while (XenBus_WatchRingReadIndex != XenBus_WatchRingWriteIndex)
james@0 600 {
james@0 601 XenBus_WatchRingReadIndex = (XenBus_WatchRingReadIndex + 1) & 127;
james@0 602 index = atoi(XenBus_WatchRing[XenBus_WatchRingReadIndex].Token);
james@0 603 //XenBus_WatchRing[XenBus_WatchRingReadIndex].Path
james@0 604 //XenBus_WatchRing[XenBus_WatchRingReadIndex].Token
james@0 605
james@0 606 entry = &XenBus_WatchEntries[index];
james@0 607 if (!entry->Active)
james@0 608 {
james@0 609 KdPrint((__DRIVER_NAME " +++ Watch not active! = %s Token = %s\n", XenBus_WatchRing[XenBus_WatchRingReadIndex].Path, XenBus_WatchRing[XenBus_WatchRingReadIndex].Token));
james@0 610 continue;
james@0 611 }
james@0 612 entry->Count++;
james@0 613 if (!entry->ServiceRoutine)
james@0 614 {
james@0 615 KdPrint((__DRIVER_NAME " +++ no handler for watch! = %s Token = %s\n", XenBus_WatchRing[XenBus_WatchRingReadIndex].Path, XenBus_WatchRing[XenBus_WatchRingReadIndex].Token));
james@0 616 continue;
james@0 617 }
james@0 618 //KdPrint((__DRIVER_NAME " +++ Watch Triggered Path = %s Token = %d (%s)\n", XenBus_WatchRing[XenBus_WatchRingReadIndex].Path, index, XenBus_WatchRing[XenBus_WatchRingReadIndex].Token));
james@0 619 entry->ServiceRoutine(XenBus_WatchRing[XenBus_WatchRingReadIndex].Path, entry->ServiceContext);
james@0 620 }
james@0 621 }
james@0 622 }
james@0 623
james@0 624 char *
andy@13 625 XenBus_AddWatch (
andy@13 626 PVOID Context,
andy@13 627 xenbus_transaction_t xbt,
andy@13 628 const char *Path,
andy@13 629 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 630 PVOID ServiceContext)
james@0 631 {
andy@13 632 WDFDEVICE Device = Context;
james@0 633 struct xsd_sockmsg *rep;
james@0 634 char *msg;
james@0 635 int i;
james@0 636 char Token[20];
james@0 637 struct write_req req[2];
james@0 638
james@6 639 KdPrint((__DRIVER_NAME " --> XenBus_AddWatch\n"));
james@0 640
james@0 641 // check that Path < 128 chars
james@0 642
james@0 643 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@0 644 if (XenBus_WatchEntries[i].Active == 0)
james@0 645 break;
james@0 646
james@0 647 if (i == MAX_WATCH_ENTRIES)
james@0 648 {
james@0 649 KdPrint((__DRIVER_NAME " +++ No more watch slots left\n"));
james@0 650 return NULL;
james@0 651 }
james@0 652
james@0 653 req[0].data = Path;
james@0 654 req[0].len = strlen(Path) + 1;
james@0 655
james@0 656 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", i);
james@0 657 req[1].data = Token;
james@0 658 req[1].len = strlen(Token) + 1;
james@0 659
andy@13 660 rep = xenbus_msg_reply(Device, XS_WATCH, xbt, req, ARRAY_SIZE(req));
james@0 661
james@0 662 msg = errmsg(rep);
james@0 663 if (msg)
james@0 664 return msg;
james@0 665
james@0 666 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 667
james@0 668 strncpy(XenBus_WatchEntries[i].Path, Path, 128);
james@0 669 XenBus_WatchEntries[i].ServiceRoutine = ServiceRoutine;
james@0 670 XenBus_WatchEntries[i].ServiceContext = ServiceContext;
james@0 671 XenBus_WatchEntries[i].Count = 0;
james@0 672 XenBus_WatchEntries[i].Active = 1;
james@0 673
james@6 674 KdPrint((__DRIVER_NAME " <-- XenBus_AddWatch\n"));
james@0 675
james@0 676 return NULL;
james@0 677 }
james@0 678
james@0 679 char *
andy@13 680 XenBus_RemWatch(
andy@13 681 PVOID Context,
andy@13 682 xenbus_transaction_t xbt,
andy@13 683 const char *Path,
andy@13 684 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 685 PVOID ServiceContext)
james@0 686 {
andy@13 687 WDFDEVICE Device = Context;
james@0 688 struct xsd_sockmsg *rep;
james@0 689 char *msg;
james@0 690 int i;
james@0 691 char Token[20];
james@0 692 struct write_req req[2];
james@0 693
james@0 694 //KdPrint((__DRIVER_NAME " --> XenBus_RemWatch\n"));
james@0 695
james@0 696 // check that Path < 128 chars
james@0 697
james@0 698 for (i = 0; i < MAX_WATCH_ENTRIES; i++) {
james@0 699 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 700 break;
james@0 701 }
james@0 702
james@0 703 if (i == MAX_WATCH_ENTRIES)
james@0 704 {
james@0 705 KdPrint((__DRIVER_NAME " Watch not set - can't remove\n"));
james@0 706 return NULL;
james@0 707 }
james@0 708
james@0 709 req[0].data = Path;
james@0 710 req[0].len = strlen(Path) + 1;
james@0 711
james@0 712 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", i);
james@0 713 req[1].data = Token;
james@0 714 req[1].len = strlen(Token) + 1;
james@0 715
andy@13 716 rep = xenbus_msg_reply(Device, XS_UNWATCH, xbt, req, ARRAY_SIZE(req));
james@0 717
james@0 718 msg = errmsg(rep);
james@0 719 if (msg)
james@0 720 return msg;
james@0 721
james@0 722 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 723
james@0 724 XenBus_WatchEntries[i].Active = 0;
james@0 725
james@0 726 //KdPrint((__DRIVER_NAME " <-- XenBus_RemWatch\n"));
james@0 727
james@0 728 return NULL;
james@0 729 }
james@0 730
james@0 731
james@0 732 char *
andy@13 733 XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt)
james@0 734 {
andy@13 735 WDFDEVICE Device = Context;
james@0 736 /* xenstored becomes angry if you send a length 0 message, so just
james@0 737 shove a nul terminator on the end */
james@0 738 struct write_req req = { "", 1};
james@0 739 struct xsd_sockmsg *rep;
james@0 740 char *err;
james@0 741
andy@13 742 rep = xenbus_msg_reply(Device, XS_TRANSACTION_START, 0, &req, 1);
james@0 743 err = errmsg(rep);
james@0 744 if (err)
james@0 745 return err;
james@0 746 *xbt = atoi((char *)(rep + 1));
james@0 747 //sscanf((char *)(rep + 1), "%u", xbt);
james@0 748 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 749 return NULL;
james@0 750 }
james@0 751
james@0 752 char *
andy@13 753 XenBus_EndTransaction(PVOID Context, xenbus_transaction_t t, int abort, int *retry)
james@0 754 {
andy@13 755 WDFDEVICE Device = Context;
james@0 756 struct xsd_sockmsg *rep;
james@0 757 struct write_req req;
james@0 758 char *err;
james@0 759
james@0 760 *retry = 0;
james@0 761
james@0 762 req.data = abort ? "F" : "T";
james@0 763 req.len = 2;
andy@13 764 rep = xenbus_msg_reply(Device, XS_TRANSACTION_END, t, &req, 1);
james@0 765 err = errmsg(rep);
james@0 766 if (err) {
james@0 767 if (!strcmp(err, "EAGAIN")) {
james@0 768 *retry = 1;
james@0 769 ExFreePoolWithTag(err, XENPCI_POOL_TAG);
james@0 770 return NULL;
james@0 771 } else {
james@0 772 return err;
james@0 773 }
james@0 774 }
james@0 775 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 776 return NULL;
james@0 777 }
james@0 778 /*
james@0 779 void
james@0 780 XenBus_ThreadProc(PVOID StartContext)
james@0 781 {
james@0 782 char *response;
james@0 783
james@0 784 //KdPrint((__DRIVER_NAME " --> XenBus_ThreadProc\n"));
james@0 785
james@0 786 //do_ls_test("device");
james@0 787
james@0 788 // do_ls_test("local");
james@0 789
james@0 790 // do_ls_test("control");
james@0 791
james@0 792 // do_ls_test(".");
james@0 793
james@0 794 response = XenBus_AddWatch(XBT_NIL, SHUTDOWN_PATH, XenBus_ShutdownHandler, NULL);
james@0 795
james@0 796 //KdPrint((__DRIVER_NAME " <-- watch response = '%s'\n", response));
james@0 797
james@0 798 //KdPrint((__DRIVER_NAME " <-- XenBus_ThreadProc\n"));
james@0 799 }
james@0 800 */
james@0 801
james@0 802 static BOOLEAN
james@0 803 XenBus_Interrupt(PKINTERRUPT Interrupt, PVOID ServiceContext)
james@0 804 {
james@0 805 UNREFERENCED_PARAMETER(Interrupt);
james@0 806 UNREFERENCED_PARAMETER(ServiceContext);
james@0 807
james@0 808 //KdPrint((__DRIVER_NAME " --> XenBus_Interrupt (Count = %d)\n", ReadThreadSetCount++));
james@0 809
james@0 810 KeSetEvent(&XenBus_ReadThreadEvent, 1, FALSE);
james@0 811
james@0 812 //KdPrint((__DRIVER_NAME " <-- XenBus_Interrupt\n"));
james@0 813
james@0 814 return TRUE;
james@0 815 }
james@0 816
james@0 817 char *
andy@13 818 XenBus_Printf(PVOID Context, xenbus_transaction_t xbt, const char *path, const char *fmt, ...)
james@0 819 {
andy@13 820 WDFDEVICE Device = Context;
james@0 821 va_list ap;
james@0 822 char buf[1024];
james@0 823
james@0 824 va_start(ap, fmt);
james@0 825 RtlStringCbVPrintfA(buf, ARRAY_SIZE(buf), fmt, ap);
james@0 826 va_end(ap);
andy@13 827 return XenBus_Write(Device, xbt, path, buf);
james@0 828 }